|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
🚦 Rate limiting API with pluggable backends More...
Go to the source code of this file.
Data Structures | |
| struct | rate_limit_config_t |
| Rate limit configuration. More... | |
| struct | rate_limiter_backend_ops_t |
| Backend operations vtable. More... | |
Typedefs | |
| typedef struct rate_limiter_s | rate_limiter_t |
| Opaque rate limiter handle. | |
Enumerations | |
| enum | rate_event_type_t { RATE_EVENT_SESSION_CREATE = 0 , RATE_EVENT_SESSION_LOOKUP = 1 , RATE_EVENT_SESSION_JOIN = 2 , RATE_EVENT_CONNECTION = 3 , RATE_EVENT_IMAGE_FRAME = 4 , RATE_EVENT_AUDIO = 5 , RATE_EVENT_PING = 6 , RATE_EVENT_CLIENT_JOIN = 7 , RATE_EVENT_CONTROL = 8 , RATE_EVENT_MAX } |
| Rate limit event types. More... | |
Functions | |
| rate_limiter_t * | rate_limiter_create_memory (void) |
| Create in-memory rate limiter. | |
| rate_limiter_t * | rate_limiter_create_sqlite (const char *db_path) |
| Create SQLite-backed rate limiter. | |
| void | rate_limiter_set_sqlite_db (rate_limiter_t *limiter, void *db) |
| Set SQLite database handle for rate limiter. | |
| void | rate_limiter_destroy (rate_limiter_t *limiter) |
| Destroy rate limiter and free resources. | |
| asciichat_error_t | rate_limiter_check (rate_limiter_t *limiter, const char *ip_address, rate_event_type_t event_type, const rate_limit_config_t *config, bool *allowed) |
| Check if an event from an IP address should be rate limited. | |
| asciichat_error_t | rate_limiter_record (rate_limiter_t *limiter, const char *ip_address, rate_event_type_t event_type) |
| Record a rate limit event. | |
| asciichat_error_t | rate_limiter_cleanup (rate_limiter_t *limiter, uint32_t max_age_secs) |
| Clean up old rate limit events. | |
Variables | |
| const rate_limit_config_t | DEFAULT_RATE_LIMITS [RATE_EVENT_MAX] |
| Default rate limits for each event type. | |
🚦 Rate limiting API with pluggable backends
Supports two backends:
Example usage:
Definition in file rate_limit.h.
| typedef struct rate_limiter_s rate_limiter_t |
Opaque rate limiter handle.
Backend implementation is hidden from users.
Definition at line 76 of file rate_limit.h.
| enum rate_event_type_t |
Rate limit event types.
Definition at line 46 of file rate_limit.h.
| asciichat_error_t rate_limiter_check | ( | rate_limiter_t * | limiter, |
| const char * | ip_address, | ||
| rate_event_type_t | event_type, | ||
| const rate_limit_config_t * | config, | ||
| bool * | allowed | ||
| ) |
Check if an event from an IP address should be rate limited.
Uses sliding window: counts events in the last window_secs seconds.
| limiter | Rate limiter instance | |
| ip_address | IP address string (IPv4 or IPv6) | |
| event_type | Type of event | |
| config | Rate limit configuration (NULL = use defaults) | |
| [out] | allowed | True if event is allowed, false if rate limited |
Definition at line 127 of file rate_limit.c.
References rate_limiter_s::backend_data, rate_limiter_backend_ops_t::check, ERROR_INVALID_PARAM, rate_limiter_s::ops, RATE_EVENT_MAX, and SET_ERRNO.
Referenced by check_and_record_rate_limit().
| asciichat_error_t rate_limiter_cleanup | ( | rate_limiter_t * | limiter, |
| uint32_t | max_age_secs | ||
| ) |
Clean up old rate limit events.
Deletes events older than the specified age to prevent unbounded growth. Should be called periodically (e.g., every 5 minutes).
| limiter | Rate limiter instance |
| max_age_secs | Delete events older than this (0 = use default 1 hour) |
Definition at line 160 of file rate_limit.c.
References rate_limiter_s::backend_data, rate_limiter_backend_ops_t::cleanup, ERROR_INVALID_PARAM, rate_limiter_s::ops, and SET_ERRNO.
| rate_limiter_t * rate_limiter_create_memory | ( | void | ) |
Create in-memory rate limiter.
Thread-safe implementation using uthash and mutexes. Suitable for ascii-chat server where persistence is not needed.
Definition at line 72 of file rate_limit.c.
References rate_limiter_s::backend_data, log_error, memory_backend_create(), memory_backend_ops, and rate_limiter_s::ops.
Referenced by server_main().
| rate_limiter_t * rate_limiter_create_sqlite | ( | const char * | db_path | ) |
Create SQLite-backed rate limiter.
Persistent implementation using SQLite database. Suitable for acds discovery server where persistence is needed.
| db_path | Path to SQLite database (NULL = externally managed database) |
Definition at line 89 of file rate_limit.c.
References rate_limiter_s::backend_data, log_error, rate_limiter_s::ops, sqlite_backend_create(), and sqlite_backend_ops.
Referenced by acds_server_init().
| void rate_limiter_destroy | ( | rate_limiter_t * | limiter | ) |
Destroy rate limiter and free resources.
| limiter | Rate limiter instance (NULL-safe) |
Definition at line 115 of file rate_limit.c.
References rate_limiter_s::backend_data, rate_limiter_backend_ops_t::destroy, and rate_limiter_s::ops.
Referenced by acds_server_init(), acds_server_shutdown(), and server_main().
| asciichat_error_t rate_limiter_record | ( | rate_limiter_t * | limiter, |
| const char * | ip_address, | ||
| rate_event_type_t | event_type | ||
| ) |
Record a rate limit event.
Should be called after rate_limiter_check() returns allowed=true.
| limiter | Rate limiter instance |
| ip_address | IP address string |
| event_type | Type of event |
Definition at line 144 of file rate_limit.c.
References rate_limiter_s::backend_data, ERROR_INVALID_PARAM, rate_limiter_s::ops, RATE_EVENT_MAX, rate_limiter_backend_ops_t::record, and SET_ERRNO.
Referenced by check_and_record_rate_limit().
| void rate_limiter_set_sqlite_db | ( | rate_limiter_t * | limiter, |
| void * | db | ||
| ) |
Set SQLite database handle for rate limiter.
For SQLite-backed rate limiters where the database lifecycle is managed externally (e.g., ACDS manages its own database). Must be called after rate_limiter_create_sqlite(NULL).
| limiter | Rate limiter instance (must be SQLite backend) |
| db | SQLite database handle |
Definition at line 106 of file rate_limit.c.
References rate_limiter_s::backend_data, and sqlite_backend_set_db().
Referenced by acds_server_init().
|
extern |
Default rate limits for each event type.
Definition at line 29 of file rate_limit.c.