Peer discovery: loop address families too
Works around a Python bug https://bugs.python.org/issue35302
This commit is contained in:
parent
c3039db50c
commit
7f1096ae53
@ -25,7 +25,8 @@
|
|||||||
|
|
||||||
'''Representation of a peer server.'''
|
'''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
|
from electrumx.lib.util import cachedproperty
|
||||||
import electrumx.lib.util as util
|
import electrumx.lib.util as util
|
||||||
@ -112,15 +113,24 @@ class Peer(object):
|
|||||||
for feature in self.FEATURES:
|
for feature in self.FEATURES:
|
||||||
setattr(self, feature, getattr(peer, feature))
|
setattr(self, feature, getattr(peer, feature))
|
||||||
|
|
||||||
def connection_port_pairs(self):
|
def connection_tuples(self):
|
||||||
'''Return a list of (kind, port) pairs to try when making a
|
'''Return a list of (kind, port, family) tuples to try when making a
|
||||||
connection.'''
|
connection.
|
||||||
|
'''
|
||||||
# Use a list not a set - it's important to try the registered
|
# Use a list not a set - it's important to try the registered
|
||||||
# ports first.
|
# ports first.
|
||||||
pairs = [('SSL', self.ssl_port), ('TCP', self.tcp_port)]
|
pairs = [('SSL', self.ssl_port), ('TCP', self.tcp_port)]
|
||||||
while self.other_port_pairs:
|
while self.other_port_pairs:
|
||||||
pairs.append(self.other_port_pairs.pop())
|
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):
|
def mark_bad(self):
|
||||||
'''Mark as bad to avoid reconnects but also to remember for a
|
'''Mark as bad to avoid reconnects but also to remember for a
|
||||||
|
|||||||
@ -202,10 +202,10 @@ class PeerManager(object):
|
|||||||
async def _should_drop_peer(self, peer):
|
async def _should_drop_peer(self, peer):
|
||||||
peer.try_count += 1
|
peer.try_count += 1
|
||||||
is_good = False
|
is_good = False
|
||||||
for kind, port in peer.connection_port_pairs():
|
for kind, port, family in peer.connection_tuples():
|
||||||
peer.last_try = time.time()
|
peer.last_try = time.time()
|
||||||
|
|
||||||
kwargs = {}
|
kwargs = {'family': family}
|
||||||
if kind == 'SSL':
|
if kind == 'SSL':
|
||||||
kwargs['ssl'] = ssl.SSLContext(ssl.PROTOCOL_TLS)
|
kwargs['ssl'] = ssl.SSLContext(ssl.PROTOCOL_TLS)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user