odyssey/sources/relay.c

117 lines
2.6 KiB
C
Raw Normal View History

2017-05-26 11:49:17 +00:00
/*
2017-07-05 12:42:49 +00:00
* Odissey.
2017-05-26 11:49:17 +00:00
*
2017-07-05 12:42:49 +00:00
* Advanced PostgreSQL connection pooler.
2017-05-26 11:49:17 +00:00
*/
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
2017-05-31 15:47:15 +00:00
#include <inttypes.h>
2017-05-26 11:49:17 +00:00
#include <signal.h>
#include <machinarium.h>
2017-06-07 11:50:58 +00:00
#include <shapito.h>
2017-05-26 11:49:17 +00:00
#include "sources/macro.h"
#include "sources/version.h"
2017-08-08 13:50:50 +00:00
#include "sources/atomic.h"
#include "sources/list.h"
#include "sources/pid.h"
#include "sources/id.h"
2017-07-26 14:05:29 +00:00
#include "sources/log_file.h"
#include "sources/log_system.h"
#include "sources/logger.h"
#include "sources/daemon.h"
#include "sources/scheme.h"
#include "sources/scheme_mgr.h"
#include "sources/config.h"
#include "sources/msg.h"
#include "sources/system.h"
#include "sources/server.h"
#include "sources/server_pool.h"
#include "sources/client.h"
#include "sources/client_pool.h"
#include "sources/route_id.h"
#include "sources/route.h"
#include "sources/route_pool.h"
2017-09-15 12:58:29 +00:00
#include "sources/instance.h"
#include "sources/router.h"
#include "sources/pooler.h"
#include "sources/relay.h"
#include "sources/frontend.h"
2017-05-26 11:49:17 +00:00
static inline void
od_relay(void *arg)
{
od_relay_t *relay = arg;
od_instance_t *instance = relay->system->instance;
2017-05-26 11:49:17 +00:00
for (;;)
{
2017-06-13 11:57:54 +00:00
machine_msg_t *msg;
msg = machine_queue_get(relay->task_queue, UINT32_MAX);
2017-05-26 11:49:17 +00:00
if (msg == NULL)
break;
od_msg_t msg_type;
msg_type = machine_msg_get_type(msg);
switch (msg_type) {
case OD_MCLIENT_NEW:
{
od_client_t *client;
client = *(od_client_t**)machine_msg_get_data(msg);
2017-05-27 14:21:39 +00:00
client->system = relay->system;
2017-05-26 11:49:17 +00:00
int64_t coroutine_id;
coroutine_id = machine_coroutine_create(od_frontend, client);
2017-05-26 11:49:17 +00:00
if (coroutine_id == -1) {
2017-07-26 14:05:29 +00:00
od_error_client(&instance->logger, &client->id, "relay",
2017-05-31 15:47:15 +00:00
"failed to create coroutine");
2017-05-26 11:49:17 +00:00
machine_close(client->io);
od_client_free(client);
break;
}
client->coroutine_id = coroutine_id;
break;
}
default:
assert(0);
break;
2017-05-26 11:49:17 +00:00
}
2017-05-26 11:49:17 +00:00
machine_msg_free(msg);
}
2017-07-26 14:05:29 +00:00
od_log(&instance->logger, "relay: stopped");
2017-05-26 11:49:17 +00:00
}
2017-06-01 12:45:49 +00:00
void od_relay_init(od_relay_t *relay, od_system_t *system, int id)
2017-05-26 11:49:17 +00:00
{
relay->machine = -1;
2017-06-01 12:45:49 +00:00
relay->id = id;
relay->system = system;
2017-05-26 11:49:17 +00:00
}
int od_relay_start(od_relay_t *relay)
{
od_instance_t *instance = relay->system->instance;
relay->task_queue = machine_queue_create();
if (relay->task_queue == NULL) {
2017-07-26 14:05:29 +00:00
od_error(&instance->logger, "relay", "failed to create task queue");
return -1;
}
2017-06-01 12:45:49 +00:00
char name[32];
snprintf(name, sizeof(name), "relay: %d", relay->id);
relay->machine = machine_create(name, od_relay, relay);
2017-05-26 11:49:17 +00:00
if (relay->machine == -1) {
machine_queue_free(relay->task_queue);
2017-07-26 14:05:29 +00:00
od_error(&instance->logger, "relay", "failed to start relay");
return -1;
2017-05-26 11:49:17 +00:00
}
return 0;
}