Skip to content

Commit

Permalink
Avoid unnecessary yield if buffer has not fully accumulated yet
Browse files Browse the repository at this point in the history
This reduces the number of yields from WebSocket to the stream parser by
accumulating self.reading_buffer_size worth of bytes before yielding.
  • Loading branch information
hyperair committed Jun 8, 2017
1 parent 0714fb2 commit 56c38ee
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ws4py/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,19 @@ def once(self):
logger.debug("WebSocket is already terminated")
return False
try:
if not self.buf:
b = self.sock.recv(self.reading_buffer_size)
wanted = self.reading_buffer_size - len(self.buf)

if wanted > 0:
b = self.sock.recv(wanted)

if self._is_secure:
b += self._get_from_pending()
if not b:

if not b: # 0-length read. flush buffer and exit
if self.buf:
self.process(self.buf)
return False

self.buf += b
except (socket.error, OSError, pyOpenSSLError) as e:
if hasattr(e, "errno") and e.errno == errno.EINTR:
Expand Down

0 comments on commit 56c38ee

Please sign in to comment.