Skip to content

Commit

Permalink
fixup - decompressing recv_into() needs to handle too-big recv()
Browse files Browse the repository at this point in the history
I suspect the bootstrap thing is flawed ...
but let's not fix that now (seems to have worked for a while anyway)
  • Loading branch information
ploxiln committed Mar 27, 2019
1 parent 075221b commit aa3d364
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion nsq/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def upgrade_to_deflate(self):
# is currently in place (normal or SSL)...
#
# first read any compressed bytes the existing IOStream might have
# already buffered and use that to bootstrap the DefalteSocket, then
# already buffered and use that to bootstrap the DeflateSocket, then
# monkey patch the existing IOStream by replacing its socket
# with a wrapper that will automagically handle compression.
existing_data = self.stream._consume(self.stream._read_buffer_size)
Expand Down
11 changes: 8 additions & 3 deletions nsq/deflate_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ def read(self, size):

def recv_into(self, buf, nbytes=0):
# no real support of efficient recv_into()
data = self.recv(nbytes or len(buf))
buf[:len(data)] = data
return len(data)
n = nbytes or len(buf)
data = self.recv(n)
r = len(data)
if r > n:
self._bootstrapped = data[n:]
r = n
buf[:r] = data[:r]
return r

def _recv(self, size, method):
if self._bootstrapped:
Expand Down
11 changes: 8 additions & 3 deletions nsq/snappy_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ def read(self, size):

def recv_into(self, buf, nbytes=0):
# no real support of efficient recv_into()
data = self.recv(nbytes or len(buf))
buf[:len(data)] = data
return len(data)
n = nbytes or len(buf)
data = self.recv(n)
r = len(data)
if r > n:
self._bootstrapped = data[n:]
r = n
buf[:r] = data[:r]
return r

def _recv(self, size, method):
if self._bootstrapped:
Expand Down

0 comments on commit aa3d364

Please sign in to comment.