Skip to content

Commit

Permalink
Give eventlet access to the socket when running under gunicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 16, 2015
1 parent 8c3eb84 commit 3c63157
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions engineio/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ def __init__(self, engineio_app, wsgi_app=None, engineio_path='engine.io'):
self.engineio_path = engineio_path

def __call__(self, environ, start_response):
if 'gunicorn.socket' in environ:
# gunicorn saves the socket under environ['gunicorn.socket'], while
# eventlet saves it under environ['eventlet.input']. Eventlet also
# stores the socket inside a wrapper class, while gunicon writes it
# directly into the environment. To give eventlet's WebSocket
# module access to this socket when running under gunicorn, here we
# copy the socket to the eventlet format.
class Input(object):
def __init__(self, socket):
self.socket = socket

def get_socket(self):
return self.socket

environ['eventlet.input'] = Input(environ['gunicorn.socket'])
path = environ['PATH_INFO']
if path is not None and \
path.startswith('/{0}/'.format(self.engineio_path)):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ def test_404(self):
self.assertEqual(r, ['Not Found'])
start_response.assert_called_once_with(
"404 Not Found", [('Content-type', 'text/plain')])

def test_gunicorn_socket(self):
mock_wsgi_app = None
mock_eio_app = mock.Mock()
m = middleware.Middleware(mock_eio_app, mock_wsgi_app)
environ = {'gunicorn.socket': 123, 'PATH_INFO': '/foo/bar'}
start_response = mock.MagicMock()
m(environ, start_response)
self.assertIn('eventlet.input', environ)
self.assertEqual(environ['eventlet.input'].get_socket(), 123)

0 comments on commit 3c63157

Please sign in to comment.