-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added simple, configurable rate limit for lightpush and store-q…
…uery 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 Co-authored-by: gabrielmer <[email protected]> Fixing typos and PR comments Fix tests Move chronos/tokenbucket to nwaku codebasee with limited and fixed feature set Add meterics to lightpush rate limits Fixes for code and test + test reliability with timing
- Loading branch information
1 parent
24f6fed
commit 7dd66d0
Showing
21 changed files
with
677 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
import ./test_client | ||
import ./test_client, ./test_ratelimit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
{.used.} | ||
|
||
import | ||
std/[options, strscans], | ||
testutils/unittests, | ||
chronicles, | ||
chronos, | ||
libp2p/crypto/crypto | ||
|
||
import | ||
../../waku/[ | ||
node/peer_manager, | ||
common/ratelimit, | ||
waku_core, | ||
waku_lightpush, | ||
waku_lightpush/client, | ||
waku_lightpush/common, | ||
waku_lightpush/protocol_metrics, | ||
waku_lightpush/rpc, | ||
waku_lightpush/rpc_codec, | ||
], | ||
../testlib/[assertions, wakucore, testasync, futures, testutils], | ||
./lightpush_utils, | ||
../resources/[pubsub_topics, content_topics, payloads] | ||
|
||
suite "Rate limited push service": | ||
asyncTest "push message with rate limit not violated": | ||
## Setup | ||
let | ||
serverSwitch = newTestSwitch() | ||
clientSwitch = newTestSwitch() | ||
|
||
await allFutures(serverSwitch.start(), clientSwitch.start()) | ||
|
||
## Given | ||
var handlerFuture = newFuture[(string, WakuMessage)]() | ||
let handler: PushMessageHandler = proc( | ||
peer: PeerId, pubsubTopic: PubsubTopic, message: WakuMessage | ||
): Future[WakuLightPushResult[void]] {.async.} = | ||
handlerFuture.complete((pubsubTopic, message)) | ||
return ok() | ||
|
||
let | ||
tokenPeriod = 500.millis | ||
server = | ||
await newTestWakuLightpushNode(serverSwitch, handler, some((3, tokenPeriod))) | ||
client = newTestWakuLightpushClient(clientSwitch) | ||
|
||
let serverPeerId = serverSwitch.peerInfo.toRemotePeerInfo() | ||
|
||
let sendMsgProc = proc(): Future[void] {.async.} = | ||
let message = fakeWakuMessage() | ||
|
||
handlerFuture = newFuture[(string, WakuMessage)]() | ||
let requestRes = | ||
await client.publish(DefaultPubsubTopic, message, peer = serverPeerId) | ||
|
||
check await handlerFuture.withTimeout(50.millis) | ||
|
||
check: | ||
requestRes.isOk() | ||
handlerFuture.finished() | ||
|
||
let (handledMessagePubsubTopic, handledMessage) = handlerFuture.read() | ||
|
||
check: | ||
handledMessagePubsubTopic == DefaultPubsubTopic | ||
handledMessage == message | ||
|
||
let waitInBetweenFor = 20.millis | ||
|
||
# Test cannot be too explicit about the time when the TokenBucket resets | ||
# the internal timer, although in normal use there is no use case to care about it. | ||
var firstWaitExtend = 300.millis | ||
|
||
for runCnt in 0 ..< 3: | ||
let startTime = Moment.now() | ||
for testCnt in 0 ..< 3: | ||
await sendMsgProc() | ||
await sleepAsync(20.millis) | ||
|
||
var endTime = Moment.now() | ||
var elapsed: Duration = (endTime - startTime) | ||
await sleepAsync(tokenPeriod - elapsed + firstWaitExtend) | ||
firstWaitEXtend = 100.millis | ||
|
||
## Cleanup | ||
await allFutures(clientSwitch.stop(), serverSwitch.stop()) | ||
|
||
asyncTest "push message with rate limit reject": | ||
## Setup | ||
let | ||
serverSwitch = newTestSwitch() | ||
clientSwitch = newTestSwitch() | ||
|
||
await allFutures(serverSwitch.start(), clientSwitch.start()) | ||
|
||
## Given | ||
var handlerFuture = newFuture[(string, WakuMessage)]() | ||
let handler = proc( | ||
peer: PeerId, pubsubTopic: PubsubTopic, message: WakuMessage | ||
): Future[WakuLightPushResult[void]] {.async.} = | ||
handlerFuture.complete((pubsubTopic, message)) | ||
return ok() | ||
|
||
let | ||
server = | ||
await newTestWakuLightpushNode(serverSwitch, handler, some((3, 1000.millis))) | ||
client = newTestWakuLightpushClient(clientSwitch) | ||
|
||
let serverPeerId = serverSwitch.peerInfo.toRemotePeerInfo() | ||
let topic = DefaultPubsubTopic | ||
|
||
let successProc = proc(): Future[void] {.async.} = | ||
let message = fakeWakuMessage() | ||
handlerFuture = newFuture[(string, WakuMessage)]() | ||
let requestRes = | ||
await client.publish(DefaultPubsubTopic, message, peer = serverPeerId) | ||
discard await handlerFuture.withTimeout(10.millis) | ||
|
||
check: | ||
requestRes.isOk() | ||
handlerFuture.finished() | ||
let (handledMessagePubsubTopic, handledMessage) = handlerFuture.read() | ||
check: | ||
handledMessagePubsubTopic == DefaultPubsubTopic | ||
handledMessage == message | ||
|
||
let rejectProc = proc(): Future[void] {.async.} = | ||
let message = fakeWakuMessage() | ||
handlerFuture = newFuture[(string, WakuMessage)]() | ||
let requestRes = | ||
await client.publish(DefaultPubsubTopic, message, peer = serverPeerId) | ||
discard await handlerFuture.withTimeout(10.millis) | ||
|
||
check: | ||
requestRes.isErr() | ||
requestRes.error == "TOO_MANY_REQUESTS" | ||
|
||
for testCnt in 0 .. 2: | ||
await successProc() | ||
await sleepAsync(20.millis) | ||
|
||
await rejectProc() | ||
|
||
await sleepAsync(1000.millis) | ||
|
||
## next one shall succeed due to the rate limit time window has passed | ||
await successProc() | ||
|
||
## Cleanup | ||
await allFutures(clientSwitch.stop(), serverSwitch.stop()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.