Skip to content

Commit

Permalink
rewrite iplist algo
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Oct 30, 2014
1 parent 5491456 commit fe67fb9
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions local/proxylib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1961,24 +1961,19 @@ def reorg_ipaddrs():
sock = None
for i in range(kwargs.get('max_retry', 4)):
reorg_ipaddrs()
window = self.max_window + i
if len(self.ssl_connection_good_ipaddrs) > len(self.ssl_connection_bad_ipaddrs):
window = max(2, window-2)
if len(self.ssl_connection_bad_ipaddrs)/2 >= len(self.ssl_connection_good_ipaddrs) <= 1.5 * window:
window += 2
good_ipaddrs = [x for x in addresses if x in self.ssl_connection_good_ipaddrs]
good_ipaddrs = sorted(good_ipaddrs, key=self.ssl_connection_time.get)[:window]
good_ipaddrs = sorted([x for x in addresses if x in self.ssl_connection_good_ipaddrs], key=self.ssl_connection_time.get)
bad_ipaddrs = sorted([x for x in addresses if x in self.ssl_connection_bad_ipaddrs], key=self.ssl_connection_bad_ipaddrs.get)
unknown_ipaddrs = [x for x in addresses if x not in self.ssl_connection_good_ipaddrs and x not in self.ssl_connection_bad_ipaddrs]
random.shuffle(unknown_ipaddrs)
unknown_ipaddrs = unknown_ipaddrs[:window]
bad_ipaddrs = [x for x in addresses if x in self.ssl_connection_bad_ipaddrs]
bad_ipaddrs = sorted(bad_ipaddrs, key=self.ssl_connection_bad_ipaddrs.get)[:window]
addrs = good_ipaddrs + unknown_ipaddrs + bad_ipaddrs
remain_window = 3 * window - len(addrs)
if 0 < remain_window <= len(addresses):
addrs += random.sample(addresses, remain_window)
if len(good_ipaddrs) > 2 * window or len(bad_ipaddrs) < 0.5 * len(good_ipaddrs):
addrs = addrs[:2*window]
window = self.max_window + i
if len(bad_ipaddrs) < 0.2 * len(good_ipaddrs) and len(good_ipaddrs) > 10:
addrs = good_ipaddrs[:window]
addrs += [random.choice(unknown_ipaddrs)] if unknown_ipaddrs else []
elif len(good_ipaddrs) > 2 * window or len(bad_ipaddrs) < 0.5 * len(good_ipaddrs):
addrs = (good_ipaddrs[:window] + unknown_ipaddrs + bad_ipaddrs)[:2*window]
else:
addrs = good_ipaddrs[:window] + unknown_ipaddrs[:window] + bad_ipaddrs[:window]
addrs += random.sample(addresses, min(len(addresses), 3*window-len(addrs))) if len(addrs) < 3*window else []
logging.debug('%s good_ipaddrs=%d, unknown_ipaddrs=%r, bad_ipaddrs=%r', cache_key, len(good_ipaddrs), len(unknown_ipaddrs), len(bad_ipaddrs))
queobj = Queue.Queue()
for addr in addrs:
Expand Down

0 comments on commit fe67fb9

Please sign in to comment.