Reduced helper_randon dependency from network module #2252

Merged
anand-skss merged 1 commits from test3 into v0.6 2024-06-21 10:02:46 +02:00
6 changed files with 18 additions and 19 deletions
Showing only changes of commit e578759a3f - Show all commits

View File

@ -1,11 +1,11 @@
""" """
Announce addresses as they are received from other hosts Announce addresses as they are received from other hosts
""" """
import random
from six.moves import queue from six.moves import queue
# magic imports! # magic imports!
import connectionpool import connectionpool
from helper_random import randomshuffle
from protocol import assembleAddrMessage from protocol import assembleAddrMessage
from queues import addrQueue # FIXME: init with queue from queues import addrQueue # FIXME: init with queue
@ -29,9 +29,9 @@ class AddrThread(StoppableThread):
if chunk: if chunk:
# Choose peers randomly # Choose peers randomly
connections = connectionpool.pool.establishedConnections() connections = connectionpool.pool.establishedConnections()
randomshuffle(connections) random.shuffle(connections)
for i in connections: for i in connections:
randomshuffle(chunk) random.shuffle(chunk)
filtered = [] filtered = []
for stream, peer, seen, destination in chunk: for stream, peer, seen, destination in chunk:
# peer's own address or address received from peer # peer's own address or address received from peer

View File

@ -9,6 +9,7 @@ Basic infrastructure for asynchronous socket service clients and servers.
import os import os
import select import select
import socket import socket
import random
import sys import sys
import time import time
import warnings import warnings
@ -19,7 +20,6 @@ from errno import (
) )
from threading import current_thread from threading import current_thread
import helper_random
try: try:
from errno import WSAEWOULDBLOCK from errno import WSAEWOULDBLOCK
@ -233,13 +233,13 @@ def select_poller(timeout=0.0, map=None):
if err.args[0] in (WSAENOTSOCK, ): if err.args[0] in (WSAENOTSOCK, ):
return return
for fd in helper_random.randomsample(r, len(r)): for fd in random.sample(r, len(r)):
obj = map.get(fd) obj = map.get(fd)
if obj is None: if obj is None:
continue continue
read(obj) read(obj)
for fd in helper_random.randomsample(w, len(w)): for fd in random.sample(w, len(w)):
obj = map.get(fd) obj = map.get(fd)
if obj is None: if obj is None:
continue continue
@ -297,7 +297,7 @@ def poll_poller(timeout=0.0, map=None):
except socket.error as err: except socket.error as err:
if err.args[0] in (EBADF, WSAENOTSOCK, EINTR): if err.args[0] in (EBADF, WSAENOTSOCK, EINTR):
return return
for fd, flags in helper_random.randomsample(r, len(r)): for fd, flags in random.sample(r, len(r)):
obj = map.get(fd) obj = map.get(fd)
if obj is None: if obj is None:
continue continue
@ -357,7 +357,7 @@ def epoll_poller(timeout=0.0, map=None):
if err.args[0] != EINTR: if err.args[0] != EINTR:
raise raise
r = [] r = []
for fd, flags in helper_random.randomsample(r, len(r)): for fd, flags in random.sample(r, len(r)):
obj = map.get(fd) obj = map.get(fd)
if obj is None: if obj is None:
continue continue
@ -420,7 +420,7 @@ def kqueue_poller(timeout=0.0, map=None):
events = kqueue_poller.pollster.control(updates, selectables, timeout) events = kqueue_poller.pollster.control(updates, selectables, timeout)
if len(events) > 1: if len(events) > 1:
events = helper_random.randomsample(events, len(events)) events = random.sample(events, len(events))
for event in events: for event in events:
fd = event.ident fd = event.ident

View File

