Skip to content

Commit

Permalink
fix: use a monotonic clock in memory storage
Browse files Browse the repository at this point in the history
That should be more robust against system time update, e.g.
ntp or leap seconds.
  • Loading branch information
xrmx committed Mar 21, 2021
1 parent 61f4e63 commit e5e87e1
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions token_bucket/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def replenish(self, key, rate, capacity):

tokens_in_bucket, last_replenished_at = self._buckets[key]

now = time.time()
now = time.monotonic()

# NOTE(kgriffs): This will detect many, but not all,
# manifestations of the race condition. If a later
Expand All @@ -123,7 +123,7 @@ def replenish(self, key, rate, capacity):
]

except KeyError:
self._buckets[key] = [capacity, time.time()]
self._buckets[key] = [capacity, time.monotonic()]

def consume(self, key, num_tokens):
"""Attempt to take one or more tokens from a bucket.
Expand Down

0 comments on commit e5e87e1

Please sign in to comment.