Reduced helper_randon dependency from network module

This commit is contained in:
anand k 2024-06-13 07:03:36 +05:30
parent 205e25337f
commit e578759a3f
No known key found for this signature in database
GPG Key ID: 515AC24FA525DDE0
6 changed files with 18 additions and 19 deletions

View File

@ -1,11 +1,11 @@
"""
Announce addresses as they are received from other hosts
"""
import random
from six.moves import queue
# magic imports!
import connectionpool
from helper_random import randomshuffle
from protocol import assembleAddrMessage
from queues import addrQueue # FIXME: init with queue
@ -29,9 +29,9 @@ class AddrThread(StoppableThread):
if chunk:
# Choose peers randomly
connections = connectionpool.pool.establishedConnections()
randomshuffle(connections)
random.shuffle(connections)
for i in connections:
randomshuffle(chunk)
random.shuffle(chunk)
filtered = []
for stream, peer, seen, destination in chunk:
# 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 select
import socket
import random
import sys
import time
import warnings
@ -19,7 +20,6 @@ from errno import (
)
from threading import current_thread
import helper_random
try:
from errno import WSAEWOULDBLOCK
@ -233,13 +233,13 @@ def select_poller(timeout=0.0, map=None):
if err.args[0] in (WSAENOTSOCK, ):
return
for fd in helper_random.randomsample(r, len(r)):
for fd in random.sample(r, len(r)):
obj = map.get(fd)
if obj is None:
continue
read(obj)
for fd in helper_random.randomsample(w, len(w)):
for fd in random.sample(w, len(w)):
obj = map.get(fd)
if obj is None:
continue
@ -297,7 +297,7 @@ def poll_poller(timeout=0.0, map=None):
except socket.error as err:
if err.args[0] in (EBADF, WSAENOTSOCK, EINTR):
return
for fd, flags in helper_random.randomsample(r, len(r)):
for fd, flags in random.sample(r, len(r)):
obj = map.get(fd)
if obj is None:
continue
@ -357,7 +357,7 @@ def epoll_poller(timeout=0.0, map=None):
if err.args[0] != EINTR:
raise
r = []
for fd, flags in helper_random.randomsample(r, len(r)):
for fd, flags in random.sample(r, len(r)):
obj = map.get(fd)
if obj is None:
continue
@ -420,7 +420,7 @@ def kqueue_poller(timeout=0.0, map=None):
events = kqueue_poller.pollster.control(updates, selectables, timeout)
if len(events) > 1:
events = helper_random.randomsample(events, len(events))
events = random.sample(events, len(events))
for event in events:
fd = event.ident

View File

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

View File

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

View File

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

View File

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