odyssey/sources/route.h

181 lines
4.1 KiB
C
Raw Normal View History

#ifndef ODYSSEY_ROUTE_H
#define ODYSSEY_ROUTE_H
/*
2018-03-12 14:03:15 +00:00
* Odyssey.
*
2018-04-04 13:19:58 +00:00
* Scalable PostgreSQL connection pooler.
*/
typedef struct od_route od_route_t;
struct od_route {
od_rule_t *rule;
od_route_id_t id;
2020-07-08 06:26:17 +00:00
od_stat_t stats;
od_stat_t stats_prev;
2020-07-08 06:26:17 +00:00
bool stats_mark_db;
od_server_pool_t server_pool;
od_client_pool_t client_pool;
kiwi_params_lock_t params;
2020-08-10 09:49:57 +00:00
int64_t tcp_connections;
machine_channel_t *wait_bus;
pthread_mutex_t lock;
2020-07-08 06:26:17 +00:00
2020-09-02 16:13:50 +00:00
od_error_logger_t *err_logger;
bool extra_logging_enabled;
2020-07-08 06:26:17 +00:00
od_list_t link;
};
static inline void od_route_init(od_route_t *route, bool extra_route_logging)
{
route->rule = NULL;
od_route_id_init(&route->id);
od_server_pool_init(&route->server_pool);
od_client_pool_init(&route->client_pool);
2020-07-08 06:26:17 +00:00
/* stat init */
route->stats_mark_db = false;
route->extra_logging_enabled = extra_route_logging;
if (extra_route_logging) {
2020-09-02 16:13:50 +00:00
/* error logging */
route->err_logger = od_err_logger_create_default();
} else {
2020-09-02 16:13:50 +00:00
route->err_logger = NULL;
}
2020-07-08 06:26:17 +00:00
od_stat_init(&route->stats);
od_stat_init(&route->stats_prev);
kiwi_params_lock_init(&route->params);
od_list_init(&route->link);
route->wait_bus = NULL;
pthread_mutex_init(&route->lock, NULL);
}
static inline void od_route_free(od_route_t *route)
{
od_route_id_free(&route->id);
od_server_pool_free(&route->server_pool);
kiwi_params_lock_free(&route->params);
if (route->wait_bus)
machine_channel_free(route->wait_bus);
2020-05-28 06:12:10 +00:00
if (route->stats.enable_quantiles) {
od_stat_free(&route->stats);
free(route->rule->quantiles);
2020-05-28 06:12:10 +00:00
}
if (route->extra_logging_enabled) {
2020-09-02 16:13:50 +00:00
od_err_logger_free(route->err_logger);
route->err_logger = NULL;
}
pthread_mutex_destroy(&route->lock);
free(route);
}
static inline od_route_t *od_route_allocate()
{
od_route_t *route = malloc(sizeof(*route));
if (route == NULL)
return NULL;
2020-09-02 16:13:50 +00:00
od_route_init(route, true);
route->wait_bus = machine_channel_create();
if (route->wait_bus == NULL) {
od_route_free(route);
return NULL;
}
return route;
}
static inline void od_route_lock(od_route_t *route)
{
pthread_mutex_lock(&route->lock);
}
static inline void od_route_unlock(od_route_t *route)
{
pthread_mutex_unlock(&route->lock);
}
static inline int od_route_is_dynamic(od_route_t *route)
2018-03-21 14:42:58 +00:00
{
return route->rule->db_is_default || route->rule->user_is_default;
2018-03-21 14:42:58 +00:00
}
static inline int od_route_match_compare_client_cb(od_client_t *client,
void **argv)
{
2019-01-30 09:30:31 +00:00
return od_id_cmp(&client->id, argv[0]);
}
static inline od_client_t *od_route_match_client(od_route_t *route, od_id_t *id)
{
void *argv[] = { id };
od_client_t *match;
match = od_client_pool_foreach(&route->client_pool, OD_CLIENT_ACTIVE,
od_route_match_compare_client_cb, argv);
if (match)
return match;
match = od_client_pool_foreach(&route->client_pool, OD_CLIENT_QUEUE,
od_route_match_compare_client_cb, argv);
if (match)
return match;
match = od_client_pool_foreach(&route->client_pool, OD_CLIENT_PENDING,
od_route_match_compare_client_cb, argv);
if (match)
return match;
return NULL;
}
static inline void od_route_kill_client(od_route_t *route, od_id_t *id)
{
od_client_t *client;
client = od_route_match_client(route, id);
if (client)
od_client_kill(client);
}
static inline int od_route_kill_cb(od_client_t *client, void **argv)
{
(void)argv;
od_client_kill(client);
return 0;
}
static inline void od_route_kill_client_pool(od_route_t *route)
{
od_client_pool_foreach(&route->client_pool, OD_CLIENT_ACTIVE,
od_route_kill_cb, NULL);
od_client_pool_foreach(&route->client_pool, OD_CLIENT_PENDING,
od_route_kill_cb, NULL);
od_client_pool_foreach(&route->client_pool, OD_CLIENT_QUEUE,
od_route_kill_cb, NULL);
}
static inline int od_route_wait(od_route_t *route, uint32_t time_ms)
{
machine_msg_t *msg;
msg = machine_channel_read(route->wait_bus, time_ms);
if (msg) {
machine_msg_free(msg);
return 0;
}
return -1;
}
static inline int od_route_signal(od_route_t *route)
{
machine_msg_t *msg;
msg = machine_msg_create(0);
if (msg == NULL)
return -1;
machine_channel_write(route->wait_bus, msg);
return 0;
}
#endif /* ODYSSEY_ROUTE_H */