nwaku/waku/common/ratelimit.nim
NagyZoltanPeter a00f350cd1
feat: Added simple, configurable rate limit for lightpush and store-query (#2390)
* feat: Added simple, configurable rate limit for lightpush and store-query
Adjust lightpush rest response to rate limit, added tests ann some fixes
Add rest store query test for rate limit checks and proper error response
Update apps/wakunode2/external_config.nim
Move chronos/tokenbucket to nwaku codebasee with limited and fixed feature set
Add meterics counter to lightpush rate limits

Co-authored-by: gabrielmer <101006718+gabrielmer@users.noreply.github.com>
2024-04-15 15:28:35 +02:00

25 lines
602 B
Nim

when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import std/options
import chronos/timer
import ./tokenbucket
export tokenbucket
type RateLimitSetting* = tuple[volume: int, period: Duration]
let DefaultGlobalNonRelayRateLimit*: RateLimitSetting = (60, 1.minutes)
proc newTokenBucket*(setting: Option[RateLimitSetting]): Option[TokenBucket] =
if setting.isNone:
return none[TokenBucket]()
let (volume, period) = setting.get()
if volume <= 0 or period <= 0.seconds:
return none[TokenBucket]()
return some(TokenBucket.new(volume, period))