|
1 | 1 | # Rate limiter
|
2 | 2 |
|
3 |
| -The rate limiter mechanism allows controlling the rate at which operations are executed. It ensures that at most a certain number of operations are run concurrently within a specified time frame, preventing system overload and ensuring fair resource usage. Note that you can choose if algorithm takes into account only the start of execution or the whole execution of an operation. |
| 3 | +The rate limiter mechanism allows controlling the rate at which operations are executed. It ensures that at most a certain number of operations are run concurrently within a specified time frame, preventing system overload and ensuring fair resource usage. Note that you can choose algorithm that takes into account only the start of execution or the whole execution time of an operation. |
4 | 4 |
|
5 | 5 | ## API
|
6 | 6 |
|
@@ -34,26 +34,27 @@ The `operation` can be provided directly using a by-name parameter, i.e. `f: =>
|
34 | 34 | ## Configuration
|
35 | 35 |
|
36 | 36 | The configuration of a `RateLimiter` depends on an underlying algorithm that controls whether an operation can be executed or not. The following algorithms are available:
|
37 |
| -- `RateLimiterAlgorithm.FixedWindow(rate: Int, dur: FiniteDuration)` - where `rate` is the maximum number of operations to be executed in fixed windows of `dur` duration. |
38 |
| -- `RateLimiterAlgorithm.SlidingWindow(rate: Int, dur: FiniteDuration)` - where `rate` is the maximum number of operations to be executed in any window of time of duration `dur`. |
39 |
| -- `RateLimiterAlgorithm.Bucket(maximum: Int, dur: FiniteDuration)` - where `maximum` is the maximum capacity of tokens available in the token bucket algorithm and one token is added each `dur`. It can represent both the leaky bucket algorithm or the token bucket algorithm. |
40 |
| -- `DurationRateLimiterAlgorithm.FixedWindow(rate: Int, dur: FiniteDuration)` - where `rate` is the maximum number of operations which execution spans fixed windows of `dur` duration. |
41 |
| -- `DurationRateLimiterAlgorithm.SlidingWindow(rate: Int, dur: FiniteDuration)` - where `rate` is the maximum number of operations which execution spans any window of time of duration `dur`. |
| 37 | +- `RateLimiterAlgorithm.FixedWindow(rate: Int, per: FiniteDuration)` - where `rate` is the maximum number of operations to be executed in fixed windows of `per` duration. |
| 38 | +- `RateLimiterAlgorithm.SlidingWindow(rate: Int, per: FiniteDuration)` - where `rate` is the maximum number of operations to be executed in any window of time of duration `per`. |
| 39 | +- `RateLimiterAlgorithm.Bucket(maximum: Int, per: FiniteDuration)` - where `rate` is the maximum capacity of tokens available in the token bucket algorithm and one token is added each `per`. It can represent both the leaky bucket algorithm or the token bucket algorithm. |
| 40 | +- `DurationRateLimiterAlgorithm.FixedWindow(rate: Int, per: FiniteDuration)` - where `rate` is the maximum number of operations which execution spans fixed windows of `per` duration. Considers whole execution time of an operation. Operation spanning more than one window blocks permits in all windows that it spans. |
| 41 | +- `DurationRateLimiterAlgorithm.SlidingWindow(rate: Int, per: FiniteDuration)` - where `rate` is the maximum number of operations which execution spans any window of time of duration `per`. Considers whole execution time of an operation. Operation release permit after `per` passed since operation ended. |
42 | 42 |
|
43 | 43 | ### API shorthands
|
44 | 44 |
|
45 | 45 | You can use one of the following shorthands to define a Rate Limiter with the corresponding algorithm:
|
46 | 46 |
|
47 |
| -- `RateLimiter.fixedWindow(rate: Int, dur: FiniteDuration, operationMode: RateLimiterMode = RateLimiterMode.OperationStart)`, |
48 |
| -- `RateLimiter.slidingWindow(rate: Int, dur: FiniteDuration, operationMode: RateLimiterMode = RateLimiterMode.OperationStart)`, |
49 |
| -- `RateLimiter.leakyBucket(maximum: Int, dur: FiniteDuration)` |
| 47 | +- `RateLimiter.fixedWindow(maxOperations: Int, window: FiniteDuration)` |
| 48 | +- `RateLimiter.slidingWindow(maxOperations: Int, window: FiniteDuration)` |
| 49 | +- `RateLimiter.leakyBucket(maxTokens: Int, refillInterval: FiniteDuration)` |
| 50 | +- `RateLimiter.durationFixedWindow(maxOperations: Int, window: FiniteDuration)` |
| 51 | +- `RateLimiter.durationSlidingWindow(maxOperations: Int, window: FiniteDuration)` |
50 | 52 |
|
51 |
| -These shorthands also allow to define if the whole execution time of an operation should be considered. |
52 | 53 | See the tests in `ox.resilience.*` for more.
|
53 | 54 |
|
54 | 55 | ## Custom rate limiter algorithms
|
55 | 56 |
|
56 |
| -The `RateLimiterAlgorithm` employed by `RateLimiter` can be extended to implement new algorithms or modify existing ones. Its interface is modelled like that of a `Semaphore` although the underlying implementation could be different. For best compatibility with the existing interface of `RateLimiter`, methods `acquire` and `tryAcquire` should offer the same guaranties as Java's `Semaphores`. There is also method `def runOperation[T](operation: => T, permits: Int): T` for cases where considering span of execution may be necessary. |
| 57 | +The `RateLimiterAlgorithm` employed by `RateLimiter` can be extended to implement new algorithms or modify existing ones. Its interface is modelled like that of a `Semaphore` although the underlying implementation could be different. For best compatibility with the existing interface of `RateLimiter`, methods `acquire` and `tryAcquire` should offer the same guaranties as Java's `Semaphores`. There is also method `def runOperation[T](operation: => T, permits: Int): T` for cases where considering span of execution may be necessary(see implementations in `DurationRateLimiterAlgorithm`). |
57 | 58 |
|
58 | 59 | Additionally, there are two methods employed by the `GenericRateLimiter` for updating its internal state automatically:
|
59 | 60 | - `def update(): Unit`: Updates the internal state of the rate limiter to reflect its current situation. Invoked in a background fork repeatedly, when a rate limiter is created.
|
|
0 commit comments