| 1 | /** |
| 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0. |
| 4 | */ |
| 5 | |
| 6 | #ifndef AWS_RATE_LIMITERS_H |
| 7 | #define AWS_RATE_LIMITERS_H |
| 8 | |
| 9 | #include <aws/mqtt/mqtt.h> |
| 10 | |
| 11 | #include <aws/io/io.h> |
| 12 | |
| 13 | struct aws_rate_limiter_token_bucket_options { |
| 14 | /* Clock function override. If left null, the high resolution clock will be used */ |
| 15 | aws_io_clock_fn *clock_fn; |
| 16 | |
| 17 | /* How many tokens regenerate per second? */ |
| 18 | uint64_t tokens_per_second; |
| 19 | |
| 20 | /* Initial amount of tokens the limiter will start with */ |
| 21 | uint64_t initial_token_count; |
| 22 | |
| 23 | /* |
| 24 | * Maximum amount of tokens the limiter can hold. Regenerated tokens that exceed this maximum are |
| 25 | * discarded |
| 26 | */ |
| 27 | uint64_t maximum_token_count; |
| 28 | }; |
| 29 | |
| 30 | /** |
| 31 | * A token-bucket based rate limiter. |
| 32 | * |
| 33 | * Has an unusually complex implementation due to implementer-desired constraints: |
| 34 | * |
| 35 | * (1) Model regeneration as an integral rate per second. This is for ease-of-use. A regeneration interval would |
| 36 | * be a much simpler implementation, but not as intuitive (or accurate for non-integral rates). |
| 37 | * (2) Integer math only. Not comfortable falling back on doubles and not having a good understanding of the |
| 38 | * accuracy issues, over time, that doing so would create. |
| 39 | * (3) Minimize as much as possible the dangers of multiplication saturation and integer division round-down. |
| 40 | * (4) No integer division round-off "error" accumulation allowed. Arguments could be made that it might be small |
| 41 | * enough to never make a difference but I'd rather not even have the argument at all. |
| 42 | * (5) A perfectly accurate how-long-must-I-wait query. Not just a safe over-estimate. |
| 43 | */ |
| 44 | struct aws_rate_limiter_token_bucket { |
| 45 | uint64_t last_service_time; |
| 46 | uint64_t current_token_count; |
| 47 | |
| 48 | uint64_t fractional_nanos; |
| 49 | uint64_t fractional_nano_tokens; |
| 50 | |
| 51 | struct aws_rate_limiter_token_bucket_options config; |
| 52 | }; |
| 53 | |
| 54 | AWS_EXTERN_C_BEGIN |
| 55 | |
| 56 | /** |
| 57 | * Initializes a token-bucket-based rate limiter |
| 58 | * |
| 59 | * @param limiter rate limiter to intiialize |
| 60 | * @param options configuration values for the token bucket rate limiter |
| 61 | * @return AWS_OP_SUCCESS/AWS_OP_ERR |
| 62 | */ |
| 63 | AWS_MQTT_API int aws_rate_limiter_token_bucket_init( |
| 64 | struct aws_rate_limiter_token_bucket *limiter, |
| 65 | const struct aws_rate_limiter_token_bucket_options *options); |
| 66 | |
| 67 | /** |
| 68 | * Resets a token-bucket-based rate limiter |
| 69 | * |
| 70 | * @param limiter rate limiter to reset |
| 71 | */ |
| 72 | AWS_MQTT_API void aws_rate_limiter_token_bucket_reset(struct aws_rate_limiter_token_bucket *limiter); |
| 73 | |
| 74 | /** |
| 75 | * Queries if the token bucket has a number of tokens currently available |
| 76 | * |
| 77 | * @param limiter token bucket rate limiter to query, non-const because token count is lazily updated |
| 78 | * @param token_count how many tokens to check for |
| 79 | * @return true if that many tokens are available, false otherwise |
| 80 | */ |
| 81 | AWS_MQTT_API bool aws_rate_limiter_token_bucket_can_take_tokens( |
| 82 | struct aws_rate_limiter_token_bucket *limiter, |
| 83 | uint64_t token_count); |
| 84 | |
| 85 | /** |
| 86 | * Takes a number of tokens from the token bucket rate limiter |
| 87 | * |
| 88 | * @param limiter token bucket rate limiter to take from |
| 89 | * @param token_count how many tokens to take |
| 90 | * @return AWS_OP_SUCCESS if there were that many tokens available, AWS_OP_ERR otherwise |
| 91 | */ |
| 92 | AWS_MQTT_API int aws_rate_limiter_token_bucket_take_tokens( |
| 93 | struct aws_rate_limiter_token_bucket *limiter, |
| 94 | uint64_t token_count); |
| 95 | |
| 96 | /** |
| 97 | * Queries a token-bucket-based rate limiter for how long, in nanoseconds, until a specified amount of tokens will |
| 98 | * be available. |
| 99 | * |
| 100 | * @param limiter token-bucket-based rate limiter to query |
| 101 | * @param token_count how many tokens need to be avilable |
| 102 | * @return how long the caller must wait, in nanoseconds, before that many tokens are available |
| 103 | */ |
| 104 | AWS_MQTT_API uint64_t aws_rate_limiter_token_bucket_compute_wait_for_tokens( |
| 105 | struct aws_rate_limiter_token_bucket *limiter, |
| 106 | uint64_t token_count); |
| 107 | |
| 108 | AWS_EXTERN_C_END |
| 109 | |
| 110 | #endif /* AWS_RATE_LIMITERS_H */ |