2017-05-26 11:49:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* odissey.
|
|
|
|
*
|
|
|
|
* PostgreSQL connection pooler and request router.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <soprano.h>
|
|
|
|
|
|
|
|
#include "od_macro.h"
|
|
|
|
#include "od_version.h"
|
|
|
|
#include "od_list.h"
|
|
|
|
#include "od_pid.h"
|
|
|
|
#include "od_syslog.h"
|
|
|
|
#include "od_log.h"
|
|
|
|
#include "od_daemon.h"
|
|
|
|
#include "od_scheme.h"
|
|
|
|
#include "od_lex.h"
|
|
|
|
#include "od_config.h"
|
|
|
|
#include "od_msg.h"
|
2017-05-26 12:17:45 +00:00
|
|
|
#include "od_system.h"
|
2017-05-26 11:49:17 +00:00
|
|
|
#include "od_instance.h"
|
|
|
|
|
|
|
|
#include "od_server.h"
|
|
|
|
#include "od_server_pool.h"
|
|
|
|
#include "od_client.h"
|
|
|
|
#include "od_client_pool.h"
|
|
|
|
#include "od_route_id.h"
|
|
|
|
#include "od_route.h"
|
2017-06-05 13:45:28 +00:00
|
|
|
#include "od_route_pool.h"
|
2017-05-26 11:49:17 +00:00
|
|
|
|
2017-06-05 13:45:28 +00:00
|
|
|
#include "od_router.h"
|
2017-05-26 11:49:17 +00:00
|
|
|
#include "od_pooler.h"
|
|
|
|
#include "od_relay.h"
|
|
|
|
#include "od_frontend.h"
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
od_relay(void *arg)
|
|
|
|
{
|
|
|
|
od_relay_t *relay = arg;
|
2017-05-26 12:17:45 +00:00
|
|
|
od_instance_t *instance = relay->system->instance;
|
2017-05-26 11:49:17 +00:00
|
|
|
|
2017-06-01 12:45:49 +00:00
|
|
|
od_log(&instance->log, "relay %d: started", relay->id);
|
2017-05-26 11:49:17 +00:00
|
|
|
|
2017-05-26 13:44:42 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2017-05-26 11:49:17 +00:00
|
|
|
machine_msg_t msg;
|
2017-05-26 12:17:45 +00:00
|
|
|
msg = machine_queue_get(relay->system->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;
|
2017-05-26 12:17:45 +00:00
|
|
|
coroutine_id = machine_coroutine_create(od_frontend, client);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (coroutine_id == -1) {
|
2017-05-31 15:47:15 +00:00
|
|
|
od_error_client(&instance->log, client->id, "relay",
|
|
|
|
"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;
|
|
|
|
}
|
2017-05-26 13:44:42 +00:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|
2017-05-26 12:17:45 +00:00
|
|
|
|
2017-05-26 11:49:17 +00:00
|
|
|
machine_msg_free(msg);
|
|
|
|
}
|
|
|
|
|
2017-06-01 09:28:23 +00:00
|
|
|
od_log(&instance->log, "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;
|
2017-05-26 12:17:45 +00:00
|
|
|
relay->system = system;
|
2017-05-26 11:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int od_relay_start(od_relay_t *relay)
|
|
|
|
{
|
2017-05-26 12:17:45 +00:00
|
|
|
od_instance_t *instance = relay->system->instance;
|
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) {
|
2017-05-31 15:47:15 +00:00
|
|
|
od_error(&instance->log, "failed to start relay");
|
2017-05-26 11:49:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|