Skip to content

Instantly share code, notes, and snippets.

@nkallen
Created November 11, 2008 23:53
Show Gist options
  • Save nkallen/24033 to your computer and use it in GitHub Desktop.
Save nkallen/24033 to your computer and use it in GitHub Desktop.
C{
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
typedef int twitter_is_fresh_t(char *, char *);
static twitter_is_fresh_t *twitter_is_fresh_ptr = NULL;
void twitter_init() {
char *error;
void *library = NULL;
if (!twitter_is_fresh_ptr) {
library = dlopen("/usr/local/lib/freshyfresh.so", RTLD_NOW);
if (!library) {
fprintf (stderr, "Could not open library: %s\n", dlerror());
exit(1);
}
dlerror();
twitter_is_fresh_ptr = (twitter_is_fresh_t *)dlsym(library, "twitter_is_fresh");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "Could not resolve function pointer: %s\n", error);
exit(1);
}
}
}
int twitter_is_fresh(char *generation_key, char *etag) {
twitter_init();
return twitter_is_fresh_ptr(generation_key, etag);
}
}C
sub vcl_recv {
if (req.request != "GET" && req.request != "HEAD") {
pass;
}
// if (req.restarts > 1) {
// set req.grace = 0s;
// } else {
set req.grace = 5s;
// }
if (req.http.Authorization) {
lookup;
}
}
sub vcl_hash {
set req.hash += req.url;
set req.hash += req.http.Authorization;
hash;
}
sub vcl_fetch {
if (!obj.cacheable) {
pass;
}
set obj.grace = 5s;
deliver;
}
sub vcl_hit {
if (!obj.cacheable) {
pass;
}
call check_freshness;
deliver;
}
sub check_freshness {
C{
char *etag = VRT_GetHdr(sp, HDR_OBJ, "\005ETag:");
char *generation_key = VRT_GetHdr(sp, HDR_OBJ, "\021X-Generation-Key:");
if (!twitter_is_fresh(generation_key, etag)) {
VRT_l_obj_ttl(sp, 0);
VRT_done(sp, VCL_RET_RESTART);
}
}C
}
sub vcl_fetch {
deliver;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment