From 916b85c862d740b4efe8fb3f73fa4ae0489b81b1 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Sat, 24 Jun 2017 12:23:16 +0200 Subject: [PATCH] Connection pool cleanup - minor code quality improvement --- src/network/connectionpool.py | 40 ++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index 67778b46..ae76c318 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -69,7 +69,7 @@ class BMConnectionPool(object): def removeConnection(self, connection): if isinstance(connection, network.udp.UDPSocket): 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)] elif connection.isOutbound: try: @@ -163,24 +163,26 @@ class BMConnectionPool(object): self.lastSpawned = time.time() - if acceptConnections and len(self.listeningSockets) == 0: - self.startListening() - logger.info('Listening for incoming connections.') - if acceptConnections and len(self.udpSockets) == 0: - if BMConfigParser().safeGet("network", "bind") is None: - self.startUDPSocket() - else: - for bind in re.sub("[^\w.]+", " ", BMConfigParser().safeGet("network", "bind")).split(): - self.startUDPSocket(bind) - logger.info('Starting UDP socket(s).') - if len(self.listeningSockets) > 0 and not acceptConnections: - for i in self.listeningSockets: - i.handle_close() - logger.info('Stopped listening for incoming connections.') - if len(self.udpSockets) > 0 and not acceptConnections: - for i in self.udpSockets: - i.handle_close() - logger.info('Stopped udp sockets.') + if acceptConnections: + if not self.listeningSockets: + self.startListening() + logger.info('Listening for incoming connections.') + if not self.udpSockets: + if BMConfigParser().safeGet("network", "bind") is None: + self.startUDPSocket() + else: + for bind in re.sub("[^\w.]+", " ", BMConfigParser().safeGet("network", "bind")).split(): + self.startUDPSocket(bind) + logger.info('Starting UDP socket(s).') + else: + if self.listeningSockets: + for i in self.listeningSockets: + i.handle_close() + logger.info('Stopped listening for incoming connections.') + if self.udpSockets: + for i in self.udpSockets: + i.handle_close() + logger.info('Stopped udp sockets.') # while len(asyncore.socket_map) > 0 and state.shutdown == 0: # print "loop, state = %s" % (proxy.state)