Connection pool cleanup

- minor code quality improvement
This commit is contained in:
Peter Šurda 2017-06-24 12:23:16 +02:00
parent 0dc0b22974
commit 916b85c862
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

View File

@ -69,7 +69,7 @@ class BMConnectionPool(object):
def removeConnection(self, connection): def removeConnection(self, connection):
if isinstance(connection, network.udp.UDPSocket): if isinstance(connection, network.udp.UDPSocket):
del self.udpSockets[connection.listening.host] del self.udpSockets[connection.listening.host]
if isinstance(connection, network.tcp.TCPServer): elif isinstance(connection, network.tcp.TCPServer):
del self.listeningSockets[state.Peer(connection.destination.host, connection.destination.port)] del self.listeningSockets[state.Peer(connection.destination.host, connection.destination.port)]
elif connection.isOutbound: elif connection.isOutbound:
try: try:
@ -163,24 +163,26 @@ class BMConnectionPool(object):
self.lastSpawned = time.time() self.lastSpawned = time.time()
if acceptConnections and len(self.listeningSockets) == 0: if acceptConnections:
self.startListening() if not self.listeningSockets:
logger.info('Listening for incoming connections.') self.startListening()
if acceptConnections and len(self.udpSockets) == 0: logger.info('Listening for incoming connections.')
if BMConfigParser().safeGet("network", "bind") is None: if not self.udpSockets:
self.startUDPSocket() if BMConfigParser().safeGet("network", "bind") is None:
else: self.startUDPSocket()
for bind in re.sub("[^\w.]+", " ", BMConfigParser().safeGet("network", "bind")).split(): else:
self.startUDPSocket(bind) for bind in re.sub("[^\w.]+", " ", BMConfigParser().safeGet("network", "bind")).split():
logger.info('Starting UDP socket(s).') self.startUDPSocket(bind)
if len(self.listeningSockets) > 0 and not acceptConnections: logger.info('Starting UDP socket(s).')
for i in self.listeningSockets: else:
i.handle_close() if self.listeningSockets:
logger.info('Stopped listening for incoming connections.') for i in self.listeningSockets:
if len(self.udpSockets) > 0 and not acceptConnections: i.handle_close()
for i in self.udpSockets: logger.info('Stopped listening for incoming connections.')
i.handle_close() if self.udpSockets:
logger.info('Stopped udp sockets.') for i in self.udpSockets:
i.handle_close()
logger.info('Stopped udp sockets.')
# while len(asyncore.socket_map) > 0 and state.shutdown == 0: # while len(asyncore.socket_map) > 0 and state.shutdown == 0:
# print "loop, state = %s" % (proxy.state) # print "loop, state = %s" % (proxy.state)