mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
gthread: only read sockets when they are readable
This commit is contained in:
parent
2f9eb19b66
commit
0ebb73aa24
@ -40,12 +40,15 @@ class TConn(object):
|
|||||||
|
|
||||||
self.timeout = None
|
self.timeout = None
|
||||||
self.parser = None
|
self.parser = None
|
||||||
|
self.initialized = False
|
||||||
|
|
||||||
# set the socket to non blocking
|
# set the socket to non blocking
|
||||||
self.sock.setblocking(False)
|
self.sock.setblocking(False)
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
|
self.initialized = True
|
||||||
self.sock.setblocking(True)
|
self.sock.setblocking(True)
|
||||||
|
|
||||||
if self.parser is None:
|
if self.parser is None:
|
||||||
# wrap the socket if needed
|
# wrap the socket if needed
|
||||||
if self.cfg.is_ssl:
|
if self.cfg.is_ssl:
|
||||||
@ -120,23 +123,28 @@ class ThreadWorker(base.Worker):
|
|||||||
# initialize the connection object
|
# initialize the connection object
|
||||||
conn = TConn(self.cfg, sock, client, server)
|
conn = TConn(self.cfg, sock, client, server)
|
||||||
self.nr_conns += 1
|
self.nr_conns += 1
|
||||||
# enqueue the job
|
|
||||||
self.enqueue_req(conn)
|
# wait until socket is readable
|
||||||
|
with self._lock:
|
||||||
|
self.poller.register(conn.sock, selectors.EVENT_READ,
|
||||||
|
partial(self.on_client_socket_readable, conn))
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
|
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
|
||||||
errno.EWOULDBLOCK):
|
errno.EWOULDBLOCK):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def reuse_connection(self, conn, client):
|
def on_client_socket_readable(self, conn, client):
|
||||||
with self._lock:
|
with self._lock:
|
||||||
# unregister the client from the poller
|
# unregister the client from the poller
|
||||||
self.poller.unregister(client)
|
self.poller.unregister(client)
|
||||||
# remove the connection from keepalive
|
|
||||||
try:
|
if conn.initialized:
|
||||||
self._keep.remove(conn)
|
# remove the connection from keepalive
|
||||||
except ValueError:
|
try:
|
||||||
# race condition
|
self._keep.remove(conn)
|
||||||
return
|
except ValueError:
|
||||||
|
# race condition
|
||||||
|
return
|
||||||
|
|
||||||
# submit the connection to a worker
|
# submit the connection to a worker
|
||||||
self.enqueue_req(conn)
|
self.enqueue_req(conn)
|
||||||
@ -249,7 +257,7 @@ class ThreadWorker(base.Worker):
|
|||||||
|
|
||||||
# add the socket to the event loop
|
# add the socket to the event loop
|
||||||
self.poller.register(conn.sock, selectors.EVENT_READ,
|
self.poller.register(conn.sock, selectors.EVENT_READ,
|
||||||
partial(self.reuse_connection, conn))
|
partial(self.on_client_socket_readable, conn))
|
||||||
else:
|
else:
|
||||||
self.nr_conns -= 1
|
self.nr_conns -= 1
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user