MemoryStore acquires a usage token from other rates even when acquiring a token has failed
|
# Optimisations: We do not need to update every rate |
|
# that expires only the ones that don't have usage tokens. |
|
for use_dt, rate in zip(record, self.limit.rates): |
|
if use_dt[0] == 0: |
|
if (then := (use_dt[1] + rate.period)) <= now: |
|
# We have no tokens left but the rate has expired |
|
# so we reset it and acquire a token. |
|
use_dt[:] = [rate.uses - 1, now] |
|
elif (retry := then - now) > worst: |
|
# no tokens and the rate has time left to expire. |
|
worst = retry |
|
worst_rate = rate |
|
else: |
|
use_dt[0] -= 1 |
|
if worst is False: |
|
return True, 0.0, None |
|
|
|
return False, worst, worst_rate |
Fix:
- Check if acquiring possible
- Then acquire
MemoryStore acquires a usage token from other rates even when acquiring a token has failed
uprate/uprate/store.py
Lines 145 to 162 in c86dd7d
Fix: