2018-08-28 14:43:46 +00:00
|
|
|
#ifndef ODYSSEY_SERVER_POOL_H
|
|
|
|
#define ODYSSEY_SERVER_POOL_H
|
2017-05-25 12:00:58 +00:00
|
|
|
|
|
|
|
/*
|
2018-03-12 14:03:15 +00:00
|
|
|
* Odyssey.
|
2017-05-25 12:00:58 +00:00
|
|
|
*
|
2018-04-04 13:19:58 +00:00
|
|
|
* Scalable PostgreSQL connection pooler.
|
2020-04-02 11:00:56 +00:00
|
|
|
*/
|
2017-05-25 12:00:58 +00:00
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
typedef struct od_server_pool od_server_pool_t;
|
2017-05-25 12:00:58 +00:00
|
|
|
|
2020-04-02 11:00:56 +00:00
|
|
|
typedef int (*od_server_pool_cb_t)(od_server_t *, void **);
|
2017-05-25 12:00:58 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
struct od_server_pool {
|
2017-05-25 12:00:58 +00:00
|
|
|
od_list_t active;
|
|
|
|
od_list_t idle;
|
2020-04-02 11:00:56 +00:00
|
|
|
int count_active;
|
|
|
|
int count_idle;
|
2017-05-25 12:00:58 +00:00
|
|
|
};
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
static inline void od_server_pool_init(od_server_pool_t *pool)
|
2018-12-06 14:23:15 +00:00
|
|
|
{
|
|
|
|
pool->count_active = 0;
|
2020-12-28 10:43:31 +00:00
|
|
|
pool->count_idle = 0;
|
2018-12-06 14:23:15 +00:00
|
|
|
od_list_init(&pool->idle);
|
|
|
|
od_list_init(&pool->active);
|
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
static inline void od_server_pool_free(od_server_pool_t *pool)
|
2018-12-06 14:23:15 +00:00
|
|
|
{
|
|
|
|
od_server_t *server;
|
|
|
|
od_list_t *i, *n;
|
2020-04-02 11:00:56 +00:00
|
|
|
od_list_foreach_safe(&pool->idle, i, n)
|
|
|
|
{
|
2018-12-06 14:23:15 +00:00
|
|
|
server = od_container_of(i, od_server_t, link);
|
|
|
|
od_server_free(server);
|
|
|
|
}
|
2020-04-02 11:00:56 +00:00
|
|
|
od_list_foreach_safe(&pool->active, i, n)
|
|
|
|
{
|
2018-12-06 14:23:15 +00:00
|
|
|
server = od_container_of(i, od_server_t, link);
|
|
|
|
od_server_free(server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
static inline void od_server_pool_set(od_server_pool_t *pool,
|
|
|
|
od_server_t *server,
|
|
|
|
od_server_state_t state)
|
2018-12-06 14:23:15 +00:00
|
|
|
{
|
|
|
|
if (server->state == state)
|
|
|
|
return;
|
|
|
|
switch (server->state) {
|
2020-12-28 10:43:31 +00:00
|
|
|
case OD_SERVER_UNDEF:
|
|
|
|
break;
|
|
|
|
case OD_SERVER_IDLE:
|
|
|
|
pool->count_idle--;
|
|
|
|
break;
|
|
|
|
case OD_SERVER_ACTIVE:
|
|
|
|
pool->count_active--;
|
|
|
|
break;
|
2018-12-06 14:23:15 +00:00
|
|
|
}
|
|
|
|
od_list_t *target = NULL;
|
|
|
|
switch (state) {
|
2020-12-28 10:43:31 +00:00
|
|
|
case OD_SERVER_UNDEF:
|
|
|
|
break;
|
|
|
|
case OD_SERVER_IDLE:
|
|
|
|
target = &pool->idle;
|
|
|
|
pool->count_idle++;
|
|
|
|
break;
|
|
|
|
case OD_SERVER_ACTIVE:
|
|
|
|
target = &pool->active;
|
|
|
|
pool->count_active++;
|
|
|
|
break;
|
2018-12-06 14:23:15 +00:00
|
|
|
}
|
|
|
|
od_list_unlink(&server->link);
|
|
|
|
od_list_init(&server->link);
|
|
|
|
if (target)
|
|
|
|
od_list_push(target, &server->link);
|
|
|
|
server->state = state;
|
|
|
|
}
|
2017-05-25 12:00:58 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
static inline od_server_t *od_server_pool_next(od_server_pool_t *pool,
|
|
|
|
od_server_state_t state)
|
2018-12-06 14:23:15 +00:00
|
|
|
{
|
2020-12-28 10:43:31 +00:00
|
|
|
int target_count = 0;
|
2018-12-06 14:23:15 +00:00
|
|
|
od_list_t *target = NULL;
|
|
|
|
switch (state) {
|
2020-12-28 10:43:31 +00:00
|
|
|
case OD_SERVER_IDLE:
|
|
|
|
target_count = pool->count_idle;
|
|
|
|
target = &pool->idle;
|
|
|
|
break;
|
|
|
|
case OD_SERVER_ACTIVE:
|
|
|
|
target_count = pool->count_active;
|
|
|
|
target = &pool->active;
|
|
|
|
break;
|
|
|
|
case OD_SERVER_UNDEF:
|
|
|
|
assert(0);
|
|
|
|
break;
|
2018-12-06 14:23:15 +00:00
|
|
|
}
|
|
|
|
if (target_count == 0)
|
|
|
|
return NULL;
|
|
|
|
od_server_t *server;
|
|
|
|
server = od_container_of(target->next, od_server_t, link);
|
|
|
|
return server;
|
|
|
|
}
|
2017-05-25 12:00:58 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
static inline od_server_t *od_server_pool_foreach(od_server_pool_t *pool,
|
|
|
|
od_server_state_t state,
|
|
|
|
od_server_pool_cb_t callback,
|
|
|
|
void **argv)
|
2018-12-06 14:23:15 +00:00
|
|
|
{
|
|
|
|
od_list_t *target = NULL;
|
|
|
|
switch (state) {
|
2020-12-28 10:43:31 +00:00
|
|
|
case OD_SERVER_IDLE:
|
|
|
|
target = &pool->idle;
|
|
|
|
break;
|
|
|
|
case OD_SERVER_ACTIVE:
|
|
|
|
target = &pool->active;
|
|
|
|
break;
|
|
|
|
case OD_SERVER_UNDEF:
|
|
|
|
assert(0);
|
|
|
|
break;
|
2018-12-06 14:23:15 +00:00
|
|
|
}
|
|
|
|
od_server_t *server;
|
|
|
|
od_list_t *i, *n;
|
2020-04-02 11:00:56 +00:00
|
|
|
od_list_foreach_safe(target, i, n)
|
|
|
|
{
|
2018-12-06 14:23:15 +00:00
|
|
|
server = od_container_of(i, od_server_t, link);
|
|
|
|
int rc;
|
|
|
|
rc = callback(server, argv);
|
|
|
|
if (rc) {
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-25 12:00:58 +00:00
|
|
|
|
2020-12-28 10:43:31 +00:00
|
|
|
static inline int od_server_pool_total(od_server_pool_t *pool)
|
2018-08-28 14:43:46 +00:00
|
|
|
{
|
2018-12-06 14:23:15 +00:00
|
|
|
return pool->count_active + pool->count_idle;
|
2017-05-31 13:48:44 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
#endif /* ODYSSEY_SERVER_POOL_H */
|