Browse Source

Re-use the same IP if server still has active threads

To avoid problems when re-connecting after for example a timeout. 

Closes #733
pull/687/head
Safihre 9 years ago
parent
commit
a99a51e0d4
  1. 30
      sabnzbd/downloader.py

30
sabnzbd/downloader.py

@ -98,26 +98,40 @@ class Server(object):
@property
def hostip(self):
""" based on value of load_balancing() and self.info:
0: return the host name itself (so: do nothing)
1 and self.info has more than 1 entry (read: IP address): Return a random entry from the possible IPs
2 and self.info has more than 1 entry (read: IP address): Return the quickest IP based on the happyeyeballs algorithm
""" In case a server still has active connections, we use the same IP again
If new connection then based on value of load_balancing() and self.info:
0 - return the first entry, so all threads use the same IP
1 - and self.info has more than 1 entry (read: IP address): Return a random entry from the possible IPs
2 - and self.info has more than 1 entry (read: IP address): Return the quickest IP based on the happyeyeballs algorithm
In case of problems: return the host name itself
"""
if cfg.load_balancing() == 1 and self.info and len(self.info) > 1:
# Check if already a succesfull ongoing connection
if self.busy_threads and self.busy_threads[0].nntp:
# Re-use that IP
logging.debug('%s: Re-using address %s', self.host, self.busy_threads[0].nntp.host)
return self.busy_threads[0].nntp.host
# Determine new IP
if cfg.load_balancing() == 0 and self.info:
# Just return the first one, so all next threads use the same IP
ip = self.info[0][4][0]
logging.debug('%s: Connecting to address %s', self.host, ip)
elif cfg.load_balancing() == 1 and self.info and len(self.info) > 1:
# Return a random entry from the possible IPs
rnd = random.randint(0, len(self.info) - 1)
ip = self.info[rnd][4][0]
logging.debug('For server %s, using IP %s', self.host, ip)
logging.debug('%s: Connecting to address %s', self.host, ip)
elif cfg.load_balancing() == 2 and self.info and len(self.info) > 1:
# RFC6555 / Happy Eyeballs:
ip = happyeyeballs(self.host, port=self.port, ssl=self.ssl)
if ip:
logging.debug('For server %s, using IP %s as server', self.host, ip)
logging.debug('%s: Connecting to address %s', self.host, ip)
else:
# nothing returned, so there was a connection problem
ip = self.host
logging.debug('For server %s, no successful IP connection possible', self.host)
logging.debug('%s: No successful IP connection was possible', self.host)
else:
ip = self.host
return ip

Loading…
Cancel
Save