/usr/share/pyshared/allmydata/util/limiter.py is in tahoe-lafs 1.9.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | from twisted.internet import defer
from foolscap.api import eventually
class ConcurrencyLimiter:
"""I implement a basic concurrency limiter. Add work to it in the form of
(callable, args, kwargs) tuples. No more than LIMIT callables will be
outstanding at any one time.
"""
def __init__(self, limit=10):
self.limit = limit
self.pending = []
self.active = 0
def __repr__(self):
return "<Limiter with %d/%d/%d>" % (self.active, len(self.pending),
self.limit)
def add(self, cb, *args, **kwargs):
d = defer.Deferred()
task = (cb, args, kwargs, d)
self.pending.append(task)
self.maybe_start_task()
return d
def maybe_start_task(self):
if self.active >= self.limit:
return
if not self.pending:
return
(cb, args, kwargs, done_d) = self.pending.pop(0)
self.active += 1
d = defer.maybeDeferred(cb, *args, **kwargs)
d.addBoth(self._done, done_d)
def _done(self, res, done_d):
self.active -= 1
eventually(done_d.callback, res)
eventually(self.maybe_start_task)
|