diff options
Diffstat (limited to 'module/network/Bucket.py')
-rw-r--r-- | module/network/Bucket.py | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/module/network/Bucket.py b/module/network/Bucket.py index 434cbe662..dc1280ede 100644 --- a/module/network/Bucket.py +++ b/module/network/Bucket.py @@ -14,40 +14,44 @@ You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>. - @author: mkaay + @author: RaNaN """ -from time import time, sleep +from time import time from threading import Lock class Bucket: def __init__(self): - self.content = 0 self.rate = 0 - self.lastDrip = time() + self.tokens = 0 + self.timestamp = time() self.lock = Lock() def setRate(self, rate): self.lock.acquire() self.rate = rate self.lock.release() - - def add(self, amount): + + def consumed(self, amount): + """ return time the process have to sleep, after consumed specified amount """ self.lock.acquire() - self.drip() - allowable = min(amount, self.rate - self.content) - if allowable > 0: - sleep(0.005) - self.content += allowable + self.calc_tokens() + self.tokens -= amount + + if self.tokens < 0: + time = -self.tokens/float(self.rate) + else: + time = 0 + + self.lock.release() - return allowable + return time - def drip(self): - if self.rate: + def calc_tokens(self): + if self.tokens < self.rate: now = time() - deltaT = now - self.lastDrip - self.content = long(max(0, self.content - deltaT * self.rate)) - self.lastDrip = now - else: - self.content = 0 + delta = self.rate * (now - self.timestamp) + self.tokens = min(self.rate, self.tokens + delta) + self.timestamp = now + |