kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 8313a31eba8d4535372c120a1c1683f8fcd1ede7
parent ea07ddef34d13aea1c203d8833595f24ab0c1367
Author: Joris Vink <joris@coders.se>
Date:   Mon, 28 Jul 2014 23:27:58 +0200

If we do not own the accept lock, reduce timeout for platform wait.

This way multiple workers won't stall when passing the accept lock
around for at maximum 100 ms (time spent in epoll/kqueue waits).

Diffstat:
src/worker.c | 35++++++++++++++++++++++++++++-------
1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/src/worker.c b/src/worker.c @@ -55,7 +55,7 @@ static int worker_trylock(void); static int worker_unlock(void); static void worker_decide_next(void); -static void kore_worker_acceptlock_obtain(void); +static int kore_worker_acceptlock_obtain(void); static TAILQ_HEAD(, connection) disconnected; static TAILQ_HEAD(, connection) worker_clients; @@ -183,7 +183,7 @@ kore_worker_entry(struct kore_worker *kw) int quit; char buf[16]; struct connection *c, *cnext; - u_int64_t now, idle_check, last_cb_run; + u_int64_t now, idle_check, last_cb_run, timer; worker = kw; @@ -250,10 +250,24 @@ kore_worker_entry(struct kore_worker *kw) sig_recv = 0; } - if (!worker->has_lock) - kore_worker_acceptlock_obtain(); + /* + * If we do not own the accept lock we want to reduce the time + * spent waiting in kore_platform_event_wait() in case we get + * handed the lock so we can grab it as fast as possible. + * + * Perhaps not the ideal thing to do, but I can't come up with + * anything better right now. + */ + if (!worker->has_lock) { + if (kore_worker_acceptlock_obtain()) + timer = 100; + else + timer = 1; + } else { + timer = 100; + } - kore_platform_event_wait(); + kore_platform_event_wait(timer); if (((worker->accepted >= worker->accept_treshold) || (worker_active_connections >= worker_max_connections)) && @@ -418,15 +432,19 @@ kore_worker_acceptlock_release(void) } } -static void +static int kore_worker_acceptlock_obtain(void) { + int r; + if (worker_count == 1 && !worker->has_lock) { worker->has_lock = 1; kore_platform_enable_accept(); - return; + return (1); } + r = 0; + if (worker_trylock()) { if (worker_active_connections >= worker_max_connections) { worker->accepted = 0; @@ -438,11 +456,14 @@ kore_worker_acceptlock_obtain(void) worker->busy_warn = 1; } } else { + r = 1; worker->has_lock = 1; worker->busy_warn = 0; kore_platform_enable_accept(); } } + + return (r); } static int