Skip to content

Instantly share code, notes, and snippets.

@lkarsten
Last active September 21, 2015 12:34
Show Gist options
  • Save lkarsten/f469b529f6fe44a3caf7 to your computer and use it in GitHub Desktop.
Save lkarsten/f469b529f6fe44a3caf7 to your computer and use it in GitHub Desktop.
#
# Example of pinterest.com -alike application deployment.
#
vcl 4.0;
import std;
import directors;
import cookie;
import header;
import var;
backend current1 { .host = "192.0.2.11"; .port = "8080"; }
backend current2 { .host = "192.0.2.12"; .port = "8080"; }
backend current3 { .host = "192.0.2.13"; .port = "8080"; }
backend current4 { .host = "192.0.2.14"; .port = "8080"; }
backend prev1 { .host = "192.0.2.11"; .port = "8081"; }
backend prev2 { .host = "192.0.2.12"; .port = "8081"; }
backend prev3 { .host = "192.0.2.13"; .port = "8081"; }
backend prev4 { .host = "192.0.2.14"; .port = "8081"; }
sub vcl_init {
new rr1 = directors.round_robin();
rr1.add_backend(current1); rr1.add_backend(current2);
rr1.add_backend(current3); rr1.add_backend(current4);
new rr2 = directors.round_robin();
rr2.add_backend(prev1); rr2.add_backend(prev2);
rr2.add_backend(prev3); rr2.add_backend(prev4);
}
sub vcl_recv {
if (req.url ~ "^/upgrade/current") {
var.global_set("prev", var.global_get("current"));
var.global_set("current", req.http.new_version); // or regsub from req.url.
return(synth(200, "OK"));
} else if (req.url == "/rollback") {
var.global_set("current", var.global_get("prev"));
}
if (req.http.AppVersion == var.global_get("current")) {
set req.backend_hint = rr1.backend();
} else if (req.http.AppVersion == var.global_get("prev")) {
set req.backend_hint = rr2.backend();
} else {
# assumed that the (cached) backend response sets AppVersion since not set.
if (std.random(0, 9) == 0) {
set req.http.AppVersion = var.global_get("current");
} else {
set req.http.AppVersion = var.global_get("prev");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment