2008-09-08 00:20:55 +00:00
|
|
|
|
2008-09-09 18:47:10 +00:00
|
|
|
#include <sched.h>
|
|
|
|
|
2008-11-16 20:33:53 +00:00
|
|
|
#include <lighttpd/base.h>
|
2008-09-08 00:20:55 +00:00
|
|
|
|
2009-07-08 19:06:07 +00:00
|
|
|
static liConnection* worker_con_get(liWorker *wrk);
|
|
|
|
void worker_con_put(liConnection *con);
|
2008-09-08 00:20:55 +00:00
|
|
|
|
|
|
|
/* closing sockets - wait for proper shutdown */
|
|
|
|
|
|
|
|
typedef struct worker_closing_socket worker_closing_socket;
|
|
|
|
|
|
|
|
struct worker_closing_socket {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk;
|
2008-09-08 00:20:55 +00:00
|
|
|
GList *link;
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void worker_closing_socket_cb(int revents, void* arg) {
|
|
|
|
worker_closing_socket *scs = (worker_closing_socket*) arg;
|
|
|
|
UNUSED(revents);
|
|
|
|
|
|
|
|
/* Whatever happend: we just close the socket */
|
|
|
|
shutdown(scs->fd, SHUT_RD);
|
|
|
|
close(scs->fd);
|
|
|
|
g_queue_delete_link(&scs->wrk->closing_sockets, scs->link);
|
|
|
|
g_slice_free(worker_closing_socket, scs);
|
|
|
|
}
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
void li_worker_add_closing_socket(liWorker *wrk, int fd) {
|
2008-11-03 14:20:49 +00:00
|
|
|
worker_closing_socket *scs;
|
2008-09-08 00:20:55 +00:00
|
|
|
|
|
|
|
shutdown(fd, SHUT_WR);
|
2009-07-08 19:06:07 +00:00
|
|
|
if (g_atomic_int_get(&wrk->srv->state) == LI_SERVER_STOPPING) {
|
2008-09-09 16:09:20 +00:00
|
|
|
shutdown(fd, SHUT_RD);
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2008-09-08 00:20:55 +00:00
|
|
|
|
2008-11-03 14:20:49 +00:00
|
|
|
scs = g_slice_new0(worker_closing_socket);
|
2008-09-08 00:20:55 +00:00
|
|
|
scs->wrk = wrk;
|
|
|
|
scs->fd = fd;
|
|
|
|
g_queue_push_tail(&wrk->closing_sockets, scs);
|
|
|
|
scs->link = g_queue_peek_tail_link(&wrk->closing_sockets);
|
|
|
|
|
|
|
|
ev_once(wrk->loop, fd, EV_READ, 10.0, worker_closing_socket_cb, scs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Kill it - frees fd */
|
2009-07-08 19:06:07 +00:00
|
|
|
static void worker_rem_closing_socket(liWorker *wrk, worker_closing_socket *scs) {
|
2008-09-08 00:20:55 +00:00
|
|
|
ev_feed_fd_event(wrk->loop, scs->fd, EV_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep alive */
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
void li_worker_check_keepalive(liWorker *wrk) {
|
2008-09-08 00:20:55 +00:00
|
|
|
ev_tstamp now = ev_now(wrk->loop);
|
|
|
|
|
|
|
|
if (0 == wrk->keep_alive_queue.length) {
|
|
|
|
ev_timer_stop(wrk->loop, &wrk->keep_alive_timer);
|
|
|
|
} else {
|
2009-07-08 19:06:07 +00:00
|
|
|
wrk->keep_alive_timer.repeat = ((liConnection*)g_queue_peek_head(&wrk->keep_alive_queue))->keep_alive_data.timeout - now + 1;
|
2008-09-08 00:20:55 +00:00
|
|
|
ev_timer_again(wrk->loop, &wrk->keep_alive_timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void worker_keepalive_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = (liWorker*) w->data;
|
2008-09-08 00:20:55 +00:00
|
|
|
ev_tstamp now = ev_now(wrk->loop);
|
|
|
|
GQueue *q = &wrk->keep_alive_queue;
|
|
|
|
GList *l;
|
2009-07-08 19:06:07 +00:00
|
|
|
liConnection *con;
|
2008-09-08 00:20:55 +00:00
|
|
|
|
|
|
|
UNUSED(loop);
|
|
|
|
UNUSED(revents);
|
|
|
|
|
|
|
|
while ( NULL != (l = g_queue_peek_head_link(q)) &&
|
2009-07-08 19:06:07 +00:00
|
|
|
(con = (liConnection*) l->data)->keep_alive_data.timeout <= now ) {
|
2008-09-08 00:20:55 +00:00
|
|
|
ev_tstamp remaining = con->keep_alive_data.max_idle - wrk->srv->keep_alive_queue_timeout - (now - con->keep_alive_data.timeout);
|
|
|
|
if (remaining > 0) {
|
|
|
|
g_queue_delete_link(q, l);
|
|
|
|
con->keep_alive_data.link = NULL;
|
|
|
|
ev_timer_set(&con->keep_alive_data.watcher, remaining, 0);
|
|
|
|
ev_timer_start(wrk->loop, &con->keep_alive_data.watcher);
|
|
|
|
} else {
|
|
|
|
/* close it */
|
2008-09-24 16:59:49 +00:00
|
|
|
worker_con_put(con);
|
2008-09-08 00:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == l) {
|
|
|
|
ev_timer_stop(wrk->loop, &wrk->keep_alive_timer);
|
|
|
|
} else {
|
|
|
|
wrk->keep_alive_timer.repeat = con->keep_alive_data.timeout - now + 1;
|
|
|
|
ev_timer_again(wrk->loop, &wrk->keep_alive_timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-10 14:39:03 +00:00
|
|
|
/* check for timeouted connections */
|
|
|
|
static void worker_io_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = (liWorker*) w->data;
|
|
|
|
liConnection *con;
|
|
|
|
liWaitQueueElem *wqe;
|
2008-11-12 01:09:52 +00:00
|
|
|
ev_tstamp now = CUR_TS(wrk);
|
2008-11-10 14:39:03 +00:00
|
|
|
|
|
|
|
UNUSED(loop);
|
|
|
|
UNUSED(revents);
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
while ((wqe = li_waitqueue_pop(&wrk->io_timeout_queue)) != NULL) {
|
2008-11-12 01:09:52 +00:00
|
|
|
/* connection has timed out */
|
|
|
|
con = wqe->data;
|
2008-12-30 13:24:33 +00:00
|
|
|
_DEBUG(con->srv, con->mainvr, "connection io-timeout from %s after %.2f seconds", con->remote_addr_str->str, now - wqe->ts);
|
2009-07-09 20:17:24 +00:00
|
|
|
li_plugins_handle_close(con);
|
2008-11-12 01:09:52 +00:00
|
|
|
worker_con_put(con);
|
2008-11-10 14:39:03 +00:00
|
|
|
}
|
2008-11-12 01:09:52 +00:00
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
li_waitqueue_update(&wrk->io_timeout_queue);
|
2008-11-10 14:39:03 +00:00
|
|
|
}
|
|
|
|
|
2008-12-30 20:55:00 +00:00
|
|
|
/* run vreqest state machine */
|
|
|
|
static void worker_job_queue_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = (liWorker*) w->data;
|
2008-12-30 20:55:00 +00:00
|
|
|
GQueue q = wrk->job_queue;
|
2009-04-14 16:18:25 +00:00
|
|
|
GList *l;
|
2009-07-08 19:06:07 +00:00
|
|
|
liVRequest *vr;
|
2008-12-30 20:55:00 +00:00
|
|
|
UNUSED(loop);
|
|
|
|
UNUSED(revents);
|
|
|
|
|
|
|
|
g_queue_init(&wrk->job_queue); /* reset queue, elements are in q */
|
|
|
|
|
2009-04-14 16:18:25 +00:00
|
|
|
while (NULL != (l = g_queue_pop_head_link(&q))) {
|
|
|
|
vr = l->data;
|
|
|
|
g_assert(g_atomic_int_compare_and_exchange(&vr->queued, 1, 0));
|
2009-07-09 20:17:24 +00:00
|
|
|
li_vrequest_state_machine(vr);
|
2008-12-30 20:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-14 16:18:25 +00:00
|
|
|
/* run vreqest state machine for async queued jobs */
|
|
|
|
static void worker_job_async_queue_cb(struct ev_loop *loop, ev_async *w, int revents) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = (liWorker*) w->data;
|
2009-04-14 16:18:25 +00:00
|
|
|
GAsyncQueue *q = wrk->job_async_queue;
|
2009-07-08 19:06:07 +00:00
|
|
|
liVRequestRef *vr_ref;
|
|
|
|
liVRequest *vr;
|
2009-04-14 16:18:25 +00:00
|
|
|
UNUSED(loop);
|
|
|
|
UNUSED(revents);
|
|
|
|
|
|
|
|
while (NULL != (vr_ref = g_async_queue_try_pop(q))) {
|
2009-07-09 20:17:24 +00:00
|
|
|
if (NULL != (vr = li_vrequest_release_ref(vr_ref))) {
|
2009-04-14 16:18:25 +00:00
|
|
|
g_assert(g_atomic_int_compare_and_exchange(&vr->queued, 1, 0));
|
2009-07-09 20:17:24 +00:00
|
|
|
li_vrequest_state_machine(vr);
|
2009-04-14 16:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-07 19:59:51 +00:00
|
|
|
|
2008-09-08 00:20:55 +00:00
|
|
|
/* cache timestamp */
|
2009-07-17 11:04:01 +00:00
|
|
|
GString *li_worker_current_timestamp(liWorker *wrk, liTimeFunc timefunc, guint format_ndx) {
|
2009-01-07 19:59:51 +00:00
|
|
|
gsize len;
|
2009-01-07 22:36:17 +00:00
|
|
|
struct tm tm;
|
2009-07-17 11:04:01 +00:00
|
|
|
liWorkerTS *wts;
|
2009-01-09 20:32:49 +00:00
|
|
|
time_t now = (time_t)CUR_TS(wrk);
|
2008-09-08 00:20:55 +00:00
|
|
|
|
2009-07-17 11:04:01 +00:00
|
|
|
if (timefunc == LI_GMTIME)
|
|
|
|
wts = &g_array_index(wrk->timestamps_gmt, liWorkerTS, format_ndx);
|
|
|
|
else
|
|
|
|
wts = &g_array_index(wrk->timestamps_local, liWorkerTS, format_ndx);
|
|
|
|
|
2009-01-07 19:59:51 +00:00
|
|
|
/* cache hit */
|
2009-01-09 20:32:49 +00:00
|
|
|
if (now == wts->last_generated)
|
2009-01-07 19:59:51 +00:00
|
|
|
return wts->str;
|
2008-09-08 00:20:55 +00:00
|
|
|
|
2009-07-17 11:04:01 +00:00
|
|
|
if (timefunc == LI_GMTIME) {
|
|
|
|
if (!gmtime_r(&now, &tm))
|
|
|
|
return NULL;
|
|
|
|
} else if (!localtime_r(&now, &tm))
|
2009-01-07 22:36:17 +00:00
|
|
|
return NULL;
|
2009-07-17 11:04:01 +00:00
|
|
|
|
|
|
|
g_string_set_size(wts->str, 255);
|
|
|
|
|
2009-01-07 22:36:17 +00:00
|
|
|
len = strftime(wts->str->str, wts->str->allocated_len, g_array_index(wrk->srv->ts_formats, GString*, format_ndx)->str, &tm);
|
2009-07-17 11:04:01 +00:00
|
|
|
|
2009-01-07 19:59:51 +00:00
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
g_string_set_size(wts->str, len);
|
|
|
|
wts->last_generated = now;
|
2009-07-17 11:04:01 +00:00
|
|
|
|
2009-01-07 19:59:51 +00:00
|
|
|
return wts->str;
|
2008-09-08 00:20:55 +00:00
|
|
|
}
|
|
|
|
|
2008-09-09 00:22:21 +00:00
|
|
|
/* stop worker watcher */
|
2009-07-09 20:17:24 +00:00
|
|
|
static void li_worker_stop_cb(struct ev_loop *loop, ev_async *w, int revents) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = (liWorker*) w->data;
|
2008-09-09 00:22:21 +00:00
|
|
|
UNUSED(loop);
|
|
|
|
UNUSED(revents);
|
2009-01-01 15:44:42 +00:00
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
li_worker_stop(wrk, wrk);
|
2008-09-09 00:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* exit worker watcher */
|
2009-07-09 20:17:24 +00:00
|
|
|
static void li_worker_exit_cb(struct ev_loop *loop, ev_async *w, int revents) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = (liWorker*) w->data;
|
2008-09-09 00:22:21 +00:00
|
|
|
UNUSED(loop);
|
|
|
|
UNUSED(revents);
|
2009-01-01 15:44:42 +00:00
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
li_worker_exit(wrk, wrk);
|
2008-09-09 00:22:21 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
typedef struct li_worker_new_con_data li_worker_new_con_data;
|
|
|
|
struct li_worker_new_con_data {
|
2009-07-08 19:06:07 +00:00
|
|
|
liSocketAddress remote_addr;
|
2008-09-24 16:59:49 +00:00
|
|
|
int s;
|
2009-07-08 19:06:07 +00:00
|
|
|
liServerSocket *srv_sock;
|
2008-09-24 16:59:49 +00:00
|
|
|
};
|
|
|
|
|
2008-09-09 00:22:21 +00:00
|
|
|
/* new con watcher */
|
2009-07-09 20:17:24 +00:00
|
|
|
void li_worker_new_con(liWorker *ctx, liWorker *wrk, liSocketAddress remote_addr, int s, liServerSocket *srv_sock) {
|
2008-09-24 16:59:49 +00:00
|
|
|
if (ctx == wrk) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liConnection *con = worker_con_get(wrk);
|
2008-09-24 16:59:49 +00:00
|
|
|
|
2009-04-03 12:29:55 +00:00
|
|
|
con->srv_sock = srv_sock;
|
2009-07-08 19:06:07 +00:00
|
|
|
con->state = LI_CON_STATE_REQUEST_START;
|
2009-04-03 12:29:55 +00:00
|
|
|
con->remote_addr = remote_addr;
|
2008-09-24 16:59:49 +00:00
|
|
|
ev_io_set(&con->sock_watcher, s, EV_READ);
|
2008-09-09 00:22:21 +00:00
|
|
|
ev_io_start(wrk->loop, &con->sock_watcher);
|
2008-11-03 14:18:46 +00:00
|
|
|
con->ts = CUR_TS(con->wrk);
|
2009-07-09 20:17:24 +00:00
|
|
|
li_sockaddr_to_string(remote_addr, con->remote_addr_str, FALSE);
|
|
|
|
li_waitqueue_push(&wrk->io_timeout_queue, &con->io_timeout_elem);
|
2008-09-09 00:22:21 +00:00
|
|
|
} else {
|
2009-07-09 20:17:24 +00:00
|
|
|
li_worker_new_con_data *d = g_slice_new(li_worker_new_con_data);
|
2009-04-03 12:29:55 +00:00
|
|
|
d->remote_addr = remote_addr;
|
2008-09-24 16:59:49 +00:00
|
|
|
d->s = s;
|
2009-04-03 12:29:55 +00:00
|
|
|
d->srv_sock = srv_sock;
|
2008-09-24 16:59:49 +00:00
|
|
|
g_async_queue_push(wrk->new_con_queue, d);
|
2008-09-09 00:22:21 +00:00
|
|
|
ev_async_send(wrk->loop, &wrk->new_con_watcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
static void li_worker_new_con_cb(struct ev_loop *loop, ev_async *w, int revents) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = (liWorker*) w->data;
|
2009-07-09 20:17:24 +00:00
|
|
|
li_worker_new_con_data *d;
|
2008-09-09 00:22:21 +00:00
|
|
|
UNUSED(loop);
|
|
|
|
UNUSED(revents);
|
|
|
|
|
2008-09-24 16:59:49 +00:00
|
|
|
while (NULL != (d = g_async_queue_try_pop(wrk->new_con_queue))) {
|
2009-07-09 20:17:24 +00:00
|
|
|
li_worker_new_con(wrk, wrk, d->remote_addr, d->s, d->srv_sock);
|
|
|
|
g_slice_free(li_worker_new_con_data, d);
|
2008-09-09 00:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-03 14:18:46 +00:00
|
|
|
/* stats watcher */
|
|
|
|
static void worker_stats_watcher_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = (liWorker*) w->data;
|
2009-01-01 15:44:42 +00:00
|
|
|
ev_tstamp now = ev_now(wrk->loop);
|
2008-09-25 20:49:32 +00:00
|
|
|
UNUSED(loop);
|
|
|
|
UNUSED(revents);
|
|
|
|
|
|
|
|
if (wrk->stats.last_update && now != wrk->stats.last_update) {
|
|
|
|
wrk->stats.requests_per_sec =
|
|
|
|
(wrk->stats.requests - wrk->stats.last_requests) / (now - wrk->stats.last_update);
|
|
|
|
if (wrk->stats.requests_per_sec > 0)
|
2008-12-30 13:24:33 +00:00
|
|
|
DEBUG(wrk->srv, "worker %u: %.2f requests per second", wrk->ndx, wrk->stats.requests_per_sec);
|
2008-09-25 20:49:32 +00:00
|
|
|
}
|
|
|
|
|
2008-11-03 14:18:46 +00:00
|
|
|
/* 5s averages */
|
|
|
|
if ((now - wrk->stats.last_avg) > 5) {
|
|
|
|
/* bytes in */
|
|
|
|
wrk->stats.bytes_in_5s_diff = wrk->stats.bytes_in - wrk->stats.bytes_in_5s;
|
|
|
|
wrk->stats.bytes_in_5s = wrk->stats.bytes_in;
|
|
|
|
|
|
|
|
/* bytes out */
|
|
|
|
wrk->stats.bytes_out_5s_diff = wrk->stats.bytes_out - wrk->stats.bytes_out_5s;
|
|
|
|
wrk->stats.bytes_out_5s = wrk->stats.bytes_out;
|
|
|
|
|
|
|
|
/* requests */
|
|
|
|
wrk->stats.requests_5s_diff = wrk->stats.requests - wrk->stats.requests_5s;
|
|
|
|
wrk->stats.requests_5s = wrk->stats.requests;
|
|
|
|
|
|
|
|
/* active connections */
|
|
|
|
wrk->stats.active_cons_5s = wrk->connections_active;
|
|
|
|
|
|
|
|
wrk->stats.last_avg = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrk->stats.active_cons_cum += wrk->connections_active;
|
|
|
|
|
2008-09-25 20:49:32 +00:00
|
|
|
wrk->stats.last_requests = wrk->stats.requests;
|
|
|
|
wrk->stats.last_update = now;
|
|
|
|
}
|
|
|
|
|
2008-09-08 00:20:55 +00:00
|
|
|
/* init */
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
liWorker* li_worker_new(liServer *srv, struct ev_loop *loop) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = g_slice_new0(liWorker);
|
2008-09-08 00:20:55 +00:00
|
|
|
wrk->srv = srv;
|
|
|
|
wrk->loop = loop;
|
|
|
|
|
|
|
|
g_queue_init(&wrk->keep_alive_queue);
|
|
|
|
ev_init(&wrk->keep_alive_timer, worker_keepalive_cb);
|
|
|
|
wrk->keep_alive_timer.data = wrk;
|
|
|
|
|
2008-09-24 16:59:49 +00:00
|
|
|
wrk->connections_active = 0;
|
2009-07-08 19:06:07 +00:00
|
|
|
wrk->connections = g_array_new(FALSE, TRUE, sizeof(liConnection*));
|
2008-09-24 16:59:49 +00:00
|
|
|
|
2008-09-08 00:20:55 +00:00
|
|
|
wrk->tmp_str = g_string_sized_new(255);
|
|
|
|
|
2009-07-17 11:04:01 +00:00
|
|
|
wrk->timestamps_gmt = g_array_sized_new(FALSE, TRUE, sizeof(liWorkerTS), srv->ts_formats->len);
|
|
|
|
g_array_set_size(wrk->timestamps_gmt, srv->ts_formats->len);
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
for (i = 0; i < srv->ts_formats->len; i++)
|
|
|
|
g_array_index(wrk->timestamps_gmt, liWorkerTS, i).str = g_string_sized_new(255);
|
|
|
|
}
|
|
|
|
wrk->timestamps_local = g_array_sized_new(FALSE, TRUE, sizeof(liWorkerTS), srv->ts_formats->len);
|
|
|
|
g_array_set_size(wrk->timestamps_local, srv->ts_formats->len);
|
2009-01-07 19:59:51 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
for (i = 0; i < srv->ts_formats->len; i++)
|
2009-07-17 11:04:01 +00:00
|
|
|
g_array_index(wrk->timestamps_local, liWorkerTS, i).str = g_string_sized_new(255);
|
2009-01-07 19:59:51 +00:00
|
|
|
}
|
2008-09-08 00:20:55 +00:00
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
ev_init(&wrk->li_worker_exit_watcher, li_worker_exit_cb);
|
|
|
|
wrk->li_worker_exit_watcher.data = wrk;
|
|
|
|
ev_async_start(wrk->loop, &wrk->li_worker_exit_watcher);
|
2008-09-09 15:27:37 +00:00
|
|
|
ev_unref(wrk->loop); /* this watcher shouldn't keep the loop alive */
|
2008-09-09 00:22:21 +00:00
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
ev_init(&wrk->li_worker_stop_watcher, li_worker_stop_cb);
|
|
|
|
wrk->li_worker_stop_watcher.data = wrk;
|
|
|
|
ev_async_start(wrk->loop, &wrk->li_worker_stop_watcher);
|
2008-09-09 00:22:21 +00:00
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
ev_init(&wrk->new_con_watcher, li_worker_new_con_cb);
|
2008-09-09 00:22:21 +00:00
|
|
|
wrk->new_con_watcher.data = wrk;
|
|
|
|
ev_async_start(wrk->loop, &wrk->new_con_watcher);
|
|
|
|
wrk->new_con_queue = g_async_queue_new();
|
|
|
|
|
2008-11-03 14:18:46 +00:00
|
|
|
ev_timer_init(&wrk->stats_watcher, worker_stats_watcher_cb, 1, 1);
|
|
|
|
wrk->stats_watcher.data = wrk;
|
|
|
|
ev_timer_start(wrk->loop, &wrk->stats_watcher);
|
2008-09-25 20:49:32 +00:00
|
|
|
ev_unref(wrk->loop); /* this watcher shouldn't keep the loop alive */
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
ev_init(&wrk->collect_watcher, li_collect_watcher_cb);
|
2008-09-26 08:36:36 +00:00
|
|
|
wrk->collect_watcher.data = wrk;
|
|
|
|
ev_async_start(wrk->loop, &wrk->collect_watcher);
|
|
|
|
wrk->collect_queue = g_async_queue_new();
|
|
|
|
ev_unref(wrk->loop); /* this watcher shouldn't keep the loop alive */
|
|
|
|
|
2008-11-12 01:09:52 +00:00
|
|
|
/* io timeout timer */
|
2009-07-09 20:17:24 +00:00
|
|
|
li_waitqueue_init(&wrk->io_timeout_queue, wrk->loop, worker_io_timeout_cb, srv->io_timeout, wrk);
|
2008-11-10 14:39:03 +00:00
|
|
|
|
2008-11-12 21:16:52 +00:00
|
|
|
/* throttling */
|
2009-07-09 20:17:24 +00:00
|
|
|
li_waitqueue_init(&wrk->throttle_queue, wrk->loop, throttle_cb, THROTTLE_GRANULARITY, wrk);
|
2008-11-12 21:16:52 +00:00
|
|
|
|
2008-12-30 20:55:00 +00:00
|
|
|
/* job queue */
|
|
|
|
g_queue_init(&wrk->job_queue);
|
|
|
|
ev_timer_init(&wrk->job_queue_watcher, worker_job_queue_cb, 0, 0);
|
|
|
|
wrk->job_queue_watcher.data = wrk;
|
|
|
|
|
2009-04-14 16:18:25 +00:00
|
|
|
wrk->job_async_queue = g_async_queue_new();
|
|
|
|
ev_async_init(&wrk->job_async_queue_watcher, worker_job_async_queue_cb);
|
|
|
|
wrk->job_async_queue_watcher.data = wrk;
|
|
|
|
ev_async_start(wrk->loop, &wrk->job_async_queue_watcher);
|
|
|
|
ev_unref(wrk->loop); /* this watcher shouldn't keep the loop alive */
|
|
|
|
|
2009-03-04 01:54:38 +00:00
|
|
|
stat_cache_new(wrk, srv->stat_cache_ttl);
|
2009-03-01 20:16:58 +00:00
|
|
|
|
2008-09-08 00:20:55 +00:00
|
|
|
return wrk;
|
|
|
|
}
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
void li_worker_free(liWorker *wrk) {
|
2008-09-08 00:20:55 +00:00
|
|
|
if (!wrk) return;
|
|
|
|
|
2008-09-24 16:59:49 +00:00
|
|
|
{ /* close connections */
|
|
|
|
guint i;
|
|
|
|
if (wrk->connections_active > 0) {
|
|
|
|
ERROR(wrk->srv, "Server shutdown with unclosed connections: %u", wrk->connections_active);
|
|
|
|
for (i = wrk->connections_active; i-- > 0;) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liConnection *con = g_array_index(wrk->connections, liConnection*, i);
|
2009-07-09 20:17:24 +00:00
|
|
|
li_connection_error(con);
|
2008-09-24 16:59:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < wrk->connections->len; i++) {
|
2009-07-09 20:17:24 +00:00
|
|
|
li_connection_free(g_array_index(wrk->connections, liConnection*, i));
|
2008-09-24 16:59:49 +00:00
|
|
|
}
|
|
|
|
g_array_free(wrk->connections, TRUE);
|
|
|
|
}
|
|
|
|
|
2008-09-08 00:20:55 +00:00
|
|
|
{ /* force closing sockets */
|
|
|
|
GList *iter;
|
|
|
|
for (iter = g_queue_peek_head_link(&wrk->closing_sockets); iter; iter = g_list_next(iter)) {
|
|
|
|
worker_closing_socket_cb(EV_TIMEOUT, (worker_closing_socket*) iter->data);
|
|
|
|
}
|
|
|
|
g_queue_clear(&wrk->closing_sockets);
|
|
|
|
}
|
|
|
|
|
2009-07-26 13:00:53 +00:00
|
|
|
li_ev_safe_ref_and_stop(ev_async_stop, wrk->loop, &wrk->job_async_queue_watcher);
|
2008-09-09 15:27:37 +00:00
|
|
|
|
2009-01-07 19:59:51 +00:00
|
|
|
{ /* free timestamps */
|
|
|
|
guint i;
|
2009-07-17 11:04:01 +00:00
|
|
|
for (i = 0; i < wrk->timestamps_gmt->len; i++) {
|
|
|
|
g_string_free(g_array_index(wrk->timestamps_gmt, liWorkerTS, i).str, TRUE);
|
|
|
|
g_string_free(g_array_index(wrk->timestamps_local, liWorkerTS, i).str, TRUE);
|
|
|
|
}
|
|
|
|
g_array_free(wrk->timestamps_gmt, TRUE);
|
|
|
|
g_array_free(wrk->timestamps_local, TRUE);
|
2009-01-07 19:59:51 +00:00
|
|
|
}
|
2008-09-08 00:20:55 +00:00
|
|
|
|
2009-07-26 13:00:53 +00:00
|
|
|
li_ev_safe_ref_and_stop(ev_async_stop, wrk->loop, &wrk->li_worker_exit_watcher);
|
2009-04-14 16:18:25 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
GAsyncQueue *q = wrk->job_async_queue;
|
2009-07-08 19:06:07 +00:00
|
|
|
liVRequestRef *vr_ref;
|
|
|
|
liVRequest *vr;
|
2009-04-14 16:18:25 +00:00
|
|
|
|
|
|
|
while (NULL != (vr_ref = g_async_queue_try_pop(q))) {
|
2009-07-09 20:17:24 +00:00
|
|
|
if (NULL != (vr = li_vrequest_release_ref(vr_ref))) {
|
2009-04-14 16:18:25 +00:00
|
|
|
g_assert(g_atomic_int_compare_and_exchange(&vr->queued, 1, 0));
|
2009-07-09 20:17:24 +00:00
|
|
|
li_vrequest_state_machine(vr);
|
2009-04-14 16:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_async_queue_unref(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-24 18:02:47 +00:00
|
|
|
g_async_queue_unref(wrk->new_con_queue);
|
|
|
|
|
2009-07-26 13:00:53 +00:00
|
|
|
li_ev_safe_ref_and_stop(ev_timer_stop, wrk->loop, &wrk->stats_watcher);
|
2008-09-25 20:49:32 +00:00
|
|
|
|
2009-07-26 13:00:53 +00:00
|
|
|
li_ev_safe_ref_and_stop(ev_async_stop, wrk->loop, &wrk->collect_watcher);
|
2009-07-09 20:17:24 +00:00
|
|
|
li_collect_watcher_cb(wrk->loop, &wrk->collect_watcher, 0);
|
2008-09-26 08:36:36 +00:00
|
|
|
g_async_queue_unref(wrk->collect_queue);
|
|
|
|
|
2009-01-07 20:11:06 +00:00
|
|
|
g_string_free(wrk->tmp_str, TRUE);
|
|
|
|
|
2009-03-01 20:16:58 +00:00
|
|
|
stat_cache_free(wrk->stat_cache);
|
|
|
|
|
2009-07-08 19:06:07 +00:00
|
|
|
g_slice_free(liWorker, wrk);
|
2008-09-08 00:20:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
void li_worker_run(liWorker *wrk) {
|
2008-11-20 02:17:50 +00:00
|
|
|
#ifdef LIGHTY_OS_LINUX
|
|
|
|
/* sched_setaffinity is only available on linux */
|
2008-09-09 18:47:10 +00:00
|
|
|
cpu_set_t mask;
|
|
|
|
|
|
|
|
if (0 != sched_getaffinity(0, sizeof(mask), &mask)) {
|
|
|
|
ERROR(wrk->srv, "couldn't get cpu affinity mask: %s", g_strerror(errno));
|
|
|
|
} else {
|
|
|
|
guint cpus = 0;
|
|
|
|
while (CPU_ISSET(cpus, &mask)) cpus++;
|
|
|
|
if (cpus) {
|
|
|
|
CPU_ZERO(&mask);
|
|
|
|
CPU_SET(wrk->ndx % cpus, &mask);
|
|
|
|
if (0 != sched_setaffinity(0, sizeof(mask), &mask)) {
|
|
|
|
ERROR(wrk->srv, "couldn't set cpu affinity mask: %s", g_strerror(errno));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ERROR(wrk->srv, "%s", "cpu 0 not enabled, no affinity set");
|
|
|
|
}
|
|
|
|
}
|
2008-11-20 02:17:50 +00:00
|
|
|
#endif
|
|
|
|
|
2008-09-08 00:20:55 +00:00
|
|
|
ev_loop(wrk->loop, 0);
|
|
|
|
}
|
2008-09-09 00:22:21 +00:00
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
void li_worker_stop(liWorker *context, liWorker *wrk) {
|
2008-09-09 00:22:21 +00:00
|
|
|
if (context == wrk) {
|
|
|
|
guint i;
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
ev_async_stop(wrk->loop, &wrk->li_worker_stop_watcher);
|
2008-09-09 00:22:21 +00:00
|
|
|
ev_async_stop(wrk->loop, &wrk->new_con_watcher);
|
2009-07-09 20:17:24 +00:00
|
|
|
li_waitqueue_stop(&wrk->io_timeout_queue);
|
|
|
|
li_waitqueue_stop(&wrk->throttle_queue);
|
|
|
|
li_worker_new_con_cb(wrk->loop, &wrk->new_con_watcher, 0); /* handle remaining new connections */
|
2008-09-09 00:22:21 +00:00
|
|
|
|
2008-09-24 16:59:49 +00:00
|
|
|
/* close keep alive connections */
|
|
|
|
for (i = wrk->connections_active; i-- > 0;) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liConnection *con = g_array_index(wrk->connections, liConnection*, i);
|
|
|
|
if (con->state == LI_CON_STATE_KEEP_ALIVE)
|
2008-09-24 16:59:49 +00:00
|
|
|
worker_con_put(con);
|
2008-09-09 00:22:21 +00:00
|
|
|
}
|
2008-09-24 16:59:49 +00:00
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
li_worker_check_keepalive(wrk);
|
2008-09-09 00:22:21 +00:00
|
|
|
|
|
|
|
{ /* force closing sockets */
|
|
|
|
GList *iter;
|
|
|
|
for (iter = g_queue_peek_head_link(&wrk->closing_sockets); iter; iter = g_list_next(iter)) {
|
|
|
|
worker_rem_closing_socket(wrk, (worker_closing_socket*) iter->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-07-09 20:17:24 +00:00
|
|
|
ev_async_send(wrk->loop, &wrk->li_worker_stop_watcher);
|
2008-09-09 00:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-09 20:17:24 +00:00
|
|
|
void li_worker_exit(liWorker *context, liWorker *wrk) {
|
2008-09-09 00:22:21 +00:00
|
|
|
if (context == wrk) {
|
|
|
|
ev_unloop (wrk->loop, EVUNLOOP_ALL);
|
|
|
|
} else {
|
2009-07-09 20:17:24 +00:00
|
|
|
ev_async_send(wrk->loop, &wrk->li_worker_exit_watcher);
|
2008-09-09 00:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-24 16:59:49 +00:00
|
|
|
|
|
|
|
|
2009-07-08 19:06:07 +00:00
|
|
|
static liConnection* worker_con_get(liWorker *wrk) {
|
|
|
|
liConnection *con;
|
2008-09-24 16:59:49 +00:00
|
|
|
|
|
|
|
if (wrk->connections_active >= wrk->connections->len) {
|
2009-07-09 20:17:24 +00:00
|
|
|
con = li_connection_new(wrk);
|
2008-09-24 16:59:49 +00:00
|
|
|
con->idx = wrk->connections_active;
|
|
|
|
g_array_append_val(wrk->connections, con);
|
|
|
|
} else {
|
2009-07-08 19:06:07 +00:00
|
|
|
con = g_array_index(wrk->connections, liConnection*, wrk->connections_active);
|
2008-09-24 16:59:49 +00:00
|
|
|
}
|
|
|
|
g_atomic_int_inc((gint*) &wrk->connections_active);
|
|
|
|
return con;
|
|
|
|
}
|
|
|
|
|
2009-07-08 19:06:07 +00:00
|
|
|
void worker_con_put(liConnection *con) {
|
2009-03-28 02:37:41 +00:00
|
|
|
guint threshold;
|
2009-07-08 19:06:07 +00:00
|
|
|
liWorker *wrk = con->wrk;
|
2009-03-28 02:37:41 +00:00
|
|
|
ev_tstamp now = CUR_TS(wrk);
|
2008-11-23 17:21:45 +00:00
|
|
|
|
2009-07-08 19:06:07 +00:00
|
|
|
if (con->state == LI_CON_STATE_DEAD)
|
2008-11-23 17:21:45 +00:00
|
|
|
/* already disconnected */
|
|
|
|
return;
|
2008-09-24 16:59:49 +00:00
|
|
|
|
|
|
|
g_atomic_int_add((gint*) &wrk->connection_load, -1);
|
|
|
|
g_atomic_int_add((gint*) &wrk->connections_active, -1);
|
2009-03-28 02:37:41 +00:00
|
|
|
|
2008-09-24 16:59:49 +00:00
|
|
|
if (con->idx != wrk->connections_active) {
|
|
|
|
/* Swap [con->idx] and [wrk->connections_active] */
|
2009-07-08 19:06:07 +00:00
|
|
|
liConnection *tmp;
|
2008-09-24 16:59:49 +00:00
|
|
|
assert(con->idx < wrk->connections_active); /* con must be an active connection */
|
2009-07-08 19:06:07 +00:00
|
|
|
tmp = g_array_index(wrk->connections, liConnection*, wrk->connections_active);
|
2008-09-24 16:59:49 +00:00
|
|
|
tmp->idx = con->idx;
|
|
|
|
con->idx = wrk->connections_active;
|
2009-07-08 19:06:07 +00:00
|
|
|
g_array_index(wrk->connections, liConnection*, con->idx) = con;
|
|
|
|
g_array_index(wrk->connections, liConnection*, tmp->idx) = tmp;
|
2008-09-24 16:59:49 +00:00
|
|
|
}
|
2009-03-28 02:37:41 +00:00
|
|
|
|
|
|
|
/* realloc wrk->connections if it makes sense (too many allocated, only every 60sec) */
|
|
|
|
/* if (active < allocated*0.70) { allocated *= 0.85 } */
|
|
|
|
threshold = (wrk->connections->len * 7) / 10;
|
|
|
|
if (wrk->connections_active < threshold && (now - wrk->connections_gc_ts) < 60.0 && wrk->connections->len > 10) {
|
|
|
|
/* realloc */
|
|
|
|
guint i;
|
|
|
|
threshold = (wrk->connections->len * 85) / 100;
|
|
|
|
for (i = wrk->connections->len; i > threshold; i--) {
|
2009-07-09 20:17:24 +00:00
|
|
|
li_connection_free(g_array_index(wrk->connections, liConnection*, i-1));
|
2009-03-28 02:37:41 +00:00
|
|
|
}
|
|
|
|
wrk->connections->len = threshold;
|
|
|
|
wrk->connections_gc_ts = now;
|
|
|
|
} else {
|
|
|
|
/* no realloc */
|
2009-07-09 20:17:24 +00:00
|
|
|
li_connection_reset(con);
|
2009-03-28 02:37:41 +00:00
|
|
|
}
|
2008-09-24 16:59:49 +00:00
|
|
|
}
|