diff options
Diffstat (limited to 'components/wisp_relay/rate_limiter.h')
| -rw-r--r-- | components/wisp_relay/rate_limiter.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/components/wisp_relay/rate_limiter.h b/components/wisp_relay/rate_limiter.h new file mode 100644 index 0000000..655ddf2 --- /dev/null +++ b/components/wisp_relay/rate_limiter.h | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | #ifndef RATE_LIMITER_H | ||
| 2 | #define RATE_LIMITER_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include "freertos/FreeRTOS.h" | ||
| 7 | #include "freertos/semphr.h" | ||
| 8 | |||
| 9 | #define RATE_LIMITER_MAX_BUCKETS 16 | ||
| 10 | |||
| 11 | typedef enum { | ||
| 12 | RATE_TYPE_EVENT, | ||
| 13 | RATE_TYPE_REQ, | ||
| 14 | } rate_type_t; | ||
| 15 | |||
| 16 | typedef struct { | ||
| 17 | uint16_t events_per_minute; | ||
| 18 | uint16_t reqs_per_minute; | ||
| 19 | } rate_config_t; | ||
| 20 | |||
| 21 | typedef struct { | ||
| 22 | int fd; | ||
| 23 | uint16_t event_count; | ||
| 24 | uint16_t req_count; | ||
| 25 | uint32_t window_start; | ||
| 26 | bool active; | ||
| 27 | } rate_bucket_t; | ||
| 28 | |||
| 29 | typedef struct rate_limiter { | ||
| 30 | rate_config_t config; | ||
| 31 | rate_bucket_t buckets[RATE_LIMITER_MAX_BUCKETS]; | ||
| 32 | SemaphoreHandle_t lock; | ||
| 33 | } rate_limiter_t; | ||
| 34 | |||
| 35 | void rate_limiter_init(rate_limiter_t *rl, const rate_config_t *config); | ||
| 36 | void rate_limiter_destroy(rate_limiter_t *rl); | ||
| 37 | bool rate_limiter_check(rate_limiter_t *rl, int fd, rate_type_t type); | ||
| 38 | void rate_limiter_reset(rate_limiter_t *rl, int fd); | ||
| 39 | |||
| 40 | #endif | ||