Commit f5effb5
Ratelimiter (#181)
* feat(ratelimiter): add token bucket rate limiter
Introduce a thread-safe token bucket rate limiter with dynamic capacity
and refill rate adjustment. Includes comprehensive tests and usage
examples. Provides both blocking and non-blocking methods, context
support, and runtime configuration.
* feat(ratelimiter): add token bucket rate limiter
Introduce a thread-safe token bucket rate limiter with dynamic capacity
and refill rate adjustment. Includes comprehensive tests and usage
examples. Provides both blocking and non-blocking methods, context
support, and runtime configuration.
* Delete .DS_Store
Signed-off-by: Dimitrios Kyriakidis Kortsekidis <kwstasnere@gmail.com>
* fix(ratelimiter): improve token precision and WaitN accuracy
Refactored TokenBucket to use float64 for tokens, allowing fractional
token accumulation and more precise rate limiting. Updated refill and
token consumption logic to prevent rounding errors. Enhanced WaitN to
avoid busy-waiting and improve responsiveness to context cancellation.
Adjusted tests for timing accuracy with new token handling.
* fix(ratelimiter): avoid race on capacity check in WaitN
Previously, the capacity check in WaitN accessed t.capacity without
locking, which could lead to a race condition if the capacity was
changed concurrently. This change ensures the capacity is read under
lock before comparing with the requested token count, improving
thread safety.
* feat(ratelimiter): improve precision, fix races, enhance docs
- Move unexported methods to helper.go for better organization
- Add comprehensive documentation for all exported functions
- Improve code readability with better formatting
- Add expected output to all examples in EXAMPLES.md
* Update README.md
* fix(ratelimiter): add capacity validation to nextAvailableDuration
- Return -1 for impossible requests exceeding capacity
- Handle -1 return value in WaitN with proper error message
- Fix mutex ordering to prevent race conditions
* fix(ratelimiter): improve token bucket validation logic
Refactored token bucket methods to better handle invalid input and edge
cases. Removed unnecessary mutex usage in nextAvailableDuration and added
checks for negative refill rates. Cleaned up code formatting and comments
for clarity.
* Update README.md
Signed-off-by: Dimitrios Kyriakidis Kortsekidis <kwstasnere@gmail.com>
* Update ratelimiter/ratelimiter.go
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Dimitrios Kyriakidis Kortsekidis <kwstasnere@gmail.com>
* Fix typo
* Delete dbutils directory / Uploaded by mistake
Signed-off-by: Dimitrios Kyriakidis Kortsekidis <kwstasnere@gmail.com>
* Refactor suggestions by coderabbitai
* Add more tests
* Update ratelimiter/ratelimiter.go
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
* Update README.md
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
* formatted
* updated gitignore
* updated gitignore
* Fix lint issues
---------
Signed-off-by: Dimitrios Kyriakidis Kortsekidis <kwstasnere@gmail.com>
Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
Co-authored-by: kashif khan <kashif.khan@trufflesec.com>1 parent 88d88b7 commit f5effb5
7 files changed
Lines changed: 1183 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
71 | 72 | | |
72 | 73 | | |
73 | 74 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
| 100 | + | |
99 | 101 | | |
100 | 102 | | |
101 | 103 | | |
| |||
0 commit comments