From 114610016813129e8cbef1a238a8e0beaa75afb4 Mon Sep 17 00:00:00 2001 From: TheKysek Date: Sat, 21 Jan 2017 14:05:13 +0100 Subject: [PATCH] Limit number of connections --- src/listener.py | 9 ++++++--- src/shared.py | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/listener.py b/src/listener.py index 499a006..d344da9 100644 --- a/src/listener.py +++ b/src/listener.py @@ -28,8 +28,11 @@ class Listener(threading.Thread): conn, addr = self.s.accept() logging.info('Incoming connection from: {}:{}'.format(addr[0], addr[1])) with shared.connections_lock: - c = Connection(addr[0], addr[1], conn) - c.start() - shared.connections.add(c) + if len(shared.connections) > shared.connection_limit: + conn.close() + else: + c = Connection(addr[0], addr[1], conn) + c.start() + shared.connections.add(c) except socket.timeout: pass diff --git a/src/shared.py b/src/shared.py index a0aadf2..a72a6a3 100644 --- a/src/shared.py +++ b/src/shared.py @@ -42,6 +42,7 @@ node_pool = set() unchecked_node_pool = set() outgoing_connections = 8 +connection_limit = 150 objects = {} objects_lock = threading.Lock()