Commit 680baec9 authored by Nils Goroll's avatar Nils Goroll

Update $Init to $Event and add a usage example

parent 5171433e
...@@ -6,17 +6,31 @@ server s1 { ...@@ -6,17 +6,31 @@ server s1 {
} -start } -start
varnish v1 -vcl+backend { varnish v1 -vcl+backend {
import std;
import example from "${vmod_topbuild}/src/.libs/libvmod_example.so"; import example from "${vmod_topbuild}/src/.libs/libvmod_example.so";
sub vcl_init {
std.log(example.info());
}
sub vcl_deliver { sub vcl_deliver {
set resp.http.hello = example.hello("World"); set resp.http.hello = example.hello("World");
set resp.http.info = example.info();
} }
} -start } -start
logexpect l1 -v v1 -g raw -d 1 {
expect 0 0 CLI {^Rd vcl.load}
expect 0 = VCL_Log {^vmod_example loaded at }
} -start
client c1 { client c1 {
txreq -url "/" txreq -url "/"
rxresp rxresp
expect resp.http.hello == "Hello, World" expect resp.http.hello == "Hello, World"
expect resp.http.info ~ "^vmod_example warmed at "
} }
client c1 -run client c1 -run
logexpect l1 -wait
...@@ -3,17 +3,66 @@ ...@@ -3,17 +3,66 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
/* need vcl.h before vrt.h for vmod_evet_f typedef */
#include "vcl.h"
#include "vrt.h" #include "vrt.h"
#include "cache/cache.h" #include "cache/cache.h"
#include "vtim.h"
#include "vcc_if.h" #include "vcc_if.h"
int const size_t infosz = 64;
init_function(struct vmod_priv *priv, const struct VCL_conf *conf) char *info;
/*
* handle vmod internal state, vmod init/fini and/or varnish callback
* (un)registration here.
*
* malloc'ing the info buffer is only indended as a demonstration, for any
* real-world vmod, a fixed-sized buffer should be a global variable
*/
int __match_proto__(vmod_event_f)
event_function(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
{ {
char ts[VTIM_FORMAT_SIZE];
const char *event = NULL;
(void) priv;
switch (e) {
case VCL_EVENT_LOAD:
info = malloc(infosz);
if (! info)
return (-1);
event = "loaded";
break;
case VCL_EVENT_WARM:
event = "warmed";
break;
case VCL_EVENT_COLD:
event = "cooled";
break;
case VCL_EVENT_DISCARD:
free(info);
return (0);
break;
default:
return (0);
}
AN(event);
VTIM_format(VTIM_real(), ts);
snprintf(info, infosz, "vmod_example %s at %s", event, ts);
return (0); return (0);
} }
VCL_STRING
vmod_info(VRT_CTX)
{
return (info);
}
VCL_STRING VCL_STRING
vmod_hello(const struct vrt_ctx *ctx, VCL_STRING name) vmod_hello(const struct vrt_ctx *ctx, VCL_STRING name)
{ {
......
...@@ -20,7 +20,12 @@ You can even have links and lists in here: ...@@ -20,7 +20,12 @@ You can even have links and lists in here:
The init-function declared next does not have documentation. The init-function declared next does not have documentation.
$Init init_function $Event event_function
$Function STRING info()
Returns a string set by the last VCL event, demonstrating the use of
event functions.
$Function STRING hello(STRING) $Function STRING hello(STRING)
The different functions provided by the VMOD should also have their own The different functions provided by the VMOD should also have their own
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment