|
| 1 | +(* |
| 2 | + * Copyright (C) 2025 Cloud Software Group |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published |
| 6 | + * by the Free Software Foundation; version 2.1 only. with the special |
| 7 | + * exception on linking described in file LICENSE. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + *) |
| 14 | + |
| 15 | +(** This module implements a classic token-bucket rate limiter. Token buckets |
| 16 | + contain tokens that are refilled over time, and can be consumed in a |
| 17 | + thread-safe way. A token bucket accumulates [fill_rate] tokens per second, |
| 18 | + up to [burst_size]. Consumers may take tokens (if available), or query when |
| 19 | + enough tokens will become available. |
| 20 | +
|
| 21 | + Token counts are represented as floats rather than integers to allow |
| 22 | + fractional token costs and fine-grained refill rates. |
| 23 | +
|
| 24 | + Token buckets implement rate limiting by allowing operations to proceed |
| 25 | + only when sufficient tokens are available - otherwise, the operations can |
| 26 | + be delayed until enough tokens are available. |
| 27 | +
|
| 28 | + To avoid doing unnecessary work to refill the bucket, token amounts are |
| 29 | + only updated when a consume operation is carried out. The buckets keep a |
| 30 | + last_refill timestamp which is updated on consume in tandem with the token |
| 31 | + counts, and informs how many tokens should be added by the bucket refill. |
| 32 | +
|
| 33 | + We include versions of functions that take a timestamp as a parameter for |
| 34 | + testing purposes only - consumers of this library should use the |
| 35 | + timestamp-less versions. |
| 36 | +*) |
| 37 | + |
| 38 | +type t |
| 39 | + |
| 40 | +val create : burst_size:float -> fill_rate:float -> t |
| 41 | +(** Create token bucket with given parameters. |
| 42 | + @raises Invalid_argument if the fill rate is 0 or negative. |
| 43 | + @param burst_size Maximum number of tokens that can fit in the bucket |
| 44 | + @param fill_rate Number of tokens added to the bucket per second |
| 45 | + *) |
| 46 | + |
| 47 | +val peek : t -> float |
| 48 | +(** Retrieve current token amount |
| 49 | + @param tb Token bucket |
| 50 | + @return Amount of tokens in the token bucket |
| 51 | + *) |
| 52 | + |
| 53 | +val consume : t -> float -> bool |
| 54 | +(** Consume tokens from the bucket in a thread-safe manner. |
| 55 | + @param tb Token bucket |
| 56 | + @param amount How many tokens to consume |
| 57 | + @return Whether the tokens were successfully consumed |
| 58 | + *) |
| 59 | + |
| 60 | +val get_delay_until_available : t -> float -> float |
| 61 | +(** Get number of seconds that need to pass until bucket is expected to have |
| 62 | + enough tokens to fulfil the request |
| 63 | + @param tb Token bucket |
| 64 | + @param amount How many tokens we want to consume |
| 65 | + @return Number of seconds until tokens are available |
| 66 | +*) |
| 67 | + |
| 68 | +val delay_then_consume : t -> float -> unit |
| 69 | +(** [delay_then_consume tb amount] sleeps the calling thread until [amount] |
| 70 | + tokens are available, then consumes them. Thread-safe but does not |
| 71 | + guarantee fairness between competing callers. *) |
| 72 | + |
| 73 | +(**/**) |
| 74 | + |
| 75 | +(* Fuctions accepting a timestamp are meant for testing only *) |
| 76 | + |
| 77 | +val create_with_timestamp : |
| 78 | + Mtime.span -> burst_size:float -> fill_rate:float -> t |
| 79 | +(** Create token bucket with given parameters and supplied inital timestamp. |
| 80 | + @raises Invalid_argument if the fill_rate is 0 or negative. |
| 81 | + @param timestamp Initial timestamp |
| 82 | + @param burst_size Maximum number of tokens that can fit in the bucket |
| 83 | + @param fill_rate Number of tokens added to the bucket per second |
| 84 | + *) |
| 85 | + |
| 86 | +val peek_with_timestamp : Mtime.span -> t -> float |
| 87 | +(** Retrieve token amount in token bucket at given timestamp. |
| 88 | + Undefined behaviour when [timestamp] <= [tb.timestamp] |
| 89 | + @param timestamp Current time |
| 90 | + @param tb Token bucket |
| 91 | + @return Amount of tokens in the token bucket |
| 92 | + *) |
| 93 | + |
| 94 | +val consume_with_timestamp : (unit -> Mtime.span) -> t -> float -> bool |
| 95 | +(** Consume tokens from the bucket in a thread-safe manner, using supplied |
| 96 | + function for obtaining the current time |
| 97 | + @param get_time Function to obtain timestamp, e.g. Mtime_clock.elapsed |
| 98 | + @param tb Token bucket |
| 99 | + @param amount How many tokens to consume |
| 100 | + @return Whether the tokens were successfully consumed |
| 101 | + *) |
| 102 | + |
| 103 | +val get_delay_until_available_timestamp : Mtime.span -> t -> float -> float |
| 104 | +(** Get number of seconds that need to pass until bucket is expected to have |
| 105 | + enough tokens to fulfil the request |
| 106 | + @param timestamp |
| 107 | + @param tb Token bucket |
| 108 | + @param amount How many tokens we want to consume |
| 109 | + @return Number of seconds until tokens are available |
| 110 | +*) |
| 111 | + |
| 112 | +(**/**) |
0 commit comments