mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-05 23:43:07 +00:00
Adapt rate limit tests to the new balanced methods used from chronos/TokenBucket
This commit is contained in:
parent
745e773c2b
commit
90ccf03590
@ -44,8 +44,9 @@ suite "RequestRateLimiter":
|
|||||||
# conn1 reached the 75% of the main bucket over 2 periods of time
|
# conn1 reached the 75% of the main bucket over 2 periods of time
|
||||||
check limiter.checkUsage(proto, conn1, now + 3.minutes) == false
|
check limiter.checkUsage(proto, conn1, now + 3.minutes) == false
|
||||||
|
|
||||||
# conn2 has not used its tokens while we have 1 more tokens left in the main bucket
|
# conn2 has not used its tokens but while we have 0 more tokens left in the main bucket
|
||||||
check limiter.checkUsage(proto, conn2, now + 3.minutes) == true
|
# it will tolerate some burst within period.
|
||||||
|
check limiter.checkUsage(proto, conn2, now + 3.minutes + 50.seconds) == true
|
||||||
|
|
||||||
test "RequestRateLimiter Restrict overusing peer":
|
test "RequestRateLimiter Restrict overusing peer":
|
||||||
# keep limits low for easier calculation of ratios
|
# keep limits low for easier calculation of ratios
|
||||||
@ -55,6 +56,8 @@ suite "RequestRateLimiter":
|
|||||||
# as ratio is 2 in this case but max tokens are main tokens*ratio . 0.75
|
# as ratio is 2 in this case but max tokens are main tokens*ratio . 0.75
|
||||||
# notice meanwhile we have 20 tokens over 2 period (4 mins) in sum
|
# notice meanwhile we have 20 tokens over 2 period (4 mins) in sum
|
||||||
# See: waku/common/rate_limit/request_limiter.nim #func calcPeriodRatio
|
# See: waku/common/rate_limit/request_limiter.nim #func calcPeriodRatio
|
||||||
|
# This tests shows balanced token utilization as being replenished in between periods but never overusing
|
||||||
|
# intended rates.
|
||||||
|
|
||||||
let now = Moment.now()
|
let now = Moment.now()
|
||||||
# with first use we register the peer also and start its timer
|
# with first use we register the peer also and start its timer
|
||||||
@ -64,23 +67,22 @@ suite "RequestRateLimiter":
|
|||||||
# run out of main tokens but still used one more token from the peer's bucket
|
# run out of main tokens but still used one more token from the peer's bucket
|
||||||
check limiter.checkUsage(proto, conn1, now) == false
|
check limiter.checkUsage(proto, conn1, now) == false
|
||||||
|
|
||||||
for i in 0 ..< 4:
|
for i in 0 ..< 10:
|
||||||
check limiter.checkUsage(proto, conn1, now + 3.minutes) == true
|
check limiter.checkUsage(proto, conn1, now + 3.minutes) == true
|
||||||
|
|
||||||
# conn1 reached the 75% of the main bucket over 2 periods of time
|
# conn1 reached the 75% of the main bucket over 2 periods of time
|
||||||
check limiter.checkUsage(proto, conn1, now + 3.minutes) == false
|
check limiter.checkUsage(proto, conn1, now + 3.minutes) == false
|
||||||
|
|
||||||
check limiter.checkUsage(proto, conn2, now + 3.minutes) == true
|
check limiter.checkUsage(proto, conn2, now + 3.minutes + 50.seconds) == true
|
||||||
check limiter.checkUsage(proto, conn2, now + 3.minutes) == true
|
check limiter.checkUsage(proto, conn2, now + 3.minutes + 50.seconds) == true
|
||||||
check limiter.checkUsage(proto, conn3, now + 3.minutes) == true
|
check limiter.checkUsage(proto, conn3, now + 3.minutes + 50.seconds) == true
|
||||||
check limiter.checkUsage(proto, conn2, now + 3.minutes) == true
|
check limiter.checkUsage(proto, conn2, now + 3.minutes + 50.seconds) == true
|
||||||
check limiter.checkUsage(proto, conn3, now + 3.minutes) == true
|
|
||||||
|
|
||||||
# conn1 gets replenished as the ratio was 2 giving twice as long replenish period than the main bucket
|
# conn1 gets replenished as the ratio was 2 giving twice as long replenish period than the main bucket
|
||||||
# see waku/common/rate_limit/request_limiter.nim #func calcPeriodRatio and calcPeerTokenSetting
|
# see waku/common/rate_limit/request_limiter.nim #func calcPeriodRatio and calcPeerTokenSetting
|
||||||
check limiter.checkUsage(proto, conn1, now + 4.minutes) == true
|
check limiter.checkUsage(proto, conn1, now + 4.minutes) == true
|
||||||
# requests of other peers can also go
|
# requests of other peers can also go
|
||||||
check limiter.checkUsage(proto, conn2, now + 4100.milliseconds) == true
|
check limiter.checkUsage(proto, conn2, now + 5.minutes) == true
|
||||||
check limiter.checkUsage(proto, conn3, now + 5.minutes) == true
|
check limiter.checkUsage(proto, conn3, now + 5.minutes) == true
|
||||||
|
|
||||||
test "RequestRateLimiter lowest possible volume":
|
test "RequestRateLimiter lowest possible volume":
|
||||||
|
|||||||
@ -63,8 +63,6 @@ proc checkUsage*(
|
|||||||
let avail = peerBucket.getAvailableCapacity(now)
|
let avail = peerBucket.getAvailableCapacity(now)
|
||||||
let globAvail = t.tokenBucket.get().getAvailableCapacity(now)
|
let globAvail = t.tokenBucket.get().getAvailableCapacity(now)
|
||||||
|
|
||||||
info " NZP checkUsage", peer = conn.peerId, peerAvail = avail, globalAvail = globAvail, now = now
|
|
||||||
|
|
||||||
## check requesting peer's usage is not over the calculated ratio and let that peer go which not requested much/or this time...
|
## check requesting peer's usage is not over the calculated ratio and let that peer go which not requested much/or this time...
|
||||||
if not peerBucket.tryConsume(1, now):
|
if not peerBucket.tryConsume(1, now):
|
||||||
trace "peer usage limit reached", peer = conn.peerId
|
trace "peer usage limit reached", peer = conn.peerId
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user