Skip to content

Commit

Permalink
connections: convert _num_connections to property, clarify expire()
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstask committed Aug 12, 2020
1 parent d1fdf9f commit 3135db5
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions cheroot/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from . import errors
from ._compat import selectors
from ._compat import suppress
from .makefile import MakeFile

import six
Expand Down Expand Up @@ -98,15 +99,15 @@ def expire(self):

# find any connections still registered with the selector
# that have not been active recently enough.
now = time.time()
for _, key in self._selector.get_map().items():
if key.data == self.server:
continue

conn = key.data
if (conn.last_used + self.server.timeout) < now:
self._selector.unregister(key.fd)
conn.close()
threshold = time.time() - self.server.timeout
timed_out_connections = (
(sock_fd, conn)
for _, (_, sock_fd, _, conn) in self._selector.get_map().items()
if conn != self.server and conn.last_used < threshold
)
for sock_fd, conn in timed_out_connections:
self._selector.unregister(sock_fd)
conn.close()

def get_conn(self):
"""Return a HTTPConnection object which is ready to be handled.
Expand All @@ -122,13 +123,9 @@ def get_conn(self):
cheroot.server.HTTPConnection instance, or None.
"""
# Grab file descriptors from sockets, but stop if we find a
# connection which is already marked as ready.

try:
# return a readable connection if any exist
with suppress(IndexError):
return self._readable_conns.popleft()
except IndexError:
pass

# Will require a select call.
try:
Expand Down Expand Up @@ -276,8 +273,9 @@ def close(self):

self._selector.close()

@property
def _num_connections(self):
"""Return the current number of connections.
"""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 All @@ -289,4 +287,4 @@ def _num_connections(self):
def can_add_keepalive_connection(self):
"""Flag whether it is allowed to add a new keep-alive connection."""
ka_limit = self.server.keep_alive_conn_limit
return ka_limit is None or self._num_connections() < ka_limit
return ka_limit is None or self._num_connections < ka_limit

0 comments on commit 3135db5

Please sign in to comment.