Skip to content

Commit 28355d7

Browse files
committed
Made BMConnectionPool as global runtime variable in connectionpool from singleton
1 parent 8fa9da4 commit 28355d7

20 files changed

+66
-68
lines changed

src/api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@
9696
from highlevelcrypto import calculateInventoryHash
9797

9898
try:
99-
from network import BMConnectionPool
99+
from network import connectionpool
100100
except ImportError:
101-
BMConnectionPool = None
101+
connectionpool = None
102102

103103
from network import stats, StoppableThread
104104
from version import softwareVersion
@@ -1475,18 +1475,18 @@ def HandleListConnections(self):
14751475
Returns bitmessage connection information as dict with keys *inbound*,
14761476
*outbound*.
14771477
"""
1478-
if BMConnectionPool is None:
1478+
if connectionpool is None:
14791479
raise APIError(21, 'Could not import BMConnectionPool.')
14801480
inboundConnections = []
14811481
outboundConnections = []
1482-
for i in BMConnectionPool().inboundConnections.values():
1482+
for i in connectionpool.pool.inboundConnections.values():
14831483
inboundConnections.append({
14841484
'host': i.destination.host,
14851485
'port': i.destination.port,
14861486
'fullyEstablished': i.fullyEstablished,
14871487
'userAgent': str(i.userAgent)
14881488
})
1489-
for i in BMConnectionPool().outboundConnections.values():
1489+
for i in connectionpool.pool.outboundConnections.values():
14901490
outboundConnections.append({
14911491
'host': i.destination.host,
14921492
'port': i.destination.port,

src/bitmessageqt/networkstatus.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import network.stats
1111
import state
1212
import widgets
13-
from network import BMConnectionPool, knownnodes
13+
from network import connectionpool, knownnodes
1414
from retranslateui import RetranslateMixin
1515
from tr import _translate
1616
from uisignaler import UISignaler
@@ -148,16 +148,16 @@ def updateNetworkStatusTab(self, outbound, add, destination):
148148
# pylint: disable=too-many-branches,undefined-variable
149149
if outbound:
150150
try:
151-
c = BMConnectionPool().outboundConnections[destination]
151+
c = connectionpool.pool.outboundConnections[destination]
152152
except KeyError:
153153
if add:
154154
return
155155
else:
156156
try:
157-
c = BMConnectionPool().inboundConnections[destination]
157+
c = connectionpool.pool.inboundConnections[destination]
158158
except KeyError:
159159
try:
160-
c = BMConnectionPool().inboundConnections[destination.host]
160+
c = connectionpool.pool.inboundConnections[destination.host]
161161
except KeyError:
162162
if add:
163163
return
@@ -201,7 +201,7 @@ def updateNetworkStatusTab(self, outbound, add, destination):
201201
self.tableWidgetConnectionCount.item(0, 0).setData(QtCore.Qt.UserRole, destination)
202202
self.tableWidgetConnectionCount.item(0, 1).setData(QtCore.Qt.UserRole, outbound)
203203
else:
204-
if not BMConnectionPool().inboundConnections:
204+
if not connectionpool.pool.inboundConnections:
205205
self.window().setStatusIcon('yellow')
206206
for i in range(self.tableWidgetConnectionCount.rowCount()):
207207
if self.tableWidgetConnectionCount.item(i, 0).data(QtCore.Qt.UserRole).toPyObject() != destination:

src/bitmessageqt/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
from bmconfigparser import config as config_obj
2121
from helper_sql import sqlExecute, sqlStoredProcedure
2222
from helper_startup import start_proxyconfig
23-
from network import knownnodes, AnnounceThread
23+
from network import knownnodes
24+
from network.announcethread import AnnounceThread
2425
from network.asyncore_pollchoose import set_rates
2526
from tr import _translate
2627

src/class_singleCleaner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import state
2828
from bmconfigparser import config
2929
from helper_sql import sqlExecute, sqlQuery
30-
from network import BMConnectionPool, knownnodes, StoppableThread
30+
from network import connectionpool, knownnodes, StoppableThread
3131
from tr import _translate
3232

3333

@@ -129,7 +129,7 @@ def run(self): # pylint: disable=too-many-branches
129129
os._exit(1) # pylint: disable=protected-access
130130

131131
# inv/object tracking
132-
for connection in BMConnectionPool().connections():
132+
for connection in connectionpool.pool.connections():
133133
connection.clean()
134134

135135
# discovery tracking

src/network/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22
Network subsystem package
33
"""
44

5-
try:
6-
from .announcethread import AnnounceThread
7-
from .connectionpool import BMConnectionPool
8-
except ImportError:
9-
AnnounceThread = None
10-
BMConnectionPool = None
115
from .threads import StoppableThread
126

137

14-
__all__ = ["AnnounceThread", "BMConnectionPool", "StoppableThread"]
8+
__all__ = ["StoppableThread"]
159

1610

