From bf45bbea5673d6385d2e77da3adb41642d55a172 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Wed, 12 Feb 2014 15:34:19 +0100 Subject: [PATCH] Do not send multiple equal commands to Varnish on connect Closes #135 Also accept varnish "secret" in config --- NEWS.md | 2 ++ config/environments/development.js.example | 1 + config/environments/production.js.example | 1 + config/environments/staging.js.example | 1 + config/environments/test.js.example | 1 + lib/cartodb/cache_validator.js | 15 ++++++++++----- lib/cartodb/cartodb_windshaft.js | 2 +- lib/cartodb/server_options.js | 1 + npm-shrinkwrap.json | 3 ++- package.json | 2 +- 10 files changed, 21 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index ea310d99..098755f8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ Enhancements: * Add script to flush caches (#140) * Add statsd support (#139) + * Add support for specifying a varnish password + * Avoid sending multiple varnish invalidation at once (#135) 1.7.1 -- 2014-02-11 ------------------- diff --git a/config/environments/development.js.example b/config/environments/development.js.example index 426ec246..791ebec7 100644 --- a/config/environments/development.js.example +++ b/config/environments/development.js.example @@ -99,6 +99,7 @@ var config = { ,varnish: { host: 'localhost', port: 6082, + secret: 'xxx', ttl: 86400 } // If useProfiler is true every response will be served with an diff --git a/config/environments/production.js.example b/config/environments/production.js.example index 2f08f889..5f8ad1ab 100644 --- a/config/environments/production.js.example +++ b/config/environments/production.js.example @@ -93,6 +93,7 @@ var config = { ,varnish: { host: 'localhost', port: 6082, + secret: 'xxx', ttl: 86400 } // If useProfiler is true every response will be served with an diff --git a/config/environments/staging.js.example b/config/environments/staging.js.example index 277bb77d..6dd5cc8a 100644 --- a/config/environments/staging.js.example +++ b/config/environments/staging.js.example @@ -93,6 +93,7 @@ var config = { ,varnish: { host: 'localhost', port: 6082, + secret: 'xxx', ttl: 86400 } // If useProfiler is true every response will be served with an diff --git a/config/environments/test.js.example b/config/environments/test.js.example index de8002d1..43dfa507 100644 --- a/config/environments/test.js.example +++ b/config/environments/test.js.example @@ -95,6 +95,7 @@ var config = { ,varnish: { host: '', port: null, + secret: 'xxx', ttl: 86400 } // If useProfiler is true every response will be served with an diff --git a/lib/cartodb/cache_validator.js b/lib/cartodb/cache_validator.js index 8449861e..22793603 100644 --- a/lib/cartodb/cache_validator.js +++ b/lib/cartodb/cache_validator.js @@ -2,16 +2,21 @@ var _ = require('underscore'), Varnish = require('node-varnish'), varnish_queue = null; -function init(host, port) { - varnish_queue = new Varnish.VarnishQueue(host, port); +function init(host, port, secret) { + varnish_queue = new Varnish.VarnishQueue(host, port, secret); + varnish_queue.on('error', function(e) { + console.log("[CACHE VALIDATOR ERROR] " + e); + }); } function invalidate_db(dbname, table) { + var cmd = 'purge obj.http.X-Cache-Channel ~ "^' + dbname + + ':(.*'+ table +'.*)|(table)$"'; try{ - varnish_queue.run_cmd('purge obj.http.X-Cache-Channel ~ "^' + dbname + ':(.*'+ table +'.*)|(table)$"'); - console.log('[SUCCESS FLUSHING CACHE]'); + varnish_queue.run_cmd(cmd, false); } catch (e) { - console.log("[ERROR FLUSHING CACHE] Is enable_cache set to true? Failed for: " + 'purge obj.http.X-Cache-Channel ~ "^' + dbname + ':(.*'+ table +'.*)|(table)$"'); + console.log("[CACHE VALIDATOR ERROR] could not queue command " + + cmd + " -- " + e); } } diff --git a/lib/cartodb/cartodb_windshaft.js b/lib/cartodb/cartodb_windshaft.js index 3791230a..b543e7eb 100644 --- a/lib/cartodb/cartodb_windshaft.js +++ b/lib/cartodb/cartodb_windshaft.js @@ -14,7 +14,7 @@ var CartodbWindshaft = function(serverOptions) { if(serverOptions.cache_enabled) { console.log("cache invalidation enabled, varnish on ", serverOptions.varnish_host, ' ', serverOptions.varnish_port); - Cache.init(serverOptions.varnish_host, serverOptions.varnish_port); + Cache.init(serverOptions.varnish_host, serverOptions.varnish_port, serverOptions.varnish_secret); serverOptions.afterStateChange = function(req, data, callback) { Cache.invalidate_db(req.params.dbname, req.params.table); callback(null, data); diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index a943da01..5751c04d 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -62,6 +62,7 @@ module.exports = function(){ enable_cors: global.environment.enable_cors, varnish_host: global.environment.varnish.host, varnish_port: global.environment.varnish.port, + varnish_secret: global.environment.varnish.secret, cache_enabled: global.environment.cache_enabled, log_format: global.environment.log_format, useProfiler: global.environment.useProfiler diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index aeae8be4..68b5f73d 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -3,7 +3,8 @@ "version": "1.7.1", "dependencies": { "node-varnish": { - "version": "0.1.1" + "version": "0.2.0", + "from": "http://github.com/Vizzuality/node-varnish/tarball/v0.2.0" }, "underscore": { "version": "1.3.3" diff --git a/package.json b/package.json index 361357fa..b7e34834 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "Sandro Santilli " ], "dependencies": { - "node-varnish": "0.1.1", + "node-varnish": "0.2.0", "underscore" : "~1.3.3", "windshaft" : "~0.17.2", "step": "0.0.x",