Ejemplo n.º 1
0
server_t* server_init(int port, size_t num_of_workers)
{
    server_t *server = calloc(sizeof(server_t), 1);
    server_addr_init(&(server->addr), port);
    server->evbase = event_base_new();
    server->evsignal = event_base_new();

    server->worker_count = num_of_workers;
    server->worker_list = calloc(sizeof(worker_t *), server->worker_count);
    server->worker_thread = calloc(sizeof(pthread_t *), server->worker_count);
    int i;
    for(i = 0; i < server->worker_count; i++)
    {
        server->worker_list[i] = worker_init(server);
        server->worker_thread[i] = calloc(sizeof(pthread_t), 1);
    }

    server->command_list = command_list_load();
    server->msg_buf = msg_buf_init();

    server->ketama = Ketama_new();

    Ketama_add_server(server->ketama, "127.0.0.1", 6379, 100);
    Ketama_add_server(server->ketama, "127.0.0.2", 6379, 100);
    Ketama_add_server(server->ketama, "127.0.0.3", 6379, 100);

    Ketama_create_continuum(server->ketama);
    return server;
}
Ejemplo n.º 2
0
echo_io *
make_echo_watcher(EV_P_ int wfd)
{
    if (set_nonblocking(wfd) == -1)
        return 0;

    echo_io *watcher = malloc(sizeof(echo_io));
    if (watcher) {
        msg_buf_init(&watcher->buf, server_ringbuf_capacity);
        watcher->half_closed = false;

        ev_io *io = &watcher->io;
        ev_io_init(io, echo_cb, wfd, EV_READ);

        ev_timer *timer = &watcher->timeout.timer;
        ev_init(timer, timeout_cb);
        watcher->timeout.last_activity = ev_now(EV_A);
        watcher->timeout.eio = watcher;
        timeout_cb(EV_A_ timer, EV_TIMER);
    }
    return watcher;
}