]> err.no Git - varnish/commitdiff
Add the beginning of a backend connection pool
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 30 Mar 2006 09:26:34 +0000 (09:26 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 30 Mar 2006 09:26:34 +0000 (09:26 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@86 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/Makefile.am
varnish-cache/bin/varnishd/cache.h
varnish-cache/bin/varnishd/cache_backend.c [new file with mode: 0644]
varnish-cache/bin/varnishd/cache_main.c
varnish-cache/bin/varnishd/cache_pool.c
varnish-cache/bin/varnishd/varnishd.c
varnish-cache/include/vcl_lang.h
varnish-cache/lib/libvcl/vcl_compile.c
varnish-cache/lib/libvcl/vcl_fixed_token.c

index a822cc26ce8459b1d5a727bccf5751fc845070cf..ddea6a705200befc0b04b5625053687d9c7ccf4d 100644 (file)
@@ -6,6 +6,7 @@ bin_PROGRAMS = varnishd
 
 varnishd_SOURCES = \
        cache_acceptor.c \
+       cache_backend.c \
        cache_httpd.c \
        cache_main.c \
        cache_pool.c \
index 2e87481bf9055619ed2d796d61920202d4bf0611..e69b631aaa6952ccb2ce5df2ed67aa0cc8a19ee8 100644 (file)
@@ -5,6 +5,9 @@
 /* cache_acceptor.c */
 void *vca_main(void *arg);
 
+/* cache_backend.c */
+void VBE_Init(void);
+
 /* cache_httpd.c */
 void HttpdAnalyze(struct sess *sp);
 
diff --git a/varnish-cache/bin/varnishd/cache_backend.c b/varnish-cache/bin/varnishd/cache_backend.c
new file mode 100644 (file)
index 0000000..84f8fe0
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * $Id$
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <sys/queue.h>
+#include "libvarnish.h"
+#include "vcl_lang.h"
+
+/*
+ * The internal backend structure for managing connection pools per
+ * backend.  We need to shadow the backend stucture from the VCL
+ * in order let connections live across VCL switches.
+ */
+
+struct vbe_conn {
+       TAILQ_ENTRY(vbe_conn)   list;
+       struct vbe              *vbe;
+       int                     fd;
+};
+
+struct vbe {
+       unsigned                ip;
+       TAILQ_ENTRY(vbe)        list;
+       TAILQ_HEAD(,vbe_conn)   fconn;
+       TAILQ_HEAD(,vbe_conn)   bconn;
+       unsigned                nconn;
+};
+
+static TAILQ_HEAD(,vbe) vbe_head = TAILQ_HEAD_INITIALIZER(vbe_head);
+
+static pthread_mutex_t vbemtx;
+
+/*--------------------------------------------------------------------*/
+void
+connect_to_backend(struct vbe_conn *vc, struct backend *bp)
+{
+}
+
+/*--------------------------------------------------------------------*/
+
+int
+VBE_GetFd(struct backend *bp)
+{
+       struct vbe *vp;
+       struct vbe_conn *vc;
+
+       AZ(pthread_mutex_lock(&vbemtx));
+       vp = bp->vbe;
+       if (vp == NULL) {
+               TAILQ_FOREACH(vp, &vbe_head, list)
+                       if (vp->ip == bp->ip)
+                               break;
+       }
+       if (vp == NULL) {
+               vp = calloc(sizeof *vp, 1);
+               assert(vp != NULL);
+               TAILQ_INIT(&vp->fconn);
+               TAILQ_INIT(&vp->bconn);
+               vp->ip = bp->ip;
+               bp->vbe = vp;
+               TAILQ_INSERT_TAIL(&vbe_head, vp, list);
+       }
+       /* XXX: check nconn vs backend->maxcon */
+       vc = TAILQ_FIRST(&vp->fconn);
+       if (vc != NULL) {
+               TAILQ_REMOVE(&vp->fconn, vc, list);
+               TAILQ_INSERT_TAIL(&vp->bconn, vc, list);
+               AZ(pthread_mutex_unlock(&vbemtx));
+               return (vc->fd);
+       }
+       vc = calloc(sizeof *vc, 1);
+       assert(vc != NULL);
+       vc->vbe = vp;
+       TAILQ_INSERT_TAIL(&vp->bconn, vc, list);
+       AZ(pthread_mutex_unlock(&vbemtx));
+       connect_to_backend(vc, bp);
+
+       /* XXX */
+       return (-1);
+}
+
+
+
+
+void
+VBE_Init(void)
+{
+
+       AZ(pthread_mutex_init(&vbemtx, NULL));
+}
index 9fcb1c60f3c76debf09f68087edb9de7b107496b..e67101d192f1ed275127c6c8d223d6c509fa8370 100644 (file)
@@ -103,6 +103,7 @@ child_main(void)
        printf("Child starts\n");
 
        AZ(pthread_mutex_init(&sessmtx, NULL));
+       VBE_Init();
        VSL_Init();
        CacheInitPool();
 
index 58cc00283dc00ca3d0ff7a37a109d46a4f2c071e..0e294c45ad15ab6389261e1c52d422cff5b48836 100644 (file)
@@ -33,6 +33,7 @@ CacheWorker(void *priv __unused)
 
                HttpdAnalyze(sp);
 
+               sp->backend = sp->vcl->default_backend;
                /* Call the VCL program */
                sp->vcl->main_func(sp);
 
index 6266f63878c8dccfe215be41ad9b9920bb4134ab..3ee9f9b8b894b584cd56881ed18f2f8a7a480aac 100644 (file)
@@ -68,7 +68,7 @@ vcl_default(const char *bflag)
 
        buf = NULL;
        asprintf(&buf,
-           "backend default { set backend.ip = %s; }\n"
+           "backend default { set backend.host = \"%s\"; }\n"
            "sub main {\n"
            "    pass;\n"
 #if 0
index fcb5e0951e6722979cedffe72cb10860122616b4..182a20258c421792eb9453657d8cd8dd094e9477 100644 (file)
@@ -53,6 +53,7 @@ struct sess {
 
        TAILQ_ENTRY(sess)       list;
 
+       struct backend          *backend;
        struct VCL_conf         *vcl;
 
        /* Various internal stuff */
@@ -60,21 +61,18 @@ struct sess {
        struct sessmem          *mem;
 };
 
-struct be_conn {
-       TAILQ_ENTRY(be_conn)    list;
-       int                     fd;
-};
-
 struct backend {
+       const char      *hostname;
+       const char      *portname;
+       struct addrinfo *addr;
        unsigned        ip;
        double          responsetime;
        double          timeout;
        double          bandwidth;
        int             down;
 
-       /* Internals */
-       TAILQ_HEAD(,be_conn)    bec_head;
-       unsigned                nbec;
+       /* internal stuff */
+       struct vbe      *vbe;
 };
 
 #define VCL_FARGS      struct sess *sess
index eeb31b33482625dfc526500d03bc07db51055223..47f7f49b7299e397cfcf096c7fb06e8dc04f603f 100644 (file)
@@ -109,6 +109,9 @@ struct ref {
 
 
 static struct var vars[] = {
+       { "backend.host",               STRING, 0,  "backend->hostname"    },
+       { "backend.port",               STRING, 0,  "backend->portname"    },
+#if 0
        { "req.ttlfactor",              FLOAT, 0,   "req->ttlfactor" },
        { "req.url.host",               STRING, 0,  "req->url.host" },
        { "req.url.path",               STRING, 0,  "req->url.path" },
@@ -116,7 +119,6 @@ static struct var vars[] = {
        { "req.backend",                BACKEND, 0, "req->backend"   },
        { "client.ip",                  IP, 0,      "client->ip"     },
        { "backend.response_time",      TIME, 0,    "backend->responsetime" },
-       { "backend.ip",                 IP, 0,      "backend->ip"    },
        { "backend.down",               BOOL, 0,    "backend->down"  },
        { "backend.timeout",            TIME, 0,    "backend->timeout" },
        { "backend.bandwidth",          RATE, 0,    "backend->bandwidth" },
@@ -125,6 +127,7 @@ static struct var vars[] = {
        { "obj.result",                 INT, 0,     "obj->result" },
        { "obj.size",                   SIZE, 0,    "obj->size" },
        { "obj.usage",                  INT, 0,     "obj->usage" },
+#endif
        { NULL,                         INT, 0,     "NULL" }
 };
 
index 9fd5f86e710114845b5950baf256713d1ba6244a..80733ce4dd4aa31a32f3ab81bcf23dbe20a04870 100644 (file)
@@ -434,6 +434,7 @@ vcl_output_lang_h(FILE *f)
        fputs("\n", f);
        fputs(" TAILQ_ENTRY(sess)       list;\n", f);
        fputs("\n", f);
+       fputs(" struct backend          *backend;\n", f);
        fputs(" struct VCL_conf         *vcl;\n", f);
        fputs("\n", f);
        fputs(" /* Various internal stuff */\n", f);
@@ -441,21 +442,18 @@ vcl_output_lang_h(FILE *f)
        fputs(" struct sessmem          *mem;\n", f);
        fputs("};\n", f);
        fputs("\n", f);
-       fputs("struct be_conn {\n", f);
-       fputs(" TAILQ_ENTRY(be_conn)    list;\n", f);
-       fputs(" int                     fd;\n", f);
-       fputs("};\n", f);
-       fputs("\n", f);
        fputs("struct backend {\n", f);
+       fputs(" const char      *hostname;\n", f);
+       fputs(" const char      *portname;\n", f);
+       fputs(" struct addrinfo *addr;\n", f);
        fputs(" unsigned        ip;\n", f);
        fputs(" double          responsetime;\n", f);
        fputs(" double          timeout;\n", f);
        fputs(" double          bandwidth;\n", f);
        fputs(" int             down;\n", f);
        fputs("\n", f);
-       fputs(" /* Internals */\n", f);
-       fputs(" TAILQ_HEAD(,be_conn)    bec_head;\n", f);
-       fputs(" unsigned                nbec;\n", f);
+       fputs(" /* internal stuff */\n", f);
+       fputs(" struct vbe      *vbe;\n", f);
        fputs("};\n", f);
        fputs("\n", f);
        fputs("#define VCL_FARGS        struct sess *sess\n", f);