Skip to content

Commit

Permalink
server: make connections 'private'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstask committed Aug 23, 2020
1 parent e26d758 commit c94eaab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cheroot/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ def close(self):
self._selector.close()

@property
def _num_connections(self): # noqa: D401
"""The current number of connections.
def _num_connections(self):
"""Return the current number of connections.
Includes any in the readable list or registered with the selector,
minus one for the server socket, which is always registered
Expand Down
27 changes: 19 additions & 8 deletions cheroot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,10 +1162,7 @@ def send_headers(self): # noqa: C901 # FIXME
# Override the decision to not close the connection if the connection
# manager doesn't have space for it.
if not self.close_connection:
can_keep = (
self.server.ready
and self.server.connections.can_add_keepalive_connection
)
can_keep = self.server.can_add_keepalive_connection
self.close_connection = not can_keep

if b'connection' not in hkeys:
Expand Down Expand Up @@ -1784,7 +1781,8 @@ def prepare(self): # noqa: C901 # FIXME
self.socket.settimeout(1)
self.socket.listen(self.request_queue_size)

self.connections = connections.ConnectionManager(self)
# must not be accessed once stop() has been called
self._connections = connections.ConnectionManager(self)

# Create worker threads
self.requests.start()
Expand Down Expand Up @@ -1832,6 +1830,19 @@ def _run_in_thread(self):
finally:
self.stop()

@property
def can_add_keepalive_connection(self):
"""Flag whether it is allowed to add a new keep-alive connection."""
return self.ready and self._connections.can_add_keepalive_connection

def put_conn(self, conn):
"""Put an idle connection back into the ConnectionManager."""
if self.ready:
self._connections.put(conn)
else:
# server is shutting down, just close it
conn.close()

def error_log(self, msg='', level=20, traceback=False):
"""Write error message to log.
Expand Down Expand Up @@ -2024,15 +2035,15 @@ def resolve_real_bind_addr(socket_):

def tick(self):
"""Accept a new connection and put it on the Queue."""
conn = self.connections.get_conn()
conn = self._connections.get_conn()
if conn:
try:
self.requests.put(conn)
except queue.Full:
# Just drop the conn. TODO: write 503 back?
conn.close()

self.connections.expire()
self._connections.expire()

@property
def interrupt(self):
Expand Down Expand Up @@ -2100,7 +2111,7 @@ def stop(self): # noqa: C901 # FIXME
sock.close()
self.socket = None

self.connections.close()
self._connections.close()
self.requests.stop(self.shutdown_timeout)


Expand Down
2 changes: 1 addition & 1 deletion cheroot/workers/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def run(self):
keep_conn_open = conn.communicate()
finally:
if keep_conn_open:
self.server.connections.put(conn)
self.server.put_conn(conn)
else:
conn.close()
if is_stats_enabled:
Expand Down

0 comments on commit c94eaab

Please sign in to comment.