Skip to content

Instantly share code, notes, and snippets.

@rezan
Last active December 1, 2020 18:40
Show Gist options
  • Save rezan/8a2cc08df9588bb4f20f270359c3d8fe to your computer and use it in GitHub Desktop.
Save rezan/8a2cc08df9588bb4f20f270359c3d8fe to your computer and use it in GitHub Desktop.
Probe Proxy implementation
#
# Probe Proxy v1.0
#
import directors;
import kvstore;
import probe_proxy;
import std;
acl probe_proxy_local {
"127.0.0.0"/8;
"::1"/128;
}
sub vcl_init
{
# Settings
new probe_proxy_opts = kvstore.init();
# Route all probes to this VCL
probe_proxy.global(probe_proxy.self());
# All probes will be forwarded to this gateway
# Parent VCL fills this in with probe gateway backends
new probe_proxy_gateway = directors.fallback();
}
sub vcl_recv
{
if (probe_proxy.is_probe()) {
# Security
if (probe_proxy_opts.get("token")) {
if (client.ip ~ probe_proxy_local) {
# TODO sign
} else {
# TODO validate
}
}
return (hash);
}
}
sub vcl_hash
{
if (probe_proxy.is_probe()) {
hash_data(req.url);
hash_data(probe_proxy.get_backend_path());
return (lookup);
}
}
sub vcl_backend_fetch
{
if (probe_proxy.is_probe()) {
# Use the probe gateway
if (std.health(probe_proxy_gateway.backend()) && !probe_proxy.is_marked() && bereq.retries == 0)) {
set bereq.backend = probe_proxy_gateway.backend();
# Dont loop around
probe_proxy.mark();
}
# We are a real probe, goto the probe backend
else {
if (probe_proxy.get_backend()) {
set beresp.backend = probe_proxy.get_backend();
} else {
set beresp.backend = goto.dns_backend(probe_proxy.get_backend_path());
}
probe_proxy.clear();
probe_proxy.force_fresh();
set bereq.first_byte_timeout = probe_proxy.get_timeout();
}
return (fetch);
}
}
sub vcl_backend_response
{
if (probe_proxy.is_probe()) {
set beresp.ttl = probe_proxy.get_interval();
set beresp.grace = 0s;
set beresp.keep = 0s;
return (deliver);
}
}
sub vcl_backend_error
{
if (probe_proxy.is_probe()) {
if (bereq.retries == 0 && !probe_proxy.is_marked()) {
return (retry);
}
}
}
sub vcl_deliver
{
# Strip the body
if (probe_proxy.is_probe()) {
return (synth(resp.status, resp.reason));
}
}
sub vcl_synth
{
if (probe_proxy.is_probe()) {
synthetic("Varnish probe proxy");
return (deliver);
}
}
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment