Skip to content

VIP9: Expand VCL object support

Reza Naghibi edited this page Dec 2, 2019 · 26 revisions

Synopsis

Currently objects are limited to global objects which have a lifetime of the entire VCL. This VIP is to have objects which can be created during the request and their scope is limited to that request. When the request is done, the objects are fini'ed.

Original VIP9 date: April 25, 2016

Updated VIP9 date: November 18, 2019

See also: VIP9B

Why?

By allowing VMOD objects to live in the request scope, we can easily bring in OO style types and high level programming constructs to VCL. For example: integers, strings, doubles, lists, hash tables, encryption, encoding, anything and everything.

Note that the sole purpose of this VIP is not to bring basic types to VCL. Rather, its to allow high level constructs to live in the request scope.

VCL Example

Here is a VCL snippet which compiles and works with the patch [0] and uses a new libvmod_types [1].

vcl 4.1;

import types;

sub vcl_recv
{
  //new req scoped objects
  new slocal = types.string("Request scoped string");
  new s2 = types.string("request string two");
  new count = types.integer(1);
}

sub vcl_backend_fetch
{
  //new bereq scoped objects
  new sbe = types.string("berequest string v1");
  set bereq.http.sbe = sbe.value();

  sbe.set("berequest string v2");
  set bereq.http.sbe2 = sbe.value();
}

sub vcl_deliver
{
  //referencing objects
  set resp.http.X-slocal = slocal.value();
  set resp.http.X-slocal-length = slocal.length();

  count.increment(10);
  set resp.http.count = count.value();
}

Another example of using an HTTP vmod in a request scope:

vcl 4.1;

import http;

sub vcl_recv
{
  new server_ping = http.init();
  server_ping.req_set_url("https://server.co/api/ping");
  server_ping.req_send();
}

sub vcl_deliver
{
  server_ping.resp_wait();
  set resp.http.X-ping = server_ping.resp_get_status();
}

[-1] 2016 https://github.com/rezan/varnish-cache/commit/b547bd9ad2fca9db1ef17ee73b8e9b7df9950c34

[0] 2019 https://github.com/varnishcache/varnish-cache/compare/master...rezan:req_objects

[1] https://github.com/rezan/libvmod-types

Clone this wiki locally