75 log_error(
"Failed to allocate rate limiter");
92 log_error(
"Failed to allocate rate limiter");
129 if (!limiter || !limiter->
ops || !limiter->
ops->
check) {
133 if (!ip_address || !allowed) {
145 if (!limiter || !limiter->
ops || !limiter->
ops->
record) {
177 clock_gettime(CLOCK_REALTIME, &ts);
188 return event_type_strings[event_type];
unsigned long long uint64_t
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
asciichat_error_t
Error and exit codes - unified status values (0-255)
#define log_error(...)
Log an ERROR message.
📝 Logging API with multiple log levels and terminal output control
void * memory_backend_create(void)
Create memory backend instance.
const rate_limiter_backend_ops_t memory_backend_ops
Memory backend operations vtable.
🧠 In-memory rate limiting backend interface
asciichat_error_t rate_limiter_cleanup(rate_limiter_t *limiter, uint32_t max_age_secs)
Clean up old rate limit events.
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.
void rate_limiter_destroy(rate_limiter_t *limiter)
Destroy rate limiter and free resources.
void rate_limiter_set_sqlite_db(rate_limiter_t *limiter, void *db)
Set SQLite database handle for rate limiter.
rate_limiter_t * rate_limiter_create_memory(void)
Create in-memory rate limiter.
uint64_t rate_limiter_get_time_ms(void)
Get current time in milliseconds.
const char * rate_limiter_event_type_string(rate_event_type_t event_type)
Get event type string for logging.
rate_limiter_t * rate_limiter_create_sqlite(const char *db_path)
Create SQLite-backed rate limiter.
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.
const rate_limit_config_t DEFAULT_RATE_LIMITS[RATE_EVENT_MAX]
Default rate limits for each event type.
🚦 Rate limiting API with pluggable backends
rate_event_type_t
Rate limit event types.
@ RATE_EVENT_SESSION_LOOKUP
Session lookup.
@ RATE_EVENT_CONTROL
Control packets (CAPABILITIES, STREAM_START/STOP, LEAVE)
@ RATE_EVENT_SESSION_JOIN
Session join.
@ RATE_EVENT_CONNECTION
New connection.
@ RATE_EVENT_PING
Ping/pong keepalive (PACKET_TYPE_PING, PACKET_TYPE_PONG)
@ RATE_EVENT_SESSION_CREATE
Session creation.
@ RATE_EVENT_AUDIO
Audio packet (PACKET_TYPE_AUDIO, PACKET_TYPE_AUDIO_BATCH)
@ RATE_EVENT_MAX
Sentinel value.
@ RATE_EVENT_IMAGE_FRAME
Image frame from client (PACKET_TYPE_IMAGE_FRAME)
@ RATE_EVENT_CLIENT_JOIN
Client join request (PACKET_TYPE_CLIENT_JOIN)
void sqlite_backend_set_db(void *backend_data, sqlite3 *db)
Set SQLite database handle for backend.
void * sqlite_backend_create(const char *db_path)
Create SQLite backend instance.
const rate_limiter_backend_ops_t sqlite_backend_ops
SQLite backend operations vtable.
💾 SQLite rate limiting backend interface
Rate limit configuration.
uint32_t max_events
Maximum events allowed.
Backend operations vtable.
void(* destroy)(void *backend_data)
asciichat_error_t(* cleanup)(void *backend_data, uint32_t max_age_secs)
asciichat_error_t(* record)(void *backend_data, const char *ip_address, rate_event_type_t event_type)
asciichat_error_t(* check)(void *backend_data, const char *ip_address, rate_event_type_t event_type, const rate_limit_config_t *config, bool *allowed)
void * backend_data
Backend-specific data.
const rate_limiter_backend_ops_t * ops
Backend operations.
⏱️ High-precision timing utilities using sokol_time.h and uthash