|
| 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 buckets implement rate limiting by allowing operations to proceed |
| 22 | + only when sufficient tokens are available - otherwise, the operations can |
| 23 | + be delayed until enough tokens are available. |
| 24 | +
|
| 25 | + To avoid doing unnecessary work to refill the bucket, token amounts are |
| 26 | + only updated when a consume operation is carried out. The buckets keep a |
| 27 | + last_refill timestamp which is updated on consume in tandem with the token |
| 28 | + counts, and informs how many tokens should be added by the bucket refill. |
| 29 | +
|
| 30 | + We include versions of functions that take a timestamp as a parameter for |
| 31 | + testing purposes only - consumers of this library should use the |
| 32 | + timestamp-less versions. |
| 33 | +*) |
| 34 | + |
| 35 | +type t |
| 36 | + |
| 37 | +val create : burst_size:float -> fill_rate:float -> t option |
| 38 | +(** Create token bucket with given parameters. |
| 39 | + Returns None if the fill rate is 0 or negative. |
| 40 | + @param burst_size Maximum number of tokens that can fit in the bucket |
| 41 | + @param fill_rate Number of tokens added to the bucket per second |
| 42 | + *) |
| 43 | + |
| 44 | +val peek : t -> float |
| 45 | +(** Retrieve current token amount |
| 46 | + @param tb Token bucket |
| 47 | + @return Amount of tokens in the token bucket |
| 48 | + *) |
| 49 | + |
| 50 | +val consume : t -> float -> bool |
| 51 | +(** Consume tokens from the bucket in a thread-safe manner. |
| 52 | + @param tb Token bucket |
| 53 | + @param amount How many tokens to consume |
| 54 | + @return Whether the tokens were successfully consumed |
| 55 | + *) |
| 56 | + |
| 57 | +val get_delay_until_available : t -> float -> float |
| 58 | +(** Get number of seconds that need to pass until bucket is expected to have |
| 59 | + enough tokens to fulfil the request |
| 60 | + @param tb Token bucket |
| 61 | + @param amount How many tokens we want to consume |
| 62 | + @return Number of seconds until tokens are available |
| 63 | +*) |
| 64 | + |
| 65 | +val delay_then_consume : t -> float -> unit |
| 66 | + |
| 67 | +(**/**) |
| 68 | + |
| 69 | +(* Fuctions accepting a timestamp are meant for testing only *) |
| 70 | + |
| 71 | +val create_with_timestamp : |
| 72 | + Mtime.span -> burst_size:float -> fill_rate:float -> t option |
| 73 | +(** Create token bucket with given parameters and supplied inital timestamp |
| 74 | + Returns None if the fill_rate is 0 or negative. |
| 75 | + @param timestamp Initial timestamp |
| 76 | + @param burst_size Maximum number of tokens that can fit in the bucket |
| 77 | + @param fill_rate Number of tokens added to the bucket per second |
| 78 | + *) |
| 79 | + |
| 80 | +val peek_with_timestamp : Mtime.span -> t -> float |
| 81 | +(** Retrieve token amount in token bucket at given timestamp. |
| 82 | + Undefined behaviour when [timestamp] <= [tb.timestamp] |
| 83 | + @param timestamp Current time |
| 84 | + @param tb Token bucket |
| 85 | + @return Amount of tokens in the token bucket |
| 86 | + *) |
| 87 | + |
| 88 | +val consume_with_timestamp : (unit -> Mtime.span) -> t -> float -> bool |
| 89 | +(** Consume tokens from the bucket in a thread-safe manner, using supplied |
| 90 | + function for obtaining the current time |
| 91 | + @param get_time Function to obtain timestamp, e.g. Mtime_clock.elapsed |
| 92 | + @param tb Token bucket |
| 93 | + @param amount How many tokens to consume |
| 94 | + @return Whether the tokens were successfully consumed |
| 95 | + *) |
| 96 | + |
| 97 | +val get_delay_until_available_timestamp : Mtime.span -> t -> float -> float |
| 98 | +(** Get number of seconds that need to pass until bucket is expected to have |
| 99 | + enough tokens to fulfil the request |
| 100 | + @param timestamp |
| 101 | + @param tb Token bucket |
| 102 | + @param amount How many tokens we want to consume |
| 103 | + @return Number of seconds until tokens are available |
| 104 | +*) |
| 105 | + |
| 106 | +(**/**) |
0 commit comments