You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert

4
+
5
+
Experimental extension of [m2cgen](https://github.com/BayesWitnesses/m2cgen) to export statistical models to [Varnish Configuration Language](https://varnish-cache.org/docs/trunk/users-guide/vcl.html), for use in the Varnish cache. Right now only Fastly-flavored VCL is the only target supported, though this could theoretically partially target core Varnish in the future.
6
+
7
+
## Examples
8
+
9
+
For code examples and their generated VCL outputs, see the [example_outputs](https://github.com/rubyroobs/m2vcl/tree/master/example_outputs) directory.
10
+
11
+
## Usage
12
+
13
+
Use `export_to_fastly_vcl` to export to Fastly-flavored VCL. The `export_to_fasty_vcl` function takes arguemnts `indent` (defaults to 4, indent size in the generated VCL) and `sub_name` (defaults to `score`, the prefix for the generated subroutine and input/output header names). Inputs for the subroutine can be set on the headers `req.http.<prefix>_input_<index>` and outputs will be set on the header `req.http.<prefix>_output_<index>`.
14
+
15
+
A working demo is available in [this Fastly fiddle](https://fiddle.fastlydemo.net/fiddle/754b1898), with the source provided below:
16
+
17
+
### Generating Python code
18
+
19
+
```
20
+
from sklearn.model_selection import train_test_split
set var.input_3 = std.atof(req.http.score_input_3);
43
+
44
+
declare local var.input_2 FLOAT;
45
+
set var.input_2 = std.atof(req.http.score_input_2);
46
+
47
+
declare local var.var0_0 FLOAT;
48
+
declare local var.var0_1 FLOAT;
49
+
declare local var.var0_2 FLOAT;
50
+
if (var.input_3 <= 0.800000011920929) {
51
+
set var.var0_0 = 1.0;
52
+
set var.var0_1 = 0.0;
53
+
set var.var0_2 = 0.0;
54
+
} else {
55
+
if (var.input_2 <= 4.950000047683716) {
56
+
set var.var0_0 = 0.0;
57
+
set var.var0_1 = 0.9166666666666666;
58
+
set var.var0_2 = 0.08333333333333333;
59
+
} else {
60
+
set var.var0_0 = 0.0;
61
+
set var.var0_1 = 0.02564102564102564;
62
+
set var.var0_2 = 0.9743589743589743;
63
+
}
64
+
}
65
+
set req.http.score_output_0 = var.var0_0;
66
+
set req.http.score_output_1 = var.var0_1;
67
+
set req.http.score_output_2 = var.var0_2;
68
+
return;
69
+
}
70
+
```
71
+
72
+
### VCL Usage
73
+
74
+
```
75
+
# VCL_DELIVER
76
+
set req.http.score_input_2 = "1.23456789";
77
+
set req.http.score_input_3 = "9.87654321";
78
+
call score;
79
+
set resp.http.Score-Result-0 = req.http.score_output_0;
80
+
set resp.http.Score-Result-1 = req.http.score_output_1;
81
+
set resp.http.Score-Result-2 = req.http.score_output_2;
82
+
```
83
+
84
+
## Known limitations
85
+
86
+
* Precision is limited due to limitations of Fastly, and will be lost for each subroutine the AST is broken down into due to the required float -> string -> float conversion.
87
+
* Only tested with a small subset of models i.e. highly experimental - make sure to sanity check outputs
88
+
89
+
## Todo
90
+
91
+
* Improve test coverage by performing end to end testing on Fastly
92
+
* Create tests for more models
93
+
* Support core Varnish (may require a VMOD to provide equivalent functionality of [Fastly's math trig](https://developer.fastly.com/reference/vcl/functions/math-trig/))
0 commit comments