mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-03 22:43:09 +00:00
* chore: use type for rate limit config Use type instead of `seq[string]` for rate limit config earlier. Enables to fail faster (at config time) if the string is malformated Also enables using object in some scenarios. * test: remove import warnings * improve naming and add tests
30 lines
949 B
Nim
30 lines
949 B
Nim
import chronicles, std/[net, options], results
|
|
import waku/common/rate_limit/setting
|
|
|
|
logScope:
|
|
topics = "waku conf builder rate limit"
|
|
|
|
type RateLimitConfBuilder* = object
|
|
strValue: Option[seq[string]]
|
|
objValue: Option[ProtocolRateLimitSettings]
|
|
|
|
proc init*(T: type RateLimitConfBuilder): RateLimitConfBuilder =
|
|
RateLimitConfBuilder()
|
|
|
|
proc withRateLimits*(b: var RateLimitConfBuilder, rateLimits: seq[string]) =
|
|
b.strValue = some(rateLimits)
|
|
|
|
proc build*(b: RateLimitConfBuilder): Result[ProtocolRateLimitSettings, string] =
|
|
if b.strValue.isSome() and b.objValue.isSome():
|
|
return err("Rate limits conf must only be set once on the builder")
|
|
|
|
if b.objValue.isSome():
|
|
return ok(b.objValue.get())
|
|
|
|
if b.strValue.isSome():
|
|
let rateLimits = ProtocolRateLimitSettings.parse(b.strValue.get()).valueOr:
|
|
return err("Invalid rate limits settings:" & $error)
|
|
return ok(rateLimits)
|
|
|
|
return ok(DefaultProtocolRateLimit)
|