r4512 - in trunk/varnish-tools: . cache-channels

tfheen at projects.linpro.no tfheen at projects.linpro.no
Fri Jan 29 13:16:31 CET 2010


Author: tfheen
Date: 2010-01-29 13:16:31 +0100 (Fri, 29 Jan 2010)
New Revision: 4512

Added:
   trunk/varnish-tools/cache-channels/
   trunk/varnish-tools/cache-channels/README
   trunk/varnish-tools/cache-channels/varnish-cc
   trunk/varnish-tools/cache-channels/varnish-cc.init
   trunk/varnish-tools/cache-channels/varnish-cc.yaml
Log:
Add cache channel implementation

Cache channels are useful for active cache management where a backend
publishes a feed of objects that are to be expired. See
http://ietfreport.isoc.org/idref/draft-nottingham-http-cache-channels/
for an expired internet draft about this.

Added: trunk/varnish-tools/cache-channels/README
===================================================================
--- trunk/varnish-tools/cache-channels/README	                        (rev 0)
+++ trunk/varnish-tools/cache-channels/README	2010-01-29 12:16:31 UTC (rev 4512)
@@ -0,0 +1,27 @@
+Description
+
+Perl script to read a cache channel feed and send PURGE requests to varnish.
+See http://www.mnot.net/blog/2008/01/04/cache_channels for a short
+introduction on what cache channels are.
+
+Requirements
+
+perl
+XML::Atom::Feed
+DateTime::Format::ISO8601
+HTTP::Request
+Sys::Syslog
+YAML
+
+Varnish Configuration
+
+See http://varnish.projects.linpro.no/wiki/VCLExamplePurging
+
+Installation
+
+varnish-cc      -> /usr/local/sbin/varnish-cc
+varnish-cc.init -> /etc/init.d/varnish-cc
+varnish-cc.yaml -> /etc/varnish-cc.yaml
+
+Edit /etc/varnish-cc.yaml.
+

Added: trunk/varnish-tools/cache-channels/varnish-cc
===================================================================
--- trunk/varnish-tools/cache-channels/varnish-cc	                        (rev 0)
+++ trunk/varnish-tools/cache-channels/varnish-cc	2010-01-29 12:16:31 UTC (rev 4512)
@@ -0,0 +1,84 @@
+#!/usr/bin/env perl
+#
+# cache control
+#
+
+
+use lib qw[ /root/inc ];
+
+use XML::Atom::Feed;
+use DateTime::Format::ISO8601;
+use HTTP::Request;
+use Sys::Syslog qw( :DEFAULT setlogsock);
+use YAML;
+use Data::Dumper;
+
+
+open my $fh, '<', '/etc/varnish-cc.yaml' or die "can't open config file: $!";
+my $config = YAML::LoadFile($fh);
+
+my $channel = $config->{'channel'};
+my $interval = $config->{'default_interval'}; 
+my $host = $config->{'host'};
+
+#
+#
+
+my $latest = DateTime->now(time_zone => "Europe/Oslo"); #DateTime::Format::ISO8601->parse_datetime("2006-04-13T11:23:42Z");
+my $last_entry = $latest; 
+my $cc_ns = XML::Atom::Namespace->new(dc => 'http://purl.org/syndication/cache-channel');
+my $ua = LWP::UserAgent->new;
+setlogsock('unix');
+
+sub invalidate {
+  my $e = $_[0];
+  
+  purge("http://localhost" . $e);
+
+}
+
+sub purge{
+  my $uri = $_[0];
+  my $req = HTTP::Request->new(PURGE => $uri);
+  my $response = $ua->request($req);
+  if ($response->is_error) {
+    openlog($0,'','user');
+    syslog('err', $uri . ": " . $response->status_line . "\n");
+    closelog;
+    #print STDERR $uri . ": " . $response->status_line . "\n";
+  }
+}
+
+sub get{
+  my $uri = $_[0];
+  my $req = HTTP::Request->new(GET => $uri);
+  my $response = $ua->request($req);
+  if ($response->is_error) {
+    openlog($0,'','user');
+    syslog('err', $uri . ": " . $response->status_line . "\n");
+    closelog;
+    #print STDERR $uri . ": " . $response->status_line . "\n";
+  }
+}
+
+while(1) {
+  my $feed = XML::Atom::Feed->new(URI->new($channel));
+  $interval = $feed->get($cc_ns, 'precision'); # FIXME
+  for my $entry ($feed->entries()) {
+    $t = $entry->updated();
+    $t =~ s/(?<=[+-]\d\d)(?=\d\d$)/:/;
+    my $entry_updated = DateTime::Format::ISO8601->parse_datetime($t);
+    if ($latest < $entry_updated) {
+      my $link = $entry->link->elem;
+      invalidate($link->getAttribute("href"));
+      if ($last_entry < $entry_updated) {
+        $last_entry = $entry_updated;
+      }
+    }
+  }
+  $latest = $last_entry;
+
+  sleep $interval;
+}
+
+

Added: trunk/varnish-tools/cache-channels/varnish-cc.init
===================================================================
--- trunk/varnish-tools/cache-channels/varnish-cc.init	                        (rev 0)
+++ trunk/varnish-tools/cache-channels/varnish-cc.init	2010-01-29 12:16:31 UTC (rev 4512)
@@ -0,0 +1,75 @@
+#!/bin/bash
+#
+# init script for varnish cache control
+#
+
+if [ -e /var/run/varnish-cc ] ; then
+  PID=$(</var/run/varnish-cc)
+else
+  PID="blæh"
+fi
+
+running() {
+  [ "x$(/sbin/pidof perl)" = "x$PID" ] 
+}
+
+start() {
+  if running ; then
+    echo "$0 is already running"
+    exit 1
+  fi
+  varnish-cc &
+  PID=$!
+  echo $PID > /var/run/varnish-cc
+  exit 0
+}
+
+stop() {
+  kill $PID
+  sleep 1
+  if running ; then
+    echo "$0 just won't quit"
+    exit 1
+  else
+    rm /var/run/varnish-cc
+    exit 0
+  fi
+}
+
+status() {
+  if running ; then
+    echo "varnish-cc is running as pid $PID"
+    exit 0
+  elif [ $PID = "blæh" ] ; then
+    # exited cleanly
+    echo "trouble"
+    exit 3
+  else
+    echo "trouble"
+    exit 1
+  fi
+}
+
+case "$1" in
+  "start")
+    start
+    ;;
+  "stop")
+    stop
+    ;;
+  "restart")
+    stop
+    start
+    ;;
+  "force-reload")
+    # you don't want to
+    exit 3
+    ;;
+  "status")
+    status
+    ;;
+  *)
+    echo "usage: $0 (start|stop|restart|status)"
+esac
+exit 0
+

Added: trunk/varnish-tools/cache-channels/varnish-cc.yaml
===================================================================
--- trunk/varnish-tools/cache-channels/varnish-cc.yaml	                        (rev 0)
+++ trunk/varnish-tools/cache-channels/varnish-cc.yaml	2010-01-29 12:16:31 UTC (rev 4512)
@@ -0,0 +1,3 @@
+channel: http://www.example.com:8888/atomizer/event/current
+default_interval: 30
+host: localhost:9001



More information about the varnish-commit mailing list