2024-04-15 13:28:35 +00:00
|
|
|
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]
|
|
|
|
|
2024-04-25 15:51:34 +00:00
|
|
|
# Set the default to switch off rate limiting for now
|
|
|
|
let DefaultGlobalNonRelayRateLimit*: RateLimitSetting = (0, 0.minutes)
|
2024-04-15 13:28:35 +00:00
|
|
|
|
|
|
|
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))
|