cheroot.connections module#
Utilities to manage open connections.
- class cheroot.connections.ConnectionManager(server)#
Bases:
object
Class which manages HTTPConnection objects.
This is for connections which are being kept-alive for follow-up requests.
- _expire(threshold)#
Expire least recently used connections.
- Parameters:
threshold (float) – Connections that have not been used within this duration (in seconds), are considered expired and are closed and removed.
This should be called periodically.
- _from_server_socket(server_socket)#
- property _num_connections#
Return the current number of connections.
Includes all connections registered with the selector, minus one for the server socket, which is always registered with the selector.
- _remove_invalid_sockets()#
Clean up the resources of any broken connections.
This method attempts to detect any connections in an invalid state, unregisters them from the selector and closes the file descriptors of the corresponding network sockets where possible.
- _run(expiration_interval)#
Run connection handler loop until stop was requested.
- Parameters:
expiration_interval (float) – Interval, in seconds, at which connections will be checked for expiration.
Use
expiration_interval
asselect()
timeout to assure expired connections are closed in time.On Windows cap the timeout to 0.05 seconds as
select()
does not return when a socket is ready.
- property can_add_keepalive_connection#
Flag whether it is allowed to add a new keep-alive connection.
- close()#
Close all monitored connections.
- put(conn)#
Put idle connection into the ConnectionManager to be managed.
- Parameters:
conn (cheroot.server.HTTPConnection) – HTTP connection to be managed
- run(expiration_interval)#
Run the connections selector indefinitely.
- Args:
- expiration_interval (float): Interval, in seconds, at which
connections will be checked for expiration.
Connections that are ready to process are submitted via self.server.process_conn()
Connections submitted for processing must be
put()
back if they should be examined again for another request.Can be shut down by calling
stop()
.
- stop()#
Stop the selector loop in run() synchronously.
May take up to half a second.
- class cheroot.connections._ThreadsafeSelector#
Bases:
object
Thread-safe wrapper around a DefaultSelector.
- There are 2 thread contexts in which it may be accessed:
the selector thread
one of the worker threads in workers/threadpool.py
- The expected read/write patterns are:
iter()
: selector threadregister()
: selector thread and threadpool, viaput()
unregister()
: selector thread only
Notably, this means
_ThreadsafeSelector
never needs to worry that connections will be removed behind its back.The lock is held when iterating or modifying the selector but is not required when
select()ing
on it.- close()#
Close the selector.
- property connections#
Retrieve connections registered with the selector.
- register(fileobj, events, data=None)#
Register
fileobj
with the selector.
- select(timeout=None)#
Return socket fd and data pairs from selectors.select call.
- Returns entries ready to read in the form:
(socket_file_descriptor, connection)
- unregister(fileobj)#
Unregister
fileobj
from the selector.
- cheroot.connections.prevent_socket_inheritance(sock)#
Mark the given socket fd as non-inheritable (POSIX).