flake8: network.connectionchooser
This commit is contained in:
parent
67d14f9e73
commit
27e74a15d7
|
@ -1,16 +1,16 @@
|
||||||
from queues import Queue
|
import random # nosec
|
||||||
import random
|
|
||||||
|
|
||||||
from bmconfigparser import BMConfigParser
|
|
||||||
import knownnodes
|
import knownnodes
|
||||||
import protocol
|
import protocol
|
||||||
from queues import portCheckerQueue
|
|
||||||
import state
|
import state
|
||||||
import helper_random
|
from bmconfigparser import BMConfigParser
|
||||||
|
from debug import logger
|
||||||
|
from queues import Queue, portCheckerQueue
|
||||||
|
|
||||||
|
|
||||||
def getDiscoveredPeer():
|
def getDiscoveredPeer():
|
||||||
try:
|
try:
|
||||||
peer = helper_random.randomchoice(state.discoveredPeers.keys())
|
peer = random.choice(state.discoveredPeers.keys())
|
||||||
except (IndexError, KeyError):
|
except (IndexError, KeyError):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
try:
|
try:
|
||||||
|
@ -19,8 +19,10 @@ def getDiscoveredPeer():
|
||||||
pass
|
pass
|
||||||
return peer
|
return peer
|
||||||
|
|
||||||
|
|
||||||
def chooseConnection(stream):
|
def chooseConnection(stream):
|
||||||
haveOnion = BMConfigParser().safeGet("bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS'
|
haveOnion = BMConfigParser().safeGet(
|
||||||
|
"bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS'
|
||||||
if state.trustedPeer:
|
if state.trustedPeer:
|
||||||
return state.trustedPeer
|
return state.trustedPeer
|
||||||
try:
|
try:
|
||||||
|
@ -30,15 +32,15 @@ def chooseConnection(stream):
|
||||||
except Queue.Empty:
|
except Queue.Empty:
|
||||||
pass
|
pass
|
||||||
# with a probability of 0.5, connect to a discovered peer
|
# with a probability of 0.5, connect to a discovered peer
|
||||||
if helper_random.randomchoice((False, True)) and not haveOnion:
|
if random.choice((False, True)) and not haveOnion:
|
||||||
# discovered peers are already filtered by allowed streams
|
# discovered peers are already filtered by allowed streams
|
||||||
return getDiscoveredPeer()
|
return getDiscoveredPeer()
|
||||||
for _ in range(50):
|
for _ in range(50):
|
||||||
peer = helper_random.randomchoice(knownnodes.knownNodes[stream].keys())
|
peer = random.choice(knownnodes.knownNodes[stream].keys())
|
||||||
try:
|
try:
|
||||||
rating = knownnodes.knownNodes[stream][peer]["rating"]
|
rating = knownnodes.knownNodes[stream][peer]['rating']
|
||||||
except TypeError:
|
except TypeError:
|
||||||
print "Error in %s" % (peer)
|
logger.warning('Error in %s', peer)
|
||||||
rating = 0
|
rating = 0
|
||||||
if haveOnion:
|
if haveOnion:
|
||||||
# onion addresses have a higher priority when SOCKS
|
# onion addresses have a higher priority when SOCKS
|
||||||
|
@ -52,7 +54,7 @@ def chooseConnection(stream):
|
||||||
if rating > 1:
|
if rating > 1:
|
||||||
rating = 1
|
rating = 1
|
||||||
try:
|
try:
|
||||||
if 0.05/(1.0-rating) > random.random():
|
if 0.05 / (1.0 - rating) > random.random():
|
||||||
return peer
|
return peer
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
return peer
|
return peer
|
||||||
|
|
Loading…
Reference in New Issue
Block a user