Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed chunked error

  • Loading branch information...
commit 9e2935cd00986e2459022151ee69e3c462190224 1 parent 61b81f2
@tarekziade tarekziade authored
View
16 vaurien/protocols/memcache.py
@@ -19,7 +19,6 @@ class Memcache(BaseProtocol):
def _handle(self, source, dest, to_backend):
# https://github.com/memcached/memcached/blob/master/doc/protocol.txt
-
# Sending the query
buffer = self._get_data(source)
if not buffer:
@@ -41,13 +40,16 @@ def _handle(self, source, dest, to_backend):
cmd = cmd.groups()[0]
buffer_size = self.option('buffer')
- if cmd in ('set', 'add', 'replace', 'append'):
- cmd_size = len(cmd) + len('\r\n')
- data_size = int(cmd.split()[-1])
+ cmd_parts = cmd.split()
+ mcmd = cmd_parts[0]
+
+ if mcmd in ('set', 'add', 'replace', 'append'):
+ cmd_size = len(cmd) + len(CRLF)
+ data_size = int(cmd_parts[-1])
total_size = cmd_size + data_size
# grabbing more data if needed
- left_to_read = total_size - len(buffer)
+ left_to_read = total_size - len(buffer) + len(CRLF)
if left_to_read > 0:
for chunk in chunked(left_to_read, buffer_size):
data = source.recv(chunk)
@@ -60,9 +62,9 @@ def _handle(self, source, dest, to_backend):
if buffer.startswith('VALUE'):
# we're getting back a value.
- EOW = 'END\r\n'
+ EOW = 'END' + CRLF
else:
- EOW = '\r\n'
+ EOW = CRLF
while not buffer.endswith(EOW):
data = dest.recv(buffer_size)
View
8 vaurien/tests/test_util.py
@@ -0,0 +1,8 @@
+import unittest
+from vaurien.util import chunked
+
+
+class TestUtil(unittest.TestCase):
+
+ def test_chunked(self):
+ self.assertEqual(sum(list(chunked(7634, 2049))), 7634)
View
11 vaurien/util.py
@@ -173,12 +173,13 @@ def chunked(total, chunk):
yield total
else:
data = total
- while data > 0:
- yield chunk
- if data < chunk:
- chunk = data
- else:
+ while True:
+ if data > chunk:
+ yield chunk
data -= chunk
+ else:
+ yield data
+ break
def get_data(sock, buffer=1024):
Please sign in to comment.
Something went wrong with that request. Please try again.