odyssey/core/od_route_pool.c

120 lines
2.4 KiB
C
Raw Normal View History

2016-11-11 09:40:58 +00:00
/*
* odissey.
*
* PostgreSQL connection pooler and request router.
*/
#include <stdlib.h>
2016-11-15 11:38:31 +00:00
#include <stdarg.h>
2016-11-11 09:40:58 +00:00
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
2016-11-25 12:38:52 +00:00
#include <machinarium.h>
2016-11-11 09:40:58 +00:00
#include <soprano.h>
#include "od_macro.h"
#include "od_list.h"
2016-11-28 13:03:09 +00:00
#include "od_pid.h"
2016-11-28 14:47:39 +00:00
#include "od_syslog.h"
2016-11-11 09:40:58 +00:00
#include "od_log.h"
#include "od_scheme.h"
#include "od_lex.h"
#include "od_config.h"
#include "od_server.h"
#include "od_server_pool.h"
2016-11-11 10:42:30 +00:00
#include "od_route_id.h"
2016-11-11 09:40:58 +00:00
#include "od_route.h"
#include "od_route_pool.h"
void od_routepool_init(od_routepool_t *pool)
2016-11-11 09:40:58 +00:00
{
od_listinit(&pool->list);
pool->count = 0;
}
void od_routepool_free(od_routepool_t *pool)
2016-11-11 09:40:58 +00:00
{
2016-11-29 13:19:12 +00:00
od_route_t *route;
2016-11-29 13:03:39 +00:00
od_list_t *i, *n;
2016-11-11 09:40:58 +00:00
od_listforeach_safe(&pool->list, i, n) {
2016-11-29 13:19:12 +00:00
route = od_container_of(i, od_route_t, link);
2016-11-11 09:40:58 +00:00
od_routefree(route);
}
}
2016-11-29 13:19:12 +00:00
od_route_t*
od_routepool_new(od_routepool_t *pool, od_schemeroute_t *scheme,
od_routeid_t *id)
2016-11-11 09:40:58 +00:00
{
2016-11-29 13:19:12 +00:00
od_route_t *route = od_routealloc();
2016-11-11 09:40:58 +00:00
if (route == NULL)
return NULL;
2016-11-11 10:42:30 +00:00
int rc;
rc = od_routeid_copy(&route->id, id);
if (rc == -1) {
od_routefree(route);
return NULL;
}
route->scheme = scheme;
2016-11-11 09:40:58 +00:00
od_listappend(&pool->list, &route->link);
pool->count++;
return route;
}
void od_routepool_unlink(od_routepool_t *pool, od_route_t *route)
2016-11-11 09:40:58 +00:00
{
assert(pool->count > 0);
pool->count--;
od_listunlink(&route->link);
od_routefree(route);
}
2016-11-29 13:19:12 +00:00
od_route_t*
od_routepool_match(od_routepool_t *pool, od_routeid_t *key)
2016-11-11 09:40:58 +00:00
{
2016-11-29 13:19:12 +00:00
od_route_t *route;
2016-11-29 13:03:39 +00:00
od_list_t *i;
2016-11-11 09:40:58 +00:00
od_listforeach(&pool->list, i) {
2016-11-29 13:19:12 +00:00
route = od_container_of(i, od_route_t, link);
2016-11-11 10:42:30 +00:00
if (od_routeid_compare(&route->id, key))
return route;
2016-11-11 09:40:58 +00:00
}
return NULL;
}
od_server_t*
od_routepool_next(od_routepool_t *pool, od_serverstate_t state)
{
2016-11-29 13:19:12 +00:00
od_route_t *route;
2016-11-29 13:03:39 +00:00
od_list_t *i, *n;
od_listforeach_safe(&pool->list, i, n) {
2016-11-29 13:19:12 +00:00
route = od_container_of(i, od_route_t, link);
od_server_t *server =
od_serverpool_next(&route->server_pool, state);
if (server)
return server;
}
return NULL;
}
od_server_t*
od_routepool_foreach(od_routepool_t *pool, od_serverstate_t state,
od_serverpool_cb_t callback,
void *arg)
{
2016-11-29 13:19:12 +00:00
od_route_t *route;
2016-11-29 13:03:39 +00:00
od_list_t *i, *n;
od_listforeach_safe(&pool->list, i, n) {
2016-11-29 13:19:12 +00:00
route = od_container_of(i, od_route_t, link);
od_server_t *server =
od_serverpool_foreach(&route->server_pool, state,
callback, arg);
if (server)
return server;
}
return NULL;
}