@ -7,9 +7,9 @@ import re
import socket import socket
import sys import sys
import time import time
import random
import asyncore_pollchoose as asyncore import asyncore_pollchoose as asyncore
import helper_random
import knownnodes import knownnodes
import protocol import protocol
import state import state
@ -210,7 +210,7 @@ class BMConnectionPool(object):
connection_base = TCPConnection connection_base = TCPConnection
elif proxy_type == 'SOCKS5': elif proxy_type == 'SOCKS5':
connection_base = Socks5BMConnection connection_base = Socks5BMConnection
hostname = helper_random.randomchoice([ hostname = random.choice([ # nosec B311
'quzwelsuziwqgpt2.onion', None 'quzwelsuziwqgpt2.onion', None
]) ])
elif proxy_type == 'SOCKS4a': elif proxy_type == 'SOCKS4a':
@ -222,7 +222,7 @@ class BMConnectionPool(object):
bootstrapper = bootstrap(connection_base) bootstrapper = bootstrap(connection_base)
if not hostname: if not hostname:
port = helper_random.randomchoice([8080, 8444]) port = random.choice([8080, 8444]) # nosec B311
hostname = 'bootstrap%s.bitmessage.org' % port hostname = 'bootstrap%s.bitmessage.org' % port
else: else:
port = 8444 port = 8444
@ -289,7 +289,7 @@ class BMConnectionPool(object):
state.maximumNumberOfHalfOpenConnections - pending): state.maximumNumberOfHalfOpenConnections - pending):
try: try:
chosen = self.trustedPeer or chooseConnection( chosen = self.trustedPeer or chooseConnection(
helper_random.randomchoice(self.streams)) random.choice(self.streams)) # nosec B311
except ValueError: except ValueError:
continue continue
if chosen in self.outboundConnections: if chosen in self.outboundConnections:

View File

@ -2,9 +2,9 @@
`DownloadThread` class definition `DownloadThread` class definition
""" """
import time import time
import random
import state import state
import addresses import addresses
import helper_random
import protocol import protocol
import connectionpool import connectionpool
from network import dandelion_ins from network import dandelion_ins
@ -43,7 +43,7 @@ class DownloadThread(StoppableThread):
requested = 0 requested = 0
# Choose downloading peers randomly # Choose downloading peers randomly
connections = connectionpool.pool.establishedConnections() connections = connectionpool.pool.establishedConnections()
helper_random.randomshuffle(connections) random.shuffle(connections)
requestChunk = max(int( requestChunk = max(int(
min(self.maxRequestChunk, len(missingObjects)) min(self.maxRequestChunk, len(missingObjects))
/ len(connections)), 1) if connections else 1 / len(connections)), 1) if connections else 1

View File

@ -11,7 +11,6 @@ import time
# magic imports! # magic imports!
import addresses import addresses
import helper_random
import l10n import l10n
import protocol import protocol
import state import state
@ -201,7 +200,7 @@ class TCPConnection(BMProto, TLSDispatcher):
elemCount = min( elemCount = min(
len(filtered), len(filtered),
maxAddrCount / 2 if n else maxAddrCount) maxAddrCount / 2 if n else maxAddrCount)
addrs[s] = helper_random.randomsample(filtered, elemCount) addrs[s] = random.sample(filtered, elemCount)
for substream in addrs: for substream in addrs:
for peer, params in addrs[substream]: for peer, params in addrs[substream]:
templist.append((substream, peer, params["lastseen"])) templist.append((substream, peer, params["lastseen"]))

View File

@ -3,7 +3,7 @@
""" """
import time import time
import helper_random import random
import protocol import protocol
import state import state
import connectionpool import connectionpool
@ -24,7 +24,7 @@ class UploadThread(StoppableThread):
uploaded = 0 uploaded = 0
# Choose uploading peers randomly # Choose uploading peers randomly
connections = connectionpool.pool.establishedConnections() connections = connectionpool.pool.establishedConnections()
helper_random.randomshuffle(connections) random.shuffle(connections)
for i in connections: for i in connections:
now = time.time() now = time.time()
# avoid unnecessary delay # avoid unnecessary delay