Mailing List Archive

vcl_init, VMODs & workspaces
Hi all,

I'm working on a VMOD (VC 4.1) and I would like to create a simple function able to return string values. You can think these strings as values of variables previously set somewhere else (e.g. environment variables) and used during vcl_init to make some decisions.

This would be trivial stuff outside of vcl_init (i.e. return WS_Copy(ctx->ws, "whatever", -1)), but ctx->ws is not available during vcl_init. Is there any friendly alternative not requiring the VMOD doing explicit mallocs when returning strings, keeping track of returned pointers, and releasing them when execution of vcl_init is completed?

Thanks,

--
Carlos Abalde


_______________________________________________
varnish-dev mailing list
varnish-dev@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev
Re: vcl_init, VMODs & workspaces [ In reply to ]
> This would be trivial stuff outside of vcl_init (i.e. return WS_Copy(ctx->ws, "whatever", -1)), but ctx->ws is not available during vcl_init. Is there any friendly alternative not requiring the VMOD doing explicit mallocs when returning strings, keeping track of returned pointers, and releasing them when execution of vcl_init is completed?

Hi Carlos,

There's no escape to explicit (m)allocs, but Varnish can help you
keeping track of your allocations.

If you decilare an $Event [1] function in your VCC file you will
receive at LOAD-time a PRIV_VCL private pointer [2] to track per-VCL
data. This can be used by your module for functions that need to
allocate during vcl_init{} but there's no END-OF-INIT event you could
catch to free this state early, so unless you provide a specific
function to do the cleaning, your free callback will be called later
when the VCL is discarded.

Cheers,
Dridi

[1] https://www.varnish-cache.org/docs/4.1/reference/vmod.html#event-functions
[2] https://www.varnish-cache.org/docs/4.1/reference/vmod.html#private-pointers

_______________________________________________
varnish-dev mailing list
varnish-dev@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev
Re: vcl_init, VMODs & workspaces [ In reply to ]
> On 04 May 2016, at 09:13, Dridi Boukelmoune <dridi@varni.sh> wrote:
> ...
>
> If you decilare an $Event [1] function in your VCC file you will
> receive at LOAD-time a PRIV_VCL private pointer [2] to track per-VCL
> data. This can be used by your module for functions that need to
> allocate during vcl_init{} but there's no END-OF-INIT event you could
> catch to free this state early, so unless you provide a specific
> function to do the cleaning, your free callback will be called later
> when the VCL is discarded.

Thanks Dridi. In fact I'm considering implementing a private VMOD's workspace in PRIV_VCL using WS_Init() in order to reuse WS_* functions and keep life simple when calling VMOD functions from vcl_init and a ctx->ws is not available. I wanted to check I wasn't missing anything here :)

Thanks,

--
Carlos Abalde


_______________________________________________
varnish-dev mailing list
varnish-dev@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev
Re: vcl_init, VMODs & workspaces [ In reply to ]
just to follow up, we've got both workspace and PRIV_TASK for vcl events
(including vcl_init) now in 5.0/master.

Nils

_______________________________________________
varnish-dev mailing list
varnish-dev@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev