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.
|
2017-05-25 12:00:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-08-28 14:43:46 +00:00
|
|
|
#include <ctype.h>
|
2017-05-31 15:47:15 +00:00
|
|
|
#include <inttypes.h>
|
2017-05-25 12:00:58 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <machinarium.h>
|
2018-08-28 14:43:46 +00:00
|
|
|
#include <kiwi.h>
|
|
|
|
#include <odyssey.h>
|
2017-05-25 12:00:58 +00:00
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
void
|
|
|
|
od_server_pool_init(od_server_pool_t *pool)
|
2017-05-25 12:00:58 +00:00
|
|
|
{
|
2017-05-29 15:15:18 +00:00
|
|
|
pool->count_idle = 0;
|
2017-05-25 12:00:58 +00:00
|
|
|
pool->count_active = 0;
|
|
|
|
pool->count_expire = 0;
|
|
|
|
od_list_init(&pool->idle);
|
2017-05-29 15:15:18 +00:00
|
|
|
od_list_init(&pool->active);
|
2017-05-25 12:00:58 +00:00
|
|
|
od_list_init(&pool->expire);
|
|
|
|
od_list_init(&pool->link);
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
void
|
|
|
|
od_server_pool_free(od_server_pool_t *pool)
|
2017-05-25 12:00:58 +00:00
|
|
|
{
|
|
|
|
od_server_t *server;
|
|
|
|
od_list_t *i, *n;
|
|
|
|
od_list_foreach_safe(&pool->idle, i, n) {
|
|
|
|
server = od_container_of(i, od_server_t, link);
|
|
|
|
od_server_free(server);
|
|
|
|
}
|
|
|
|
od_list_foreach_safe(&pool->expire, i, n) {
|
|
|
|
server = od_container_of(i, od_server_t, link);
|
|
|
|
od_server_free(server);
|
|
|
|
}
|
|
|
|
od_list_foreach_safe(&pool->active, i, n) {
|
|
|
|
server = od_container_of(i, od_server_t, link);
|
|
|
|
od_server_free(server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
void
|
|
|
|
od_server_pool_set(od_server_pool_t *pool, od_server_t *server,
|
|
|
|
od_server_state_t state)
|
2017-05-25 12:00:58 +00:00
|
|
|
{
|
|
|
|
if (server->state == state)
|
|
|
|
return;
|
|
|
|
switch (server->state) {
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_UNDEF:
|
2017-05-25 12:00:58 +00:00
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_EXPIRE:
|
2017-05-25 12:00:58 +00:00
|
|
|
pool->count_expire--;
|
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_IDLE:
|
2017-05-25 12:00:58 +00:00
|
|
|
pool->count_idle--;
|
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_ACTIVE:
|
2017-05-25 12:00:58 +00:00
|
|
|
pool->count_active--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
od_list_t *target = NULL;
|
|
|
|
switch (state) {
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_UNDEF:
|
2017-05-25 12:00:58 +00:00
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_EXPIRE:
|
2017-05-25 12:00:58 +00:00
|
|
|
target = &pool->expire;
|
|
|
|
pool->count_expire++;
|
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_IDLE:
|
2017-05-25 12:00:58 +00:00
|
|
|
target = &pool->idle;
|
|
|
|
pool->count_idle++;
|
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_ACTIVE:
|
2017-05-25 12:00:58 +00:00
|
|
|
target = &pool->active;
|
|
|
|
pool->count_active++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
od_list_unlink(&server->link);
|
|
|
|
od_list_init(&server->link);
|
|
|
|
if (target)
|
|
|
|
od_list_push(target, &server->link);
|
|
|
|
server->state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
od_server_t*
|
2018-08-28 14:43:46 +00:00
|
|
|
od_server_pool_next(od_server_pool_t *pool, od_server_state_t state)
|
2017-05-25 12:00:58 +00:00
|
|
|
{
|
2017-11-20 14:10:50 +00:00
|
|
|
int target_count = 0;
|
2017-05-25 12:00:58 +00:00
|
|
|
od_list_t *target = NULL;
|
|
|
|
switch (state) {
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_IDLE:
|
2017-11-20 14:10:50 +00:00
|
|
|
target_count = pool->count_idle;
|
|
|
|
target = &pool->idle;
|
2017-06-07 14:59:13 +00:00
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_EXPIRE:
|
2017-11-20 14:10:50 +00:00
|
|
|
target_count = pool->count_expire;
|
|
|
|
target = &pool->expire;
|
2017-05-25 12:00:58 +00:00
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_ACTIVE:
|
2017-11-20 14:10:50 +00:00
|
|
|
target_count = pool->count_active;
|
|
|
|
target = &pool->active;
|
2017-05-25 12:00:58 +00:00
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_UNDEF:
|
2017-11-20 14:10:50 +00:00
|
|
|
assert(0);
|
2017-05-25 12:00:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-11-20 14:10:50 +00:00
|
|
|
if (target_count == 0)
|
2017-05-25 12:00:58 +00:00
|
|
|
return NULL;
|
|
|
|
od_server_t *server;
|
|
|
|
server = od_container_of(target->next, od_server_t, link);
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
od_server_t*
|
2018-08-28 14:43:46 +00:00
|
|
|
od_server_pool_foreach(od_server_pool_t *pool, od_server_state_t state,
|
|
|
|
od_server_pool_cb_t callback,
|
|
|
|
void *arg)
|
2017-05-25 12:00:58 +00:00
|
|
|
{
|
|
|
|
od_list_t *target = NULL;
|
|
|
|
switch (state) {
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_IDLE: target = &pool->idle;
|
2017-06-07 14:59:13 +00:00
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_EXPIRE: target = &pool->expire;
|
2017-05-25 12:00:58 +00:00
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_ACTIVE: target = &pool->active;
|
2017-05-25 12:00:58 +00:00
|
|
|
break;
|
2018-08-28 14:43:46 +00:00
|
|
|
case OD_SERVER_UNDEF: assert(0);
|
2017-05-25 12:00:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
od_server_t *server;
|
|
|
|
od_list_t *i, *n;
|
|
|
|
od_list_foreach_safe(target, i, n) {
|
|
|
|
server = od_container_of(i, od_server_t, link);
|
|
|
|
int rc;
|
|
|
|
rc = callback(server, arg);
|
|
|
|
if (rc) {
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|