Limit number of connections

This commit is contained in:
TheKysek 2017-01-21 14:05:13 +01:00
parent 0c5c58e126
commit 1146100168
2 changed files with 7 additions and 3 deletions

View File

@ -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

View File

@ -42,6 +42,7 @@ node_pool = set()
unchecked_node_pool = set()
outgoing_connections = 8
connection_limit = 150
objects = {}
objects_lock = threading.Lock()