odyssey/core/od_pooler.c

146 lines
3.4 KiB
C
Raw Normal View History

2016-11-09 11:30:06 +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-09 11:30:06 +00:00
#include <stdint.h>
#include <stdio.h>
#include <string.h>
2016-11-25 12:38:52 +00:00
#include <machinarium.h>
#include <soprano.h>
2016-11-09 11:33:25 +00:00
2016-11-09 11:30:06 +00:00
#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-09 11:30:06 +00:00
#include "od_log.h"
#include "od_scheme.h"
#include "od_lex.h"
#include "od_config.h"
#include "od_server.h"
2016-11-09 12:01:48 +00:00
#include "od_server_pool.h"
2016-11-11 10:42:30 +00:00
#include "od_route_id.h"
#include "od_route.h"
#include "od_route_pool.h"
2016-11-09 11:52:09 +00:00
#include "od_client.h"
2016-11-09 12:08:55 +00:00
#include "od_client_pool.h"
2016-11-09 11:30:06 +00:00
#include "od.h"
#include "od_pooler.h"
#include "od_periodic.h"
2016-11-09 12:19:26 +00:00
#include "od_router.h"
2016-11-09 11:30:06 +00:00
2016-11-09 11:42:52 +00:00
static inline void
od_pooler(void *arg)
2016-11-09 11:30:06 +00:00
{
od_pooler_t *pooler = arg;
2016-11-09 12:19:26 +00:00
od_t *env = pooler->od;
/* resolve listen address and port */
char port[16];
snprintf(port, sizeof(port), "%d", env->scheme.port);
struct addrinfo *ai = NULL;
int rc;
rc = mm_getaddrinfo(pooler->server,
env->scheme.host, port, NULL, &ai, 0);
if (rc < 0) {
od_error(&env->log, NULL, "failed to resolve %s:%d",
env->scheme.host,
env->scheme.port);
return;
}
assert(ai != NULL);
/* bind to listen address and port */
rc = mm_bind(pooler->server, ai->ai_addr);
freeaddrinfo(ai);
if (rc < 0) {
od_error(&env->log, NULL, "bind %s:%d failed",
env->scheme.host,
env->scheme.port);
return;
}
/* starting periodic task scheduler fiber */
2016-11-25 12:38:52 +00:00
rc = mm_create(pooler->env, od_periodic, pooler);
if (rc < 0) {
od_error(&env->log, NULL, "failed to create periodic fiber");
return;
}
2016-12-20 09:35:09 +00:00
od_log(&env->log, NULL, "pooler started at %s:%d",
env->scheme.host, env->scheme.port);
2016-12-20 09:35:09 +00:00
od_log(&env->log, NULL, "");
/* accept loop */
2016-11-25 12:38:52 +00:00
while (mm_is_online(pooler->env))
2016-11-09 12:01:48 +00:00
{
mm_io_t client_io;
rc = mm_accept(pooler->server, env->scheme.backlog, &client_io);
if (rc < 0) {
od_error(&env->log, NULL, "accept failed");
continue;
}
if (pooler->client_pool.count >= env->scheme.client_max) {
od_log(&pooler->od->log, client_io,
"C: pooler client_max reached (%d), closing connection",
env->scheme.client_max);
mm_close(client_io);
continue;
}
mm_io_nodelay(client_io, env->scheme.nodelay);
if (env->scheme.keepalive > 0)
mm_io_keepalive(client_io, 1, env->scheme.keepalive);
od_client_t *client = od_clientpool_new(&pooler->client_pool);
2016-11-09 12:01:48 +00:00
if (client == NULL) {
od_error(&env->log, NULL, "failed to allocate client object");
2016-11-25 12:38:52 +00:00
mm_close(client_io);
2016-11-09 12:01:48 +00:00
continue;
}
client->id = pooler->client_seq++;
2016-11-09 12:19:26 +00:00
client->pooler = pooler;
2016-11-09 12:01:48 +00:00
client->io = client_io;
2016-11-25 12:38:52 +00:00
rc = mm_create(pooler->env, od_router, client);
2016-11-09 12:01:48 +00:00
if (rc < 0) {
od_error(&env->log, NULL, "failed to create client fiber");
2016-11-25 12:38:52 +00:00
mm_close(client_io);
2016-11-09 12:19:26 +00:00
od_clientpool_unlink(&pooler->client_pool, client);
2016-11-09 12:01:48 +00:00
continue;
}
}
2016-11-09 11:42:52 +00:00
}
int od_pooler_init(od_pooler_t *pooler, od_t *od)
2016-11-09 11:42:52 +00:00
{
2016-11-25 12:38:52 +00:00
pooler->env = mm_new();
2016-11-09 12:19:26 +00:00
if (pooler->env == NULL)
2016-11-09 11:42:52 +00:00
return -1;
2016-11-25 12:38:52 +00:00
pooler->server = mm_io_new(pooler->env);
2016-11-09 12:19:26 +00:00
if (pooler->server == NULL) {
2016-11-25 12:38:52 +00:00
mm_free(pooler->env);
2016-11-09 11:42:52 +00:00
return -1;
}
pooler->client_seq = 0;
2016-11-09 12:19:26 +00:00
pooler->od = od;
od_routepool_init(&pooler->route_pool);
2016-11-09 12:19:26 +00:00
od_clientpool_init(&pooler->client_pool);
2016-11-09 11:42:52 +00:00
return 0;
}
int od_pooler_start(od_pooler_t *pooler)
2016-11-09 11:42:52 +00:00
{
2016-11-09 12:01:48 +00:00
int rc;
2016-11-25 12:38:52 +00:00
rc = mm_create(pooler->env, od_pooler, pooler);
2016-11-09 12:01:48 +00:00
if (rc < 0) {
od_error(&pooler->od->log, NULL,
"failed to create pooler fiber");
2016-11-09 12:01:48 +00:00
return -1;
}
2016-11-25 12:38:52 +00:00
mm_start(pooler->env);
2016-11-09 11:30:06 +00:00
return 0;
}