mirror of
https://github.com/logos-messaging/nim-chat-sdk.git
synced 2026-05-03 08:43:32 +00:00
fix: pr comments
This commit is contained in:
parent
9ae939901f
commit
109b5769da
@ -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(
|
||||
@ -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
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import testutils/unittests
|
||||
import ../ratelimit/rate_limit_manager
|
||||
import ../ratelimit/ratelimit_manager
|
||||
import ../ratelimit/store/memory
|
||||
import chronos
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user