]> err.no Git - varnish/commitdiff
Make it possible to refer to backend hosts (== simple backends) by name.
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Tue, 22 Jan 2008 09:55:36 +0000 (09:55 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Tue, 22 Jan 2008 09:55:36 +0000 (09:55 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2365 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/include/vrt.h
varnish-cache/lib/libvcl/vcc_backend.c
varnish-cache/lib/libvcl/vcc_compile.c
varnish-cache/lib/libvcl/vcc_compile.h
varnish-cache/lib/libvcl/vcc_fixed_token.c

index 94ba45715162c7c9a46a899f40877e82064bcbf9..25abbc1dbf34af20c7a246a8cce9d6feb93b2b7c 100644 (file)
@@ -43,6 +43,7 @@ struct sockaddr;
 struct vrt_backend_host {
        const char      *portname;
        const char      *hostname;
+       const char      *ident;
 };
 
 struct vrt_simple_backend {
index 0a8f7afeef42744d78c7873eb7ee9b8dae4f51bd..fd41543d40fc6f5fb996556bafe12081ec3bc98d 100644 (file)
@@ -69,19 +69,19 @@ CheckHostPort(const char *host, const char *port)
  */
 
 static void
-vcc_EmitBeIdent(struct tokenlist *tl, struct token *first, struct token *last)
+vcc_EmitBeIdent(struct vsb *v, const struct token *first, const struct token *last)
 {
 
-       Fc(tl, 0, "\t.ident =");
+       vsb_printf(v, "\t.ident =");
        while (first != last) {
                if (first->dec != NULL)
-                       Fc(tl, 0, "\n\t    \"\\\"\" %.*s \"\\\" \"",
+                       vsb_printf(v, "\n\t    \"\\\"\" %.*s \"\\\" \"",
                            PF(first));
                else
-                       Fc(tl, 0, "\n\t    \"%.*s \"", PF(first));
+                       vsb_printf(v, "\n\t    \"%.*s \"", PF(first));
                first = VTAILQ_NEXT(first, list);
        }
-       Fc(tl, 0, ",\n");
+       vsb_printf(v, ",\n");
 }
 
 /*--------------------------------------------------------------------
@@ -111,12 +111,27 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh)
        struct token *t_host = NULL, *t_fhost = NULL;
        struct token *t_port = NULL, *t_fport = NULL;
        const char *ep;
+       struct host *h;
 
        t_first = tl->t;
        *nbh = tl->nbackend_host++;
 
        if (tl->t->tok == ID) {
-               ErrInternal(tl);        /* Reference by name */
+               VTAILQ_FOREACH(h, &tl->hosts, list) {
+                       if (vcc_Teq(h->name, tl->t))
+                               break;
+               }
+               if (h == NULL) {
+                       vsb_printf(tl->sb, "Reference to unknown backend ");
+                       vcc_ErrToken(tl, tl->t);
+                       vsb_printf(tl->sb, " at\n");
+                       vcc_ErrWhere(tl, tl->t);
+                       return;
+               }
+               vcc_NextToken(tl);
+               ExpectErr(tl, ';');
+               vcc_NextToken(tl);
+               *nbh = h->hnum;
                return;
        }
        ExpectErr(tl, '{');
@@ -151,6 +166,7 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh)
                        ExpectErr(tl, CSTR);
                        assert(tl->t->dec != NULL);
                        if (t_host != NULL) {
+                               assert(t_fhost != NULL);
                                vsb_printf(tl->sb,
                                    "Multiple .host fields in backend: ");
                                vcc_ErrToken(tl, t_field);
@@ -167,6 +183,7 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh)
                        ExpectErr(tl, CSTR);
                        assert(tl->t->dec != NULL);
                        if (t_port != NULL) {
+                               assert(t_fport != NULL);
                                vsb_printf(tl->sb,
                                    "Multiple .port fields in backend: ");
                                vcc_ErrToken(tl, t_field);
@@ -223,6 +240,7 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh)
        }
 
        ExpectErr(tl, '}');
+       vcc_EmitBeIdent(tl->fh, t_first, tl->t);
        Fh(tl, 0, "};\n");
        vcc_NextToken(tl);
 }
