|
cwist
Modern C web framework with libttak runtime
|
Optional process-shared Redis rate limiter. More...
#include <cwist/sys/app/middleware.h>#include <cwist/core/mem/alloc.h>#include <arpa/inet.h>#include <netinet/in.h>#include <pthread.h>#include <stdatomic.h>#include <errno.h>#include <unistd.h>#include <stdio.h>#include <string.h>#include <sys/socket.h>
Go to the source code of this file.
Macros | |
| #define | _POSIX_C_SOURCE 200809L |
| #define | REDIS_RATE_SLOTS 8 |
| #define | REDIS_RATE_PREFIX_MAX 128 |
| #define | REDIS_RATE_WRAPPER(N) |
Functions | |
| cwist_middleware_func | cwist_mw_rate_limit_ip_redis (const cwist_rate_limit_redis_cfg_t *cfg) |
| Shared per-IP 60-second window using one atomic Redis Lua command. | |
Optional process-shared Redis rate limiter.
Definition in file rate_limit_redis.c.
Definition at line 4 of file rate_limit_redis.c.
| #define REDIS_RATE_PREFIX_MAX 128 |
Definition at line 18 of file rate_limit_redis.c.
| #define REDIS_RATE_SLOTS 8 |
Definition at line 17 of file rate_limit_redis.c.
Definition at line 110 of file rate_limit_redis.c.
| cwist_middleware_func cwist_mw_rate_limit_ip_redis | ( | const cwist_rate_limit_redis_cfg_t * | cfg | ) |
Shared per-IP 60-second window using one atomic Redis Lua command.
Uses the socket peer, never forwarded headers. A rejected request returns 429 with Retry-After: 60. Backend errors and missing peer addresses follow fail_closed. The first backend failure per configuration in each process writes its error code and policy to stderr (no keys or credentials). No local fallback counter is used. Requests do not extend TTL. Use a dedicated pool on DB 0, with the default user and no AUTH.
Create the pool separately in every serving process after fork. Pool creation and DNS are blocking; timeout_ms bounds request-time pool wait and I/O. Use cwist_app_listen_ex() with one worker or CWIST_WORKERS=1, so the application listener does not fork the initialized pool. Do not destroy the pool while calls may still run. Eight distinct immutable configurations are supported per process; equal configurations reuse a slot.
Definition at line 129 of file rate_limit_redis.c.
References cap, cwist_rate_limit_redis_cfg_t::fail_closed, cwist_rate_limit_redis_cfg_t::key_prefix, cwist_rate_limit_redis_cfg_t::pool, REDIS_RATE_PREFIX_MAX, REDIS_RATE_SLOTS, cwist_rate_limit_redis_cfg_t::requests_per_minute, and cwist_rate_limit_redis_cfg_t::timeout_ms.