Commit Graph
7 Commits
Author SHA1 Message Date
Jacek Sieka 8ed098ef61 ratelimit: fix non-deterministic start time in test (#608) 2025-12-27 16:13:32 +01:00
NagyZoltanPeterandJacek Sieka 85af4db764 Token bucket unification (#582)
* Fix original TokenBucket, consolidate the behaviors of chronos/TokenBucket and waku/TokenBucket (compensating) with no interface change

* TokenBucket extended with waku's strict mode replenish, that does not allow refill just after period boundary elapsed.

* Adjust for new chat-sdk needs, added setState and getAvailableCapacity with small refactoring

* Better comments

* rename for better self explain code, added more explanatory comments upon code review finding

* Document TokenBucket with detailed samples of Balanced mode replenishment algorithm, extended TokenBucket unit test

* Address review comments, renaming the replenish modes, removed old/new algo comparison from documentation, fix Discrete mode update calculation to properly calculate correct last update time by period distance calculation.

* protect from devide by zero, code style fix

* Make (re)setState close pending request before reseting state

* Polishing interface, move discrete mode initial start time setup into ctor and remove resetState to have a cleaner and consistent interface for both usage mode

* Addressing review observation, keep only singel new ctor and leave defaults to match former use.

* Removing leftovers and confirm with coding guideline

* refactor rate limiter implementation

* make sure that `tryConsume` respects queued `consume` requests
* refill tokens the same way regardless of replenish mode and source of
tokens (manual/time-based/cancellation)
* whem manually replenishing, compute budget cap after satisfying queued
requests
* don't run worker if fill duration is 0
* add some docs

* int vs int64

* Fix Discrete mode time window drift, added tests

* wait for sleeper/waiter

---------

Co-authored-by: Jacek Sieka <jacek@status.im>
2025-12-12 15:25:45 +01:00
Eugene Kabanov 2e8551b0d9 Cancellation fixes and tests. (#445)
* Add callTick and stream cancellation tests.

* Fix stepsAsync() test.

* Cancellation changes.

* Update and add more cancellation tests.

* Fix Posix shutdown call to handle ENOTCONN error.

* With new changes to to cancellation its now possible.

* Refactor testsoon.nim to not produce artifacts after tests are finished.

* Debugging MacOS issue.

* Adjust flaky test times.

* Fix issue.

* Add test for issue #334 which was also addressed in this PR.
Avoid `break` in problematic test.

* Add noCancelWait() call which prohibits cancellation.
Fix closeWait() calls to use noCancelWait() predicate.
Adding sleep to flaky MacOS test.

* Remove all debugging echoes.

* Fix cancelAndWait() which now could perform multiple attempts to cancel target Future (mustCancel behavior).

* Fix issues revealed by switch to different cancelAndWait().

* Address review comments.

* Fix testutils compilation warning.

* Rename callTick() to internalCallTick().

* Add some documentation comments.

* Disable flaky ratelimit test.

* Rename noCancelWait() to noCancel().
Address review comments.
2023-09-15 19:38:39 +03:00
Tanguy 47016961f5 Less flaky rate limit tests (#408) 2023-06-23 10:11:14 +02:00
Jacek Sieka 229de5f842 Compile-time configuration (#371)
This PR moves all compile-time configuration to a single module,
simplifying documentation and access to these features.

Upcomfing features may be enabled either individually, or through a new
`chronosPreviewV4` catch-all designed to allow code to be prepared for
increased strictness in future chronos releases.

`-d:chronosDebug` may be used to enable the existing debugging helpers
together.
2023-03-31 07:35:04 +02:00
Tanguy 4ada7fc0e1 Fix flaky token bucket CI (#338) 2023-01-18 16:02:00 +01:00
TanguyandJacek Sieka 24146463a3 Token bucket (#279)
- A single sleepAsync per bucket
- Manual replenish with async
- Cancellation of consume

Co-authored-by: Jacek Sieka <jacek@status.im>
2022-11-02 08:03:19 +01:00