Browse Source

Reduce bandwith usage over the bandwith interval

So if e.g. your limit is 10MB per hour, then every minute
your cumulative usage will be reduced by 1/6MB.
master
Neil Booth 8 years ago
parent
commit
ab9d9f7c07
  1. 10
      lib/jsonrpc.py

10
lib/jsonrpc.py

@ -129,10 +129,12 @@ class JSONRPC(asyncio.Protocol, LoggedClass):
def using_bandwidth(self, amount):
now = time.time()
if now >= self.bandwidth_start + self.bandwidth_interval:
self.bandwidth_start = now
self.bandwidth_used = 0
self.bandwidth_used += amount
# Reduce the recorded usage in proportion to the elapsed time
elapsed = now - self.bandwidth_start
self.bandwidth_start = now
refund = int(elapsed / self.bandwidth_interval * self.bandwidth_limit)
refund = min(refund, self.bandwidth_used)
self.bandwidth_used += amount - refund
def data_received(self, data):
'''Handle incoming data (synchronously).

Loading…
Cancel
Save