Skip to content

Commit

Permalink
Add meterics to lightpush rate limits
Browse files Browse the repository at this point in the history
  • Loading branch information
NagyZoltanPeter committed Mar 21, 2024
1 parent b287730 commit b96ab38
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
9 changes: 6 additions & 3 deletions waku/common/tokenbucket.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import chronos
## This is an extract from chronos/ratelimit.nim due to the found bug in the original implementation.
## Unfortunately that bug cannot be solved without harm the original features of TokenBucket class.
## So, this current shortcut is used to enable move ahead with nwaku rate limiter implementation.

type BucketObserver* =
proc(consumeResult: bool, ): Future[void] {.async, closure.}

type TokenBucket* = ref object
budget*: int
budgetCap: int
Expand Down Expand Up @@ -39,14 +43,13 @@ proc tryConsume*(bucket: TokenBucket, tokens: int, now = Moment.now()): bool =
bucket.budget -= tokens
return true

# bucket.altUpdate(now)
bucket.update(now)

if bucket.budget >= tokens:
bucket.budget -= tokens
true
return true
else:
false
return false

proc replenish*(bucket: TokenBucket, tokens: int, now = Moment.now()) =
## Add `tokens` to the budget (capped to the bucket capacity)
Expand Down
12 changes: 12 additions & 0 deletions waku/common/waku_service_metrics.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import metrics

declarePublicCounter waku_service_requests,
"number of non-relay service requests received", ["service"]
declarePublicCounter waku_service_requests_rejected,
"number of non-relay service requests received being rejected due to limit overdue",
["service"]
13 changes: 8 additions & 5 deletions waku/waku_lightpush/protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import
./rpc,
./rpc_codec,
./protocol_metrics,
../common/ratelimit
../common/ratelimit,
../common/waku_service_metrics

export ratelimit

Expand All @@ -31,6 +32,7 @@ proc handleRequest*(
let reqDecodeRes = PushRPC.decode(buffer)
var
isSuccess = false
isRejectedDueRateLimit = false
pushResponseInfo = ""
requestId = ""

Expand All @@ -39,13 +41,14 @@ proc handleRequest*(
elif reqDecodeRes.get().request.isNone():
pushResponseInfo = emptyRequestBodyFailure
elif wl.requestRateLimiter.isSome() and not wl.requestRateLimiter.get().tryConsume(1):
isRejectedDueRateLimit = true
let pushRpcRequest = reqDecodeRes.get()
debug "lightpush request rejected due rate limit exceeded",
trace "lightpush request rejected due rate limit exceeded",
peerId = $peerId, requestId = pushRpcRequest.requestId
pushResponseInfo = TooManyRequestsMessage
##TODO: add metrics of above limit requests per minute or period defined
waku_service_requests_rejected.inc(labelValues = ["Lightpush"])
else:
##TODO: add metrics of below limit requests per minute or period defined
waku_service_requests.inc(labelValues = ["Lightpush"])

let pushRpcRequest = reqDecodeRes.get()

Expand All @@ -67,7 +70,7 @@ proc handleRequest*(
isSuccess = handleRes.isOk()
pushResponseInfo = (if isSuccess: "OK" else: handleRes.error)

if not isSuccess:
if not isSuccess and not isRejectedDueRateLimit:
waku_lightpush_errors.inc(labelValues = [pushResponseInfo])
error "failed to push message", error = pushResponseInfo
let response = PushResponse(isSuccess: isSuccess, info: some(pushResponseInfo))
Expand Down

0 comments on commit b96ab38

Please sign in to comment.