@@ -230,36 +248,40 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh)
 void
 vcc_ParseSimpleBackend(struct tokenlist *tl)
 {
-       struct token *t_be = NULL;
        struct token *t_first;
+       struct host *h;
        int nbh;
 
+       h = TlAlloc(tl, sizeof *h);
        t_first = tl->t;                /* T_BACKEND */
 
        vcc_NextToken(tl);
 
        ExpectErr(tl, ID);              /* name */
-       t_be = tl->t;
+       h->name = tl->t;
        vcc_NextToken(tl);
 
        vcc_ParseBackendHost(tl, &nbh);
        ERRCHK(tl);
 
+       h->hnum = nbh;
+       VTAILQ_INSERT_TAIL(&tl->hosts, h, list);
+
        /* In the compiled vcl we use these macros to refer to backends */
        Fh(tl, 1, "\n#define VGC_backend_%.*s (VCL_conf.backend[%d])\n",
-           PF(t_be), tl->nbackend);
+           PF(h->name), tl->nbackend);
 
-       vcc_AddDef(tl, t_be, R_BACKEND);
+       vcc_AddDef(tl, h->name, R_BACKEND);
 
        Fi(tl, 0, "\tVRT_init_simple_backend(&VGC_backend_%.*s , &sbe_%.*s);\n",
-           PF(t_be), PF(t_be));
-       Ff(tl, 0, "\tVRT_fini_backend(VGC_backend_%.*s);\n", PF(t_be));
+           PF(h->name), PF(h->name));
+       Ff(tl, 0, "\tVRT_fini_backend(VGC_backend_%.*s);\n", PF(h->name));
 
        Fc(tl, 0, "\nstatic const struct vrt_simple_backend sbe_%.*s = {\n",
-           PF(t_be));
-       Fc(tl, 0, "\t.name = \"%.*s\",\n", PF(t_be));
+           PF(h->name));
+       Fc(tl, 0, "\t.name = \"%.*s\",\n", PF(h->name));
        Fc(tl, 0, "\t.host = &bh_%d,\n", nbh);
-       vcc_EmitBeIdent(tl, t_first, tl->t);
+       vcc_EmitBeIdent(tl->fc, t_first, tl->t);
        Fc(tl, 0, "};\n");
 
        tl->nbackend++;
index 9f6932d2181f74a9ee8026229babe7cae37fbaaa..a188e9f815a8ae042074eb9c312de39bfe6b564b 100644 (file)
@@ -478,6 +478,7 @@ vcc_NewTokenList(void)
 
        tl = calloc(sizeof *tl, 1);
        assert(tl != NULL);
+       VTAILQ_INIT(&tl->hosts);
        VTAILQ_INIT(&tl->membits);
        VTAILQ_INIT(&tl->tokens);
        VTAILQ_INIT(&tl->refs);
index 42c019b0073375a22be5c60ece288a6eb5ccc797..65cd678ff7ca89a22e3552cd0330e87eb19d4527 100644 (file)
@@ -59,12 +59,19 @@ struct token {
        char                    *dec;
 };
 
+struct host {
+       VTAILQ_ENTRY(host)      list;
+       unsigned                hnum;
+       struct token            *name;
+};
+
 VTAILQ_HEAD(tokenhead, token);
 
 struct tokenlist {
        struct tokenhead        tokens;
        VTAILQ_HEAD(, source)   sources;
        VTAILQ_HEAD(, membit)   membits;
+       VTAILQ_HEAD(, host)     hosts;
        unsigned                nsources;
        struct source           *src;
        struct token            *t;
index 0aaba94fc53d254bdc1dc724f4873fa49ce9da7a..27a8eb9d886cb5e201b87b089c1d65e387f2d899 100644 (file)
@@ -423,6 +423,7 @@ vcl_output_lang_h(struct vsb *sb)
        vsb_cat(sb, "struct vrt_backend_host {\n");
        vsb_cat(sb, "   const char      *portname;\n");
        vsb_cat(sb, "   const char      *hostname;\n");
+       vsb_cat(sb, "   const char      *ident;\n");
        vsb_cat(sb, "};\n");
        vsb_cat(sb, "\n");
        vsb_cat(sb, "struct vrt_simple_backend {\n");