]> err.no Git - varnish/commitdiff
Take the vbe_conn (backend connection) structure out of the closet.
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Mon, 10 Jul 2006 07:54:05 +0000 (07:54 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Mon, 10 Jul 2006 07:54:05 +0000 (07:54 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@391 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/cache.h
varnish-cache/bin/varnishd/cache_backend.c
varnish-cache/bin/varnishd/cache_fetch.c
varnish-cache/bin/varnishd/cache_pass.c
varnish-cache/bin/varnishd/cache_pipe.c

index 5d2884eaee8f14e85960cc64fe3bb97c18710b75..f1f49df37e64de49abe8fdc8d6364ddb95c28144 100644 (file)
@@ -57,6 +57,16 @@ struct worker {
 
 #include "hash_slinger.h"
 
+/* Backend Connection ------------------------------------------------*/
+
+struct vbe_conn {
+       TAILQ_ENTRY(vbe_conn)   list;
+       struct vbe              *vbe;
+       int                     fd;
+       struct event            ev;
+       int                     inuse;
+};
+
 /* Storage -----------------------------------------------------------*/
 
 struct storage {
@@ -172,9 +182,9 @@ void VCA_Init(void);
 
 /* cache_backend.c */
 void VBE_Init(void);
-int VBE_GetFd(struct backend *bp, void **ptr, unsigned xid);
-void VBE_ClosedFd(void *ptr);
-void VBE_RecycleFd(void *ptr);
+struct vbe_conn *VBE_GetFd(struct backend *bp, unsigned xid);
+void VBE_ClosedFd(struct vbe_conn *vc);
+void VBE_RecycleFd(struct vbe_conn *vc);
 
 /* cache_ban.c */
 void BAN_Init(void);
index 06ee4a9e49bda2a40f7ef4529c3c09fc114b1adc..3b4cbf03b44c4a958c7797f9c7ac2f4920ea6f99 100644 (file)
 #include "shmlog.h"
 #include "cache.h"
 
-/* A backend connection */
-
-struct vbe_conn {
-       TAILQ_ENTRY(vbe_conn)   list;
-       struct vbe              *vbe;
-       int                     fd;
-       struct event            ev;
-       int                     inuse;
-};
-
 /* A backend IP */
 
 struct vbe {
@@ -211,8 +201,8 @@ vbe_main(void *priv)
  * new connection.
  */
 
-int
-VBE_GetFd(struct backend *bp, void **ptr, unsigned xid)
+struct vbe_conn *
+VBE_GetFd(struct backend *bp, unsigned xid)
 {
        struct vbe *vp;
        struct vbe_conn *vc;
@@ -252,24 +242,31 @@ VBE_GetFd(struct backend *bp, void **ptr, unsigned xid)
                vp->nconn++;
                AZ(pthread_mutex_unlock(&vbemtx));
                connect_to_backend(vc, bp);
-               VSL_stats->backend_conn++;
-               event_set(&vc->ev, vc->fd, EV_READ | EV_PERSIST, vbe_rdf, vc);
-               event_base_set(vbe_evb, &vc->ev);
+               if (vc->fd < 0) {
+                       AZ(pthread_mutex_lock(&vbemtx));
+                       TAILQ_REMOVE(&vc->vbe->bconn, vc, list);
+                       vp->nconn--;
+                       AZ(pthread_mutex_unlock(&vbemtx));
+                       free(vc);
+                       vc = NULL;
+               } else {
+                       VSL_stats->backend_conn++;
+                       event_set(&vc->ev, vc->fd, EV_READ | EV_PERSIST, vbe_rdf, vc);
+                       event_base_set(vbe_evb, &vc->ev);
+               }
        }
-       *ptr = vc;
-       VSL(SLT_BackendXID, vc->fd, "%u", xid);
-       return (vc->fd);
+       if (vc != NULL)
+               VSL(SLT_BackendXID, vc->fd, "%u", xid);
+       return (vc);
 }
 
 /* Close a connection ------------------------------------------------*/
 
 void
-VBE_ClosedFd(void *ptr)
+VBE_ClosedFd(struct vbe_conn *vc)
 {
-       struct vbe_conn *vc;
        int i;
 
-       vc = ptr;
        VSL(SLT_BackendClose, vc->fd, "");
        close(vc->fd);
        vc->fd = -1;
@@ -280,13 +277,11 @@ VBE_ClosedFd(void *ptr)
 /* Recycle a connection ----------------------------------------------*/
 
 void
-VBE_RecycleFd(void *ptr)
+VBE_RecycleFd(struct vbe_conn *vc)
 {
-       struct vbe_conn *vc;
        int i;
 
        VSL_stats->backend_recycle++;
-       vc = ptr;
        VSL(SLT_BackendReuse, vc->fd, "");
        i = write(vbe_pipe[1], &vc, sizeof vc);
        assert(i == sizeof vc);
index 41088e89f9a4147013716f4317568ba3109d41f1..3c9c7df7c688c6fbfa28033f4ebd7afbdb8055ce 100644 (file)
@@ -232,23 +232,23 @@ fetch_eof(struct worker *w, struct sess *sp, int fd, struct http *hp)
 int
 FetchSession(struct worker *w, struct sess *sp)
 {
-       int fd, i, cls;
-       void *fd_token;
+       int i, cls;
+       struct vbe_conn *vc;
        struct http *hp;
        char *b;
        int body;
 
        sp->obj->xid = sp->xid;
 
-       fd = VBE_GetFd(sp->backend, &fd_token, sp->xid);
-       if (fd == -1)
-               fd = VBE_GetFd(sp->backend, &fd_token, sp->xid);
-       assert(fd != -1);       /* XXX: handle this */
-       VSL(SLT_Backend, sp->fd, "%d %s", fd, sp->backend->vcl_name);
+       vc = VBE_GetFd(sp->backend, sp->xid);
+       if (vc == NULL)
+               vc = VBE_GetFd(sp->backend, sp->xid);
+       assert(vc != NULL);     /* XXX: handle this */
+       VSL(SLT_Backend, sp->fd, "%d %s", vc->fd, sp->backend->vcl_name);
 
        hp = http_New();
-       http_BuildSbuf(fd, Build_Fetch, w->sb, sp->http);
-       i = write(fd, sbuf_data(w->sb), sbuf_len(w->sb));
+       http_BuildSbuf(vc->fd, Build_Fetch, w->sb, sp->http);
+       i = write(vc->fd, sbuf_data(w->sb), sbuf_len(w->sb));
        assert(i == sbuf_len(w->sb));
        time(&sp->t_req);
 
@@ -258,10 +258,10 @@ FetchSession(struct worker *w, struct sess *sp)
         * XXX: It might be cheaper to avoid the event_engine and simply
         * XXX: read(2) the header
         */
-       http_RecvHead(hp, fd, w->eb, NULL, NULL);
+       http_RecvHead(hp, vc->fd, w->eb, NULL, NULL);
        event_base_loop(w->eb, 0);
        time(&sp->t_resp);
-       assert(http_Dissect(hp, fd, 2) == 0);
+       assert(http_Dissect(hp, vc->fd, 2) == 0);
 
        body = RFC2616_cache_policy(sp, hp);
 
@@ -273,11 +273,11 @@ FetchSession(struct worker *w, struct sess *sp)
        http_BuildSbuf(sp->fd, Build_Reply, w->sb, hp);
        if (body) {
                if (http_GetHdr(hp, "Content-Length", &b))
-                       cls = fetch_straight(w, sp, fd, hp, b);
+                       cls = fetch_straight(w, sp, vc->fd, hp, b);
                else if (http_HdrIs(hp, "Transfer-Encoding", "chunked"))
-                       cls = fetch_chunked(w, sp, fd, hp);
+                       cls = fetch_chunked(w, sp, vc->fd, hp);
                else 
-                       cls = fetch_eof(w, sp, fd, hp);
+                       cls = fetch_eof(w, sp, vc->fd, hp);
                sbuf_printf(w->sb, "Content-Length: %u\r\n", sp->obj->len);
        } else
                cls = 0;
@@ -291,9 +291,9 @@ FetchSession(struct worker *w, struct sess *sp)
                cls = 1;
 
        if (cls)
-               VBE_ClosedFd(fd_token);
+               VBE_ClosedFd(vc);
        else
-               VBE_RecycleFd(fd_token);
+               VBE_RecycleFd(vc);
 
        HSH_Unbusy(sp->obj);
        if (!sp->obj->cacheable)
index 2e0e9eedfa7dd655f863fbea8d54231b3a8f731e..a931bb672a585366ae56d681db85ebaec4744f33 100644 (file)
@@ -147,17 +147,17 @@ pass_chunked(struct worker *w, struct sess *sp, int fd, struct http *hp)
 void
 PassSession(struct worker *w, struct sess *sp)
 {
-       int fd, i;
-       void *fd_token;
+       int i;
+       struct vbe_conn *vc;
        struct http *hp;
        char *b;
        int cls;
 
-       fd = VBE_GetFd(sp->backend, &fd_token, sp->xid);
-       assert(fd != -1);
+       vc = VBE_GetFd(sp->backend, sp->xid);
+       assert(vc != NULL);
 
-       http_BuildSbuf(fd, Build_Pass, w->sb, sp->http);
-       i = write(fd, sbuf_data(w->sb), sbuf_len(w->sb));
+       http_BuildSbuf(vc->fd, Build_Pass, w->sb, sp->http);
+       i = write(vc->fd, sbuf_data(w->sb), sbuf_len(w->sb));
        assert(i == sbuf_len(w->sb));
 
        /* XXX: copy any contents */
@@ -167,19 +167,19 @@ PassSession(struct worker *w, struct sess *sp)
         * XXX: read(2) the header
         */
        hp = http_New();
-       http_RecvHead(hp, fd, w->eb, NULL, NULL);
+       http_RecvHead(hp, vc->fd, w->eb, NULL, NULL);
        event_base_loop(w->eb, 0);
-       http_Dissect(hp, fd, 2);
+       http_Dissect(hp, vc->fd, 2);
 
        http_BuildSbuf(sp->fd, Build_Reply, w->sb, hp);
        vca_write(sp, sbuf_data(w->sb), sbuf_len(w->sb));
 
        if (http_GetHdr(hp, "Content-Length", &b))
-               cls = pass_straight(w, sp, fd, hp, b);
+               cls = pass_straight(w, sp, vc->fd, hp, b);
        else if (http_HdrIs(hp, "Connection", "close"))
-               cls = pass_straight(w, sp, fd, hp, NULL);
+               cls = pass_straight(w, sp, vc->fd, hp, NULL);
        else if (http_HdrIs(hp, "Transfer-Encoding", "chunked"))
-               cls = pass_chunked(w, sp, fd, hp);
+               cls = pass_chunked(w, sp, vc->fd, hp);
        else {
                INCOMPL();
                cls = 1;
@@ -190,7 +190,7 @@ PassSession(struct worker *w, struct sess *sp)
                cls = 1;
 
        if (cls)
-               VBE_ClosedFd(fd_token);
+               VBE_ClosedFd(vc);
        else
-               VBE_RecycleFd(fd_token);
+               VBE_RecycleFd(vc);
 }
index 1067bebad9ec3fa5a4d4333d72f5ff8bd5c759b4..6e58e81ad6dcf13b5d30317a3bb36afe018fb9b1 100644 (file)
@@ -44,36 +44,35 @@ rdf(int fd, short event, void *arg)
 void
 PipeSession(struct worker *w, struct sess *sp)
 {
-       int fd, i;
-       void *fd_token;
+       int i;
+       struct vbe_conn *vc;
        struct edir e1, e2;
        char *b, *e;
 
-       fd = VBE_GetFd(sp->backend, &fd_token, sp->xid);
-       assert(fd != -1);
+       vc = VBE_GetFd(sp->backend, sp->xid);
+       assert(vc != NULL);
 
-       http_BuildSbuf(fd, Build_Pipe, w->sb, sp->http);
-       i = write(fd, sbuf_data(w->sb), sbuf_len(w->sb));
+       http_BuildSbuf(vc->fd, Build_Pipe, w->sb, sp->http);
+       i = write(vc->fd, sbuf_data(w->sb), sbuf_len(w->sb));
        assert(i == sbuf_len(w->sb));
        if (http_GetTail(sp->http, 99999999, &b, &e) && b != e) { /* XXX */
-               i = write(fd, b, e - b);
+               i = write(vc->fd, b, e - b);
                if (i != e - b) {
-                       close (fd);
+                       close (vc->fd);
                        vca_close_session(sp, "pipe");
-                       VBE_ClosedFd(fd_token);
+                       VBE_ClosedFd(vc);
                }
        }
 
-       e1.fd = fd;
+       e1.fd = vc->fd;
        e2.fd = sp->fd;
        event_set(&e1.ev, sp->fd, EV_READ | EV_PERSIST, rdf, &e1);
        event_base_set(w->eb, &e1.ev);
-       event_set(&e2.ev, fd,     EV_READ | EV_PERSIST, rdf, &e2);
+       event_set(&e2.ev, vc->fd, EV_READ | EV_PERSIST, rdf, &e2);
        event_base_set(w->eb, &e2.ev);
        event_add(&e1.ev, NULL);
        event_add(&e2.ev, NULL);
        event_base_loop(w->eb, 0);
-       close (fd);
        vca_close_session(sp, "pipe");
-       VBE_ClosedFd(fd_token);
+       VBE_ClosedFd(vc);
 }