Skip to content

[BUG] Incorrect implementation of MemoryStore #3

@WizzyGeek

Description

@WizzyGeek

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

# 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:

  1. Check if acquiring possible
  2. Then acquire

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions