fix: pr comments

This commit is contained in:
pablo 2025-08-04 09:04:52 +03:00
parent 9ae939901f
commit 109b5769da
No known key found for this signature in database
GPG Key ID: 78F35FCC60FDC63A
3 changed files with 6 additions and 21 deletions

View File

@ -1,4 +1,5 @@
import std/[times, deques, options]
# TODO: move to waku's, chronos' or a lib tocken_bucket once decided where this will live
import ./token_bucket
# import waku/common/rate_limit/token_bucket
import ./store/store
@ -54,24 +55,6 @@ proc new*[T: Serializable, S: RateLimitStore](
)
discard await store.saveBucketState(current.get())
return RateLimitManager[T, S](
store: store,
bucket: TokenBucket.new(
current.get().budgetCap,
duration,
ReplenishMode.Strict,
current.get().budget,
current.get().lastTimeFull,
),
): Future[RateLimitManager[T, S]] {.async.} =
var current = await store.loadBucketState()
if current.isNone():
# initialize bucket state with full capacity
current = some(
BucketState(budget: capacity, budgetCap: capacity, lastTimeFull: Moment.now())
)
discard await store.saveBucketState(current.get())
return RateLimitManager[T, S](
store: store,
bucket: TokenBucket.new(

View File

@ -109,10 +109,11 @@ proc update(bucket: TokenBucket, currentTime: Moment) =
else:
updateStrict(bucket, currentTime)
## Returns the available capacity of the bucket: (budget, budgetCap)
proc getAvailableCapacity*(
bucket: TokenBucket, currentTime: Moment
bucket: TokenBucket, currentTime: Moment = Moment.now()
): tuple[budget: int, budgetCap: int, lastTimeFull: Moment] =
## Returns the available capacity of the bucket: (budget, budgetCap, lastTimeFull)
if periodElapsed(bucket, currentTime):
case bucket.replenishMode
of ReplenishMode.Strict:
@ -159,6 +160,7 @@ proc new*(
): T =
assert not isZero(fillDuration)
assert budgetCap != 0
assert lastTimeFull <= Moment.now()
let actualBudget = if budget == -1: budgetCap else: budget
assert actualBudget >= 0 and actualBudget <= budgetCap

View File

@ -1,5 +1,5 @@
import testutils/unittests
import ../ratelimit/rate_limit_manager
import ../ratelimit/ratelimit_manager
import ../ratelimit/store/memory
import chronos