Merge "sync: Keep a moving average of last fetch times"

This commit is contained in:
Shawn Pearce 2012-10-25 17:35:15 -07:00 committed by Gerrit Code Review
commit 2577cec095

View File

@ -510,7 +510,6 @@ uncommitted changes are present' % project.relpath
to_fetch.append(rp) to_fetch.append(rp)
to_fetch.extend(all_projects) to_fetch.extend(all_projects)
to_fetch.sort(key=self._fetch_times.Get, reverse=True) to_fetch.sort(key=self._fetch_times.Get, reverse=True)
self._fetch_times.Clear()
fetched = self._Fetch(to_fetch, opt) fetched = self._Fetch(to_fetch, opt)
_PostRepoFetch(rp, opt.no_repo_verify) _PostRepoFetch(rp, opt.no_repo_verify)
@ -632,19 +631,24 @@ warning: Cannot automatically authenticate repo."""
return True return True
class _FetchTimes(object): class _FetchTimes(object):
_ALPHA = 0.5
def __init__(self, manifest): def __init__(self, manifest):
self._path = os.path.join(manifest.repodir, '.repopickle_fetchtimes') self._path = os.path.join(manifest.repodir, '.repopickle_fetchtimes')
self._times = None self._times = None
self._seen = set()
def Clear(self):
self._times = {}
def Get(self, project): def Get(self, project):
self._Load() self._Load()
return self._times.get(project.name, _ONE_DAY_S) return self._times.get(project.name, _ONE_DAY_S)
def Set(self, project, t): def Set(self, project, t):
self._times[project.name] = t self._Load()
name = project.name
old = self._times.get(name, t)
self._seen.add(name)
a = self._ALPHA
self._times[name] = (a*t) + ((1-a) * old)
def _Load(self): def _Load(self):
if self._times is None: if self._times is None:
@ -669,6 +673,14 @@ class _FetchTimes(object):
def Save(self): def Save(self):
if self._times is None: if self._times is None:
return return
to_delete = []
for name in self._times:
if name not in self._seen:
to_delete.append(name)
for name in to_delete:
del self._times[name]
try: try:
f = open(self._path, 'wb') f = open(self._path, 'wb')
try: try: