Skip to content
Snippets Groups Projects
Commit fa0c5866 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Improve urlgrabber_compat load balancing

parent 088ee574
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python3
#
# Hackish compat library when urlgrabber is missing (MacOS)
#
try:
# Python 3
from urllib.request import urlopen
except ImportError:
# Python 2 fallback
from urllib2 import urlopen
import urllib.parse
import calendar
import time
import random
INFO_FILETIME = object()
......@@ -29,6 +27,7 @@ class CurlWrapper:
def __getattr__(self, what):
return getattr(self.url, what)
pass
class Compat:
......@@ -40,15 +39,23 @@ class Compat:
class MirrorGroup:
def __init__(self, grabber, urls):
self.urls = urls
self.urls = list(urls)
random.shuffle(self.urls)
self.mru = dict([ (u,0) for u in self.urls ])
def urlopen(self, path):
errors = []
for u in self.urls:
for u in reversed(sorted(self.urls, key=lambda u: self.mru[u])):
url = "%s/%s" % (u, path)
try:
return CurlWrapper(urlopen(url))
# print(u, self.mru[u], path)
result = CurlWrapper(urlopen(url))
self.mru[u] += 1
return result
except IOError as e:
# print(e.__class__, e)
# TODO, penalize umreachable hosts?
self.mru[u] /= 2
errors.append((url,e))
pass
raise IOError("Failed to get '%s' (%s)" % (path, errors))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment