KEEL 1.0.0
Minimal C11 HTTP client/server library built on epoll/kqueue/io_uring/poll
Loading...
Searching...
No Matches
client_pool.h
Go to the documentation of this file.
1
13#ifndef KEEL_CLIENT_POOL_H
14#define KEEL_CLIENT_POOL_H
15
16#include <keel/allocator.h>
17#include <keel/client.h>
18#include <keel/error.h>
19#include <keel/event_ctx.h>
20#include <keel/tls.h>
21#include <stdint.h>
22
23/* ── Defaults ────────────────────────────────────────────────────── */
24
26#define KL_CPOOL_DEFAULT_CAPACITY 32
28#define KL_CPOOL_DEFAULT_MAX_PER_HOST 4
30#define KL_CPOOL_DEFAULT_IDLE_MS 60000
32/* ── Types ───────────────────────────────────────────────────────── */
33
35
36typedef struct {
39 uint64_t idle_ms;
41
48typedef struct {
49 int fd;
51 int reused;
52 void *_entry;
54
70
81
82/* ── Pool lifecycle ──────────────────────────────────────────────── */
83
93 KlAllocator *alloc, KlEventCtx *ev_ctx);
94
99
100/* ── Acquire / release ───────────────────────────────────────────── */
101
113int kl_cpool_acquire(KlClientPool *pool, const char *host, int port,
114 int is_tls, const char *proxy_host, int proxy_port,
115 KlClientPoolConn *conn);
116
122 const char *host, int port, int is_tls,
123 const char *proxy_host, int proxy_port);
124
129
130/* ── Maintenance ─────────────────────────────────────────────────── */
131
137
142
146int kl_cpool_host_count(const KlClientPool *pool, const char *host,
147 int port, int is_tls,
148 const char *proxy_host, int proxy_port);
149
150/* ── Pooled client request variants ──────────────────────────────── */
151
159 KlAllocator *alloc, const KlClientConfig *cfg,
160 const char *method, const char *url,
161 const KlClientHeader *headers, int num_headers,
162 const char *body, size_t body_len,
163 KlClientResponse *resp);
164
173 KlEventCtx *ev_ctx, KlAllocator *alloc,
174 const KlClientConfig *cfg,
175 const char *method, const char *url,
176 const KlClientHeader *headers, int num_headers,
177 const char *body, size_t body_len,
178 KlClientDoneFn on_done, void *user_data);
179
180#endif /* KEEL_CLIENT_POOL_H */
HTTP/1.1 client (sync + async).
#define KL_CLIENT_HOSTNAME_MAX
Maximum hostname length.
Definition client.h:27
void(* KlClientDoneFn)(KlClient *client, void *user_data)
Callback invoked when an async HTTP request completes.
Definition client.h:193
struct KlClient KlClient
Definition client.h:185
int kl_cpool_acquire(KlClientPool *pool, const char *host, int port, int is_tls, const char *proxy_host, int proxy_port, KlClientPoolConn *conn)
Try to acquire an idle connection from the pool.
void kl_cpool_discard(KlClientPool *pool, KlClientPoolConn *conn)
Close and discard a connection (not returned to pool).
int kl_cpool_evict_expired(KlClientPool *pool)
Evict expired idle connections (for sync-only pools without timers).
int kl_cpool_idle_count(const KlClientPool *pool)
Count of idle connections in the pool.
int kl_cpool_host_count(const KlClientPool *pool, const char *host, int port, int is_tls, const char *proxy_host, int proxy_port)
Count idle connections for a specific host tuple.
KlClient * kl_client_start_pooled(KlClientPool *pool, KlEventCtx *ev_ctx, KlAllocator *alloc, const KlClientConfig *cfg, const char *method, const char *url, const KlClientHeader *headers, int num_headers, const char *body, size_t body_len, KlClientDoneFn on_done, void *user_data)
Asynchronous HTTP request with connection pooling.
int kl_client_request_pooled(KlClientPool *pool, KlAllocator *alloc, const KlClientConfig *cfg, const char *method, const char *url, const KlClientHeader *headers, int num_headers, const char *body, size_t body_len, KlClientResponse *resp)
Synchronous HTTP request with connection pooling.
int kl_cpool_init(KlClientPool *pool, const KlClientPoolConfig *cfg, KlAllocator *alloc, KlEventCtx *ev_ctx)
Initialize a connection pool.
int kl_cpool_release(KlClientPool *pool, KlClientPoolConn *conn, const char *host, int port, int is_tls, const char *proxy_host, int proxy_port)
Return a connection to the pool for reuse.
void kl_cpool_free(KlClientPool *pool)
Free all pool resources (closes all idle connections).
KlError
Diagnostic error codes for Keel public functions.
Definition error.h:11
Bring-your-own allocator vtable.
Definition allocator.h:12
Definition client.h:73
Definition client.h:58
Definition client_pool.h:36
int capacity
Definition client_pool.h:37
int max_per_host
Definition client_pool.h:38
uint64_t idle_ms
Definition client_pool.h:39
Acquired connection handle.
Definition client_pool.h:48
int fd
Definition client_pool.h:49
void * _entry
Definition client_pool.h:52
KlTls * tls
Definition client_pool.h:50
int reused
Definition client_pool.h:51
Pool entry (internal, stored in flat array).
Definition client_pool.h:58
char host[KL_CLIENT_HOSTNAME_MAX]
Definition client_pool.h:59
struct KlClientPool * pool
Definition client_pool.h:68
char proxy_host[KL_CLIENT_HOSTNAME_MAX]
Definition client_pool.h:62
int fd
Definition client_pool.h:64
KlTls * tls
Definition client_pool.h:65
int is_tls
Definition client_pool.h:61
uint64_t idle_since_ms
Definition client_pool.h:66
int proxy_port
Definition client_pool.h:63
int port
Definition client_pool.h:60
int64_t timer_id
Definition client_pool.h:67
Definition client_pool.h:71
KlAllocator * alloc
Definition client_pool.h:77
KlError last_error
Definition client_pool.h:79
int max_per_host
Definition client_pool.h:75
int active
Definition client_pool.h:74
int capacity
Definition client_pool.h:73
KlClientPoolEntry * entries
Definition client_pool.h:72
uint64_t idle_ms
Definition client_pool.h:76
KlEventCtx * ev_ctx
Definition client_pool.h:78
Definition client.h:63
Composable event loop context.
Definition event_ctx.h:42
Definition tls.h:27