Browse Source

Peer discovery: loop address families too

Works around a Python bug https://bugs.python.org/issue35302
patch-2
Neil Booth 6 years ago
parent
commit
7f1096ae53
  1. 20
      electrumx/lib/peer.py
  2. 4
      electrumx/server/peers.py

20
electrumx/lib/peer.py

@ -25,7 +25,8 @@
'''Representation of a peer server.'''
from ipaddress import ip_address
from ipaddress import ip_address, IPv4Address, IPv6Address
from socket import AF_INET, AF_INET6
from electrumx.lib.util import cachedproperty
import electrumx.lib.util as util
@ -112,15 +113,24 @@ class Peer(object):
for feature in self.FEATURES:
setattr(self, feature, getattr(peer, feature))
def connection_port_pairs(self):
'''Return a list of (kind, port) pairs to try when making a
connection.'''
def connection_tuples(self):
'''Return a list of (kind, port, family) tuples to try when making a
connection.
'''
# Use a list not a set - it's important to try the registered
# ports first.
pairs = [('SSL', self.ssl_port), ('TCP', self.tcp_port)]
while self.other_port_pairs:
pairs.append(self.other_port_pairs.pop())
return [pair for pair in pairs if pair[1]]
if isinstance(self.ip_address, IPv4Address):
families = [AF_INET]
elif isinstance(self.ip_address, IPv6Address):
families = [AF_INET6]
else:
families = [AF_INET, AF_INET6]
return [(kind, port, family)
for kind, port in pairs if port
for family in families]
def mark_bad(self):
'''Mark as bad to avoid reconnects but also to remember for a

4
electrumx/server/peers.py

@ -202,10 +202,10 @@ class PeerManager(object):
async def _should_drop_peer(self, peer):
peer.try_count += 1
is_good = False
for kind, port in peer.connection_port_pairs():
for kind, port, family in peer.connection_tuples():
peer.last_try = time.time()
kwargs = {}
kwargs = {'family': family}
if kind == 'SSL':
kwargs['ssl'] = ssl.SSLContext(ssl.PROTOCOL_TLS)

Loading…
Cancel
Save