Varnish
varnish.js is a library to help with the management of varnish servers.
from: varnish-cache.org
Varnish is a web accelerator. You install it in front of your web application and it will speed it up significantly.
Note: This library only targets Varnish 3.x and above. For clarity on the feature please refer to the documentation
README Contents ## Features ## Installation
npm install varnish
You don't need varnish on the same machine or even at all to use this package. In order to run the integration tests you will need varnish running locally. Check out the documentation and installation.
## Usage var varnish = ;
Discover
varnish;
Create
var server = varnish; server id'test' name'mine' ; serverstart{ console;};
Manage
var admin = server; admin start ;
Analyse
var stats = server; stats; stats;
Configure
var vcl = varnish; vcl; admin;
varnish.get()
Find a varnishd instance based on id or instance name (-n). A function can be passed as a filter. The first server matches wins.
varnish;varnish; // -n referencedvarnish; // filter function
varnish.discover()
varnish;varnish;varnish
filters receive a process obj.
pid
: pidppid
: Parent piduser
: usercommand
: command runningargs
: string of args passed to command
varnish.create() ## Server()
A helper class for wrapping the generation of command line options to create a varnishd instance. For more clarity on the options to pass please refer to the varnishd documentation.
Configuration options cannot be changed once a server has been initialized and has a pid. Attempts to change will be ignored.
- pid: false
- path: '/usr/local/sbin/varnishd'
- running: false
- child: false
Server.listen(host)
Listen for client requests on the specified address and port. The address can be a host name (“localhost”), an IPv4 dotted-quad (“127.0.0.1”), or an IPv6 address enclosed in square brackets (“[::1]”). If address is not specified, varnishd will listen on all available IPv4 and IPv6 interfaces. If port is not specified, the default HTTP port as listed in /etc/services is used. Multiple listening addresses and ports can be specified.
server ; // 'varnishd -a :9000,host.com:8888'
Server.backend(host)
Use the specified host as backend server. If port is not specified, the default is 8080
server ; // 'varnishd -b backend.com'
Server.compile()
Print VCL code compiled to C language and exit. Specify the VCL file to compile with the -f option (Server.vcl
).
server compile ; // 'varnishd -C'
Server.debug()
Enables debugging mode: The parent process runs in the foreground with a CLI connection on stdin/stdout, and the child process must be started explicitly with a CLI command. Terminating the parent process will also terminate the child.
server ; // 'varnishd -d'
Server.foreground()
Run in the foreground.
server ; // 'varnishd -F'
Server.vcl(vclfile)
Use the specified VCL configuration file instead of the builtin default. See vcl for details on VCL syntax. When no configuration is supplied varnishd will not start the cache process.
server ; // 'varnishd -f /varnish/vcl/test.vcl'
Server.group(group)
Specifies the name of an unprivileged group to which the child process should switch before it starts accepting connections. This is a shortcut for specifying the group run-time parameter.
server ; // 'varnishd -g admin'
Server.hash()
Specifies the hash algorithm.
simple_list
: A simple doubly-linked list. Not recommended for production use.classic[,buckets]
: A standard hash table. This is the default. The hash key is the CRC32 of the object's URL modulo the size of the hash table. Each table entry points to a list of elements which share the same hash key. The buckets parameter specifies the number of entries in the hash table. The default is 16383.critbit
: A self-scaling tree structure. The default hash algorithm in 2.1. In comparison to a more traditional B tree the critbit tree is almost completely lockless.
server hash'classic,16383' ; // 'varnishd -h classic,16383'
Server.id()
Specify the identity of the varnish server. This can be accessed using server.identity from VCL
server id'integration' ; // 'varnishd -i integration'
Server.shmlog()
Specify size of shmlog file. Scaling suffixes like 'k', 'm' can be used up to (e)tabytes. Default is 80 Megabytes. Specifying less than 8 Megabytes is unwise.
server ; // 'varnishd -l 80M'
Server.name()
Specify a name for this instance. Amonst other things, this name is used to construct the name of the directory in which varnishd keeps temporary files and persistent state. If the specified name begins with a forward slash, it is interpreted as the absolute path to the directory which should be used for this purpose.
server name'ref' ; // 'varnishd -n ref'
Server.pidfile()
Write the process's PID to the specified file.
server ; // 'varnishd -P /var/run/varnish.pid'
Server.param()
Set the parameter specified by param to the specified value. See Run-Time Parameters for a list of parameters. This option can be used multiple times to specify multiple parameters.
server ; // 'varnishd -p ban_lurker_sleep=100 -p cli_buffer=64000'
Server.secret()
Path to a file containing a secret used for authorizing access to the management port.
server ; // 'varnishd -S /secret.file'
Server.storage()
Use the specified storage backend. See Storage Types for a list of supported storage types. This option can be used multiple times to specify multiple storage files. You can name the different backends. Varnish will then reference that backend with the given name in logs, statistics, etc.
server
Server.management()
Offer a management interface on the specified address and port. See Management Interface for a list of management commands.
server ; // 'varnishd -T localhost:5421'
Server.reverse()
Connect to this port and offer the command line interface. Think of it as a reverse shell. When running with -M and there is no backend defined the child process (the cache) will not start initially.
server ; // 'varnishd -M localhost:1245'
Server.ttl()
Specifies a hard minimum time to live for cached documents. This is a shortcut for specifying the default_ttl run-time parameter.
server ; // 'varnishd -t 500'
Server.user()
Specifies the name of an unprivileged user to which the child process should switch before it starts accepting connections. This is a shortcut for specifying the user run- time parameter.
If specifying both a user and a group, the user should be specified first.
server ; // 'varnishd -u nw'
Server.version()
Display the version number and exit.
server version ; // 'varnishd -V'
Server.workers()
Start at least min but no more than max worker threads with the specified idle timeout. This is a shortcut for specifying the thread_pool_min, thread_pool_max and thread_pool_timeout run-time parameters.
If only one number is specified, thread_pool_min and thread_pool_max are both set to this number, and thread_pool_timeout has no effect.
server ; // 'varnishd -w 2,500,300'
Server.start()
Server.kill()
Server.admin()
Server.stat()
Server.log() ## Admin(host, port, options)
Admin client for varnishadm
var varnish = admin = host port options;
Options:
name
{String} name of the instance to connect toauth
{String||Buffer} content of secret file for varnishfile
if auth not present will load file contents.auto connect
{Boolean}
Using the Server
class you can also create an Admin
instance.
var admin = server;
The nice thing about creating it from the server is all properties set that apply to the admin class will be passed through automatically if the server has already been initialized and is running.
Events
connect
: when a socket connection has been established with the management port.authenticated
: when the authentication challenge from management port has been responded to correctly.close
: when connection to the admin port has been closed.error
: any network related error.
Errors
Command Errors
All errors that occur talking directly to the admin port will be handled in a callback and will not emit an error
event.
error instanceof Error
in addition they have the a code
property.
- 100: 'Syntax Error'
- 101: 'Unknown Request'
- 102: 'Not Implemented'
- 104: 'Too Few Parameters'
- 105: 'Too Many Parameters'
- 106: 'Bad Parameter'
- 107: 'Authentication Required'
- 200: 'OK'
- 201: 'Truncated'
- 300: "Can't Perform Operation"
- 400: "Communication Error"
- 500: "Close"
not implemented by varnishadm
- 800: 'Response Parsing Error'
Admin.connect()
Use when autoconnect
flag == false
Admin.send(command, arg1, arg2, Function)
Send command to varnishadm
. This is talking to the socket directly.
admin; admin;admin;
resp in all cases will be a string of the complete response from the admin.
Command Wrappers
The following methods wrap the send command and parse the output of the response for you. As a result all callbacks passed to these method will receive three parameters.
error
{Error} object ornull
resp
parsed {Object} iferror == null
raw
the string response from the socket.
Admin.ping(callback)
Ping backend
admin
Admin.status(callback)
Check if child state == 'running'
adminstatus{ if!running // we need to start it}
Admin.start(callback)
Starts child process (sets state == 'running')
adminstart{ // started if no error};
Admin.stop(callback)
Stops child process (sets state == 'stopped')
admin;
Admin.list(callback)
Returns list of loaded vcl files
VCL {Object}
- name {String} name of vcl
- num {String} ???
- status {String} 'active|available|boot'
admin;
Admin.load(reference, path, callback)
Load a vcl from a file
Admin.use(reference, callback)
Make a vcl config active.
Admin.inline(reference, inline, callback)
Make a vcl config active.
Admin.discard(reference, callback)
Discard a loaded vcl
Admin.settings(name, callback)
Get details on varnish configuration parameters
Param:
- value: {String} current value
- unit: {String} the unit type varnish expects
- default: {String} default value
- description: {String} description
auto_restart: value: 'on' unit: 'bool' default: 'on [bool]' desc: 'Restart child process automatically if it dies.' cli_buffer: value: '8192' unit: 'bytes' default: '8192 [bytes]' desc: 'Size of buffer for CLI input. You may need to increase this if you have big VCL files and use the vcl.inline CLI command. NB: Must be specified with -p to have effect.' cli_timeout: value: '10' unit: 'seconds' default: '10 [seconds]' desc: 'Timeout for the childs replies to CLI requests from the master.'
Admin.set(parameter, new, callback)
Set a parameter
admin;
Admin.panic(callback)
Check for latest panic
Admin.clear(callback)
Clears the last panic
Admin.storage(callback)
Get list of current storage w/ type
admin
Admin.backend(callback)
List of all backend currently referenced in loaded VCLs
Backend:
- name {String}
- host {String}
- port {String}
- refs {String} number of vcls pointing at backend
- admin {String}
- status {String}
- passed {String}
- window {String}
app
Admin.ban(url, callback)
Ban urls/rules from cache
## VCL()A chain-able interface that allows vcl files to be written in javascript.
## StatStats client for varnishstat
var varnish = stat = options;
Options:
fields
{Array} array of varnish fields to collect stats onevery
sample rate insecond
units.name
instance of varnish to listen for
Events
data
: stats object- timestamp: {Date} object normalized to current timezone.
- fields: each field passed in options will be available. Each will have the following properties.
value
: the current value of the fieldpersec
: the normalized value over the last secdescription
: description about the field
exit
: when connection to stats has been closed.error
: any data received on stderr. This is not anError
object. It is aBuffer
.
Stat.list(callback) static
Stat
Stat.kill(callback) ## Top ## Log ## Resources ## Licence
(The MIT License)
Copyright (c) 2013 Nathan White nw@nwhite.net
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.