From 5a5e0a4f3655fc4f12507c315eb284b9149e807b Mon Sep 17 00:00:00 2001 From: phk Date: Fri, 7 Jul 2006 07:15:33 +0000 Subject: [PATCH] Time idle TCP connections out after 30 seconds git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@367 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 2 +- varnish-cache/bin/varnishd/cache_acceptor.c | 54 ++++++++++++++++++++- varnish-cache/bin/varnishd/cache_pool.c | 11 +---- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 3c60a06b..00642140 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -202,7 +202,7 @@ void PipeSession(struct worker *w, struct sess *sp); /* cache_pool.c */ void CacheInitPool(void); -void DealWithSession(void *arg, int good); +void DealWithSession(void *arg); /* cache_shmlog.c */ void VSL_Init(void); diff --git a/varnish-cache/bin/varnishd/cache_acceptor.c b/varnish-cache/bin/varnishd/cache_acceptor.c index bad2b851..00ef0f84 100644 --- a/varnish-cache/bin/varnishd/cache_acceptor.c +++ b/varnish-cache/bin/varnishd/cache_acceptor.c @@ -34,11 +34,15 @@ static struct event_base *evb; static struct event pipe_e; static int pipes[2]; +static struct event tick_e; +static struct timeval tick_rate; + static pthread_t vca_thread; #define SESS_IOVS 10 static struct event accept_e[2 * HERITAGE_NSOCKS]; +static TAILQ_HEAD(,sess) sesshead = TAILQ_HEAD_INITIALIZER(sesshead); struct sessmem { struct sess s; @@ -127,6 +131,41 @@ vca_write_obj(struct worker *w, struct sess *sp) /*--------------------------------------------------------------------*/ +static void +vca_tick(int a, short b, void *c) +{ + struct sess *sp, *sp2; + time_t t; + + printf("vca_tick\n"); + evtimer_add(&tick_e, &tick_rate); + time(&t); + TAILQ_FOREACH_SAFE(sp, &sesshead, list, sp2) { + if (sp->t_resp + 30 < t) { + TAILQ_REMOVE(&sesshead, sp, list); + vca_close_session(sp, "timeout"); + vca_return_session(sp); + } + } +} + +static void +vca_callback(void *arg, int bad) +{ + struct sess *sp = arg; + + TAILQ_REMOVE(&sesshead, sp, list); + if (bad) { + if (bad == 1) + vca_close_session(sp, "overflow"); + else + vca_close_session(sp, "no request"); + vca_return_session(sp); + return; + } + DealWithSession(sp); +} + static void pipe_f(int fd, short event, void *arg) { @@ -135,7 +174,9 @@ pipe_f(int fd, short event, void *arg) i = read(fd, &sp, sizeof sp); assert(i == sizeof sp); - http_RecvHead(sp->http, sp->fd, evb, DealWithSession, sp); + time(&sp->t_resp); + TAILQ_INSERT_TAIL(&sesshead, sp, list); + http_RecvHead(sp->http, sp->fd, evb, vca_callback, sp); } static void @@ -182,8 +223,10 @@ accept_f(int fd, short event, void *arg) strlcat(sp->addr, " ", VCA_ADDRBUFSIZE); strlcat(sp->addr, port, VCA_ADDRBUFSIZE); VSL(SLT_SessionOpen, sp->fd, "%s", sp->addr); + time(&sp->t_resp); + TAILQ_INSERT_TAIL(&sesshead, sp, list); sp->http = http_New(); - http_RecvHead(sp->http, sp->fd, evb, DealWithSession, sp); + http_RecvHead(sp->http, sp->fd, evb, vca_callback, sp); } static void * @@ -199,6 +242,11 @@ vca_main(void *arg) event_base_set(evb, &pipe_e); event_add(&pipe_e, NULL); + evtimer_set(&tick_e, vca_tick, NULL); + event_base_set(evb, &tick_e); + + evtimer_add(&tick_e, &tick_rate); + ep = accept_e; for (u = 0; u < HERITAGE_NSOCKS; u++) { if (heritage.sock_local[u] >= 0) { @@ -258,5 +306,7 @@ void VCA_Init(void) { + tick_rate.tv_sec = 1; + tick_rate.tv_usec = 0; AZ(pthread_create(&vca_thread, NULL, vca_main, NULL)); } diff --git a/varnish-cache/bin/varnishd/cache_pool.c b/varnish-cache/bin/varnishd/cache_pool.c index 323ab3b1..31f8d66a 100644 --- a/varnish-cache/bin/varnishd/cache_pool.c +++ b/varnish-cache/bin/varnishd/cache_pool.c @@ -135,19 +135,10 @@ out: } void -DealWithSession(void *arg, int bad) +DealWithSession(void *arg) { struct sess *sp = arg; - if (bad) { - if (bad == 1) - vca_close_session(sp, "overflow"); - else - vca_close_session(sp, "no request"); - vca_return_session(sp); - return; - } - time(&sp->t_req); /* -- 2.39.5