Skip to content

Commit

Permalink
Better handling of queued WebSocket messages in uWSGI (Fixes #256)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Apr 30, 2022
1 parent 83b5719 commit ec7b3da
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/engineio/async_drivers/gevent_uwsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class uWSGIWebSocket(object): # pragma: no cover
def __init__(self, app):
self.app = app
self._sock = None
self.received_messages = []

def __call__(self, environ, start_response):
self._sock = uwsgi.connection_fd()
Expand Down Expand Up @@ -117,6 +118,9 @@ def wait(self):
return None
return self._decode_received(msg)
else:
if self.received_messages:
return self.received_messages.pop(0)

# we wake up at least every 3 seconds to let uWSGI
# do its ping/ponging
event_set = self._event.wait(timeout=3)
Expand All @@ -133,13 +137,19 @@ def wait(self):
self._send(msg)
# maybe there is something to receive, if not, at least
# ensure uWSGI does its ping/ponging
try:
msg = uwsgi.websocket_recv_nb()
except IOError: # connection closed
self._select_greenlet.kill()
return None
if msg: # message available
return self._decode_received(msg)
while True:
try:
msg = uwsgi.websocket_recv_nb()
except IOError: # connection closed
self._select_greenlet.kill()
return None
if msg: # message available
self.received_messages.append(
self._decode_received(msg))
else:
break
if self.received_messages:
return self.received_messages.pop(0)


_async = {
Expand Down

0 comments on commit ec7b3da

Please sign in to comment.