1711
def start(config, state):
1812
"""Start network threads"""
1913
import state
14+
from .announcethread import AnnounceThread
15+
import connectionpool # pylint: disable=relative-import
2016
from .addrthread import AddrThread
2117
from .dandelion import Dandelion
2218
from .downloadthread import DownloadThread
@@ -29,7 +25,7 @@ def start(config, state):
2925
readKnownNodes()
3026
# init, needs to be early because other thread may access it early
3127
state.Dandelion = Dandelion()
32-
BMConnectionPool().connectToStream(1)
28+
connectionpool.pool.connectToStream(1)
3329
for thread in (
3430
BMNetworkThread(), InvThread(), AddrThread(),
3531
DownloadThread(), UploadThread()

src/network/addrthread.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
# magic imports!
77
import state
8+
import connectionpool
89
from helper_random import randomshuffle
910
from protocol import assembleAddrMessage
1011
from queues import addrQueue # FIXME: init with queue
11-
from network.connectionpool import BMConnectionPool
1212

1313
from threads import StoppableThread
1414

@@ -29,7 +29,7 @@ def run(self):
2929

3030
if chunk:
3131
# Choose peers randomly
32-
connections = BMConnectionPool().establishedConnections()
32+
connections = connectionpool.pool.establishedConnections()
3333
randomshuffle(connections)
3434
for i in connections:
3535
randomshuffle(chunk)

src/network/announcethread.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
# magic imports!
77
import state
8+
import connectionpool
89
from bmconfigparser import config
910
from protocol import assembleAddrMessage
10-
from network.connectionpool import BMConnectionPool
1111

1212
from node import Peer
1313
from threads import StoppableThread
@@ -31,7 +31,7 @@ def run(self):
3131
@staticmethod
3232
def announceSelf():
3333
"""Announce our presence"""
34-
for connection in BMConnectionPool().udpSockets.values():
34+
for connection in connectionpool.pool.udpSockets.values():
3535
if not connection.announcing:
3636
continue
3737
for stream in state.streamsInWhichIAmParticipating:

src/network/bmproto.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
# magic imports!
1414
import addresses
15-
import connectionpool
1615
import knownnodes
1716
import protocol
1817
import state
18+
import connectionpool
1919
from bmconfigparser import config
2020
from queues import invQueue, objectProcessorQueue, portCheckerQueue
2121
from randomtrackingdict import RandomTrackingDict
@@ -540,7 +540,7 @@ def bm_command_version(self):
540540
if not self.isOutbound:
541541
self.append_write_buf(protocol.assembleVersionMessage(
542542
self.destination.host, self.destination.port,
543-
connectionpool.BMConnectionPool().streams, True,
543+
connectionpool.pool.streams, True,
544544
nodeid=self.nodeid))
545545
logger.debug(
546546
'%(host)s:%(port)i sending version',
@@ -596,7 +596,7 @@ def peerValidityChecks(self):
596596
'Closed connection to %s because there is no overlapping'
597597
' interest in streams.', self.destination)
598598
return False
599-
if connectionpool.BMConnectionPool().inboundConnections.get(
599+
if connectionpool.pool.inboundConnections.get(
600600
self.destination):
601601
try:
602602
if not protocol.checkSocksIP(self.destination.host):
@@ -614,8 +614,8 @@ def peerValidityChecks(self):
614614
# or server full report the same error to counter deanonymisation
615615
if (
616616
Peer(self.destination.host, self.peerNode.port)
617-
in connectionpool.BMConnectionPool().inboundConnections
618-
or len(connectionpool.BMConnectionPool())
617+
in connectionpool.pool.inboundConnections
618+
or len(connectionpool.pool)
619619
> config.safeGetInt(
620620
'bitmessagesettings', 'maxtotalconnections')
621621
+ config.safeGetInt(
@@ -627,7 +627,7 @@ def peerValidityChecks(self):
627627
'Closed connection to %s due to server full'
628628
' or duplicate inbound/outbound.', self.destination)
629629
return False
630-
if connectionpool.BMConnectionPool().isAlreadyConnected(self.nonce):
630+
if connectionpool.pool.isAlreadyConnected(self.nonce):
631631
self.append_write_buf(protocol.assembleErrorMessage(
632632
errorText="I'm connected to myself. Closing connection.",
633633
fatal=2))
@@ -641,7 +641,7 @@ def peerValidityChecks(self):
641641
@staticmethod
642642
def stopDownloadingObject(hashId, forwardAnyway=False):
643643
"""Stop downloading object *hashId*"""
644-
for connection in connectionpool.BMConnectionPool().connections():
644+
for connection in connectionpool.pool.connections():
645645
try:
646646
del connection.objectsNewToMe[hashId]
647647
except KeyError:

src/network/connectionpool.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from connectionchooser import chooseConnection
1818
from node import Peer
1919
from proxy import Proxy
20-
from singleton import Singleton
2120
from tcp import (
2221
bootstrap, Socks4aBMConnection, Socks5BMConnection,
2322
TCPConnection, TCPServer)
@@ -26,7 +25,6 @@
2625
logger = logging.getLogger('default')
2726

2827

29-
@Singleton
3028
class BMConnectionPool(object):
3129
"""Pool of all existing connections"""
3230
# pylint: disable=too-many-instance-attributes
@@ -403,3 +401,6 @@ def loop(self): # pylint: disable=too-many-branches,too-many-statements
403401
pass
404402
for i in reaper:
405403
self.removeConnection(i)
404+
405+
406+
pool = BMConnectionPool()

src/network/downloadthread.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import addresses
77
import helper_random
88
import protocol
9-
from network.connectionpool import BMConnectionPool
9+
import connectionpool
1010
from objectracker import missingObjects
1111
from threads import StoppableThread
1212

@@ -41,7 +41,7 @@ def run(self):
4141
while not self._stopped:
4242
requested = 0
4343
# Choose downloading peers randomly
44-
connections = BMConnectionPool().establishedConnections()
44+
connections = connectionpool.pool.establishedConnections()
4545
helper_random.randomshuffle(connections)
4646
requestChunk = max(int(
4747
min(self.maxRequestChunk, len(missingObjects))

0 commit comments

Comments
 (0)