ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
memory.c File Reference

🧠 In-memory rate limiting backend using uthash More...

Go to the source code of this file.

Data Structures

struct  rate_event_s
 The useful part of memory backend is in events, lock: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path. More...
struct  memory_backend_t
 Memory backend data. More...

Macros

#define NETWORK_RATE_LIMIT_MEMORY_KEY_BUFFER_SIZE   256
#define RATE_LIMIT_INITIAL_EVENT_CAPACITY   100

Typedefs

typedef struct rate_event_s rate_event_t
 The useful part of memory backend is in events, lock: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path.

Functions

void * memory_backend_create (void)
 Create memory backend instance.

Variables

const rate_limiter_backend_ops_t memory_backend_ops
 Memory backend operations vtable.

Detailed Description

🧠 In-memory rate limiting backend using uthash

Thread-safe implementation for ascii-chat server where persistence is not needed.

Definition in file memory.c.

Macro Definition Documentation

◆ NETWORK_RATE_LIMIT_MEMORY_KEY_BUFFER_SIZE

#define NETWORK_RATE_LIMIT_MEMORY_KEY_BUFFER_SIZE   256

Definition at line 9 of file memory.c.

◆ RATE_LIMIT_INITIAL_EVENT_CAPACITY

#define RATE_LIMIT_INITIAL_EVENT_CAPACITY   100

Definition at line 12 of file memory.c.

Typedef Documentation

◆ rate_event_t

typedef struct rate_event_s rate_event_t

The useful part of memory backend is in events, lock: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path.

Per-address, per-event timestamp ring used by the in-memory limiter

Per-address, per-event timestamp ring used by the in-memory limiter.

key combines the client address and event type; timestamps stores the recent event times, while count, capacity, and head manage the circular window. The hash handle lets the backend find this record quickly.

Function Documentation

◆ memory_backend_create()

void * memory_backend_create ( void )

Create memory backend instance.

Returns
Backend instance or NULL on failure

Definition at line 256 of file memory.c.

256 {
258 if (!backend) {
259 log_error("Failed to allocate memory backend");
260 return NULL;
261 }
262
263 defer_named(release_backend, SAFE_FREE(backend));
264 memset(backend, 0, sizeof(*backend));
265
266 if (mutex_init(&backend->lock, "rate_limiter") != 0) {
267 log_error("Failed to initialize mutex");
268 return NULL;
269 }
270
271 log_debug("Memory rate limiter backend initialized");
272 defer_cancel(release_backend);
273 return backend;
274}
#define SAFE_FREE(ptr)
Definition common.h:377
#define SAFE_MALLOC(size, cast)
Definition common.h:265
#define log_error(...)
Log an ERROR message.
Definition log/log.h:634
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595
Memory backend data.
Definition memory.c:49
mutex_t lock
Mutex for thread safety.
Definition memory.c:51

References memory_backend_t::lock, log_debug, log_error, SAFE_FREE, and SAFE_MALLOC.

Referenced by rate_limiter_create_memory().

Variable Documentation

◆ memory_backend_ops

const rate_limiter_backend_ops_t memory_backend_ops
Initial value:
= {
.check = memory_check,
.record = memory_record,
.cleanup = memory_cleanup,
.destroy = memory_destroy,
}

Memory backend operations vtable.

Definition at line 276 of file memory.c.

276 {
277 .check = memory_check,
278 .record = memory_record,
279 .cleanup = memory_cleanup,
280 .destroy = memory_destroy,
281};

Referenced by rate_limiter_create_memory().