From c5076f5e019b73ec2f6efea5a4bafed90423df8f Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 3 Jun 2016 11:47:07 +1200 Subject: Implement a service connection handler counter, use it in Pathod test suite Lots of failures, but that's a good thing. --- netlib/tcp.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'netlib/tcp.py') diff --git a/netlib/tcp.py b/netlib/tcp.py index 914aa701..bb0c93a9 100644 --- a/netlib/tcp.py +++ b/netlib/tcp.py @@ -833,6 +833,25 @@ class BaseHandler(_Connection): return b"" +class Counter: + def __init__(self): + self._count = 0 + self._lock = threading.Lock() + + @property + def count(self): + with self._lock: + return self._count + + def __enter__(self): + with self._lock: + self._count += 1 + + def __exit__(self, *args): + with self._lock: + self._count -= 1 + + class TCPServer(object): request_queue_size = 20 @@ -845,15 +864,17 @@ class TCPServer(object): self.socket.bind(self.address()) self.address = Address.wrap(self.socket.getsockname()) self.socket.listen(self.request_queue_size) + self.counter = Counter() def connection_thread(self, connection, client_address): - client_address = Address(client_address) - try: - self.handle_client_connection(connection, client_address) - except: - self.handle_error(connection, client_address) - finally: - close_socket(connection) + with self.counter: + client_address = Address(client_address) + try: + self.handle_client_connection(connection, client_address) + except: + self.handle_error(connection, client_address) + finally: + close_socket(connection) def serve_forever(self, poll_interval=0.1): self.__is_shut_down.clear() -- cgit v1.2.3 From e60860e65d06d2b4452b7ea94902d79eed11d78c Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 3 Jun 2016 12:06:36 +1200 Subject: Make tcp.Client.connect return a context manager that closes the connection --- netlib/tcp.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'netlib/tcp.py') diff --git a/netlib/tcp.py b/netlib/tcp.py index bb0c93a9..61209d64 100644 --- a/netlib/tcp.py +++ b/netlib/tcp.py @@ -6,6 +6,7 @@ import sys import threading import time import traceback +import contextlib import binascii from six.moves import range @@ -577,6 +578,12 @@ class _Connection(object): return context +@contextlib.contextmanager +def _closer(client): + yield + client.close() + + class TCPClient(_Connection): def __init__(self, address, source_address=None): @@ -708,6 +715,7 @@ class TCPClient(_Connection): self.connection = connection self.ip_address = Address(connection.getpeername()) self._makefile() + return _closer(self) def settimeout(self, n): self.connection.settimeout(n) -- cgit v1.2.3 From 6943d7e3977a53df83267c1bbe58f60b9867a2a6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 3 Jun 2016 13:57:12 +1200 Subject: More explicit name for the tcp.Server handler counter --- netlib/tcp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'netlib/tcp.py') diff --git a/netlib/tcp.py b/netlib/tcp.py index 61209d64..de12102e 100644 --- a/netlib/tcp.py +++ b/netlib/tcp.py @@ -872,10 +872,10 @@ class TCPServer(object): self.socket.bind(self.address()) self.address = Address.wrap(self.socket.getsockname()) self.socket.listen(self.request_queue_size) - self.counter = Counter() + self.handler_counter = Counter() def connection_thread(self, connection, client_address): - with self.counter: + with self.handler_counter: client_address = Address(client_address) try: self.handle_client_connection(connection, client_address) -- cgit v1.2.3