Class: Varnish::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/varnish/client.rb

Constant Summary collapse

DEFAULT_PORT =

Default management port of varnishd

6082
DEFAULT_HOST =

We assume varnishd on localhost

'localhost'
DEFAULT_OPTS =
{
  :keep_alive => false,
  :timeout    => 1
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Examples:

Varnish::Client.new "127.0.0.1"
Varnish::Client.new "mydomain.com:6082"
Varnish::Client.new :timeout => 5
Varnish::Client.new "10.0.0.3:6060", :timeout => nil, :keep_alive => true

Configuration options

timeout

if specified (seconds), calls to varnish will be wrapped in a timeout, default is 1 second. Disable with :timeout => nil

keep_alive

if true, the connection is kept alive by sending ping commands to varnishd every few seconds



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/varnish/client.rb', line 43

def initialize(*args)
  opts = {}

  case args.length
  when 0
    self.server = DEFAULT_HOST
  when 1
    arg = args[0]
    case arg
    when String
      self.server = arg
    when Hash
      self.server = DEFAULT_HOST
      opts = arg
    end
  when 2
    self.server = args[0]
    opts = args[1]
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 2)"
  end

  opts = DEFAULT_OPTS.merge(opts)
  @timeout     = opts[:timeout]
  @keep_alive  = opts[:keep_alive]

  @mutex = Mutex.new
end

Instance Attribute Details

#hostObject

hostname or IP-address of varnishd. Default is “localhost”



24
25
26
# File 'lib/varnish/client.rb', line 24

def host
  @host
end

#keep_aliveObject

set to true, to keep the connection alive. Default is false



21
22
23
# File 'lib/varnish/client.rb', line 21

def keep_alive
  @keep_alive
end

#portObject

port number of varnishd. Default is 6082



27
28
29
# File 'lib/varnish/client.rb', line 27

def port
  @port
end

#timeoutObject

timeout in seconds when connecting to varnishd. Default is 1



18
19
20
# File 'lib/varnish/client.rb', line 18

def timeout
  @timeout
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/varnish/client.rb', line 220

def connected?
  bool @conn && !@conn.closed?
end

#disconnectObject

close the connection to varnishd. Note that the connection will automatically be re-established when another command is issued.



212
213
214
215
216
217
218
# File 'lib/varnish/client.rb', line 212

def disconnect
  if connected?
    @conn.write "quit\n"
    @conn.gets
    @conn.close unless @conn.closed?
  end
end

#param(op, *args) ⇒ Object

Set and show parameters

.param :show, [-l], [<param>]
.param :set, <param>, <value>


183
184
185
# File 'lib/varnish/client.rb', line 183

def param(op, *args)
  cmd("param.#{op}", *args)
end

#ping(timestamp = nil) ⇒ Object

Ping the server to keep the connection alive



160
161
162
# File 'lib/varnish/client.rb', line 160

def ping(timestamp = nil)
  cmd("ping", timestamp)
end

#purge(*args) ⇒ Object

Purge objects from the cache or show the purge queue.

Takes one or two arguments:

1.

.purge :url, <regexp>
.purge :hash, <regexp>

<regexp> is a string containing a varnish compatible regexp

2.

.purge <costum-purge-conditions>
.purge :list

Returns true for purging, returns an array containing the purge queue for :list

Ex.:

v = Varnish::Client.new
v.purge :url, '.*'

v.purge "req.http.host ~ www.foo.com && req.http.url ~ images"

v.purge :list
#=> [[1, "req.url ~ .*"]]


144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/varnish/client.rb', line 144

def purge(*args)
  c = 'purge'
  c << ".#{args.shift}" if [:url, :hash, :list].include?(args.first)
  response = cmd(c, *args)
  case c
  when 'purge.list'
    response.split("\n").map do |line|
      a = line.split("\t")
      [a[0].to_i, a[1]]
    end
  else
    bool response
  end
end

#running?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/varnish/client.rb', line 201

def running?
  bool status =~ /running/
end

#serverObject

Returns the varnishd management host and port as “hostname:port”



81
82
83
# File 'lib/varnish/client.rb', line 81

def server
  "#{@host}:#{@port}"
end

#server=(server) ⇒ Object

Set the varnishd management host and port. Expects a string as “hostname” or “hostname:port”



74
75
76
77
78
# File 'lib/varnish/client.rb', line 74

def server=(server)
  @host, @port = server.split(':')
  @port = (@port || DEFAULT_PORT).to_i
  server
end

#startObject



193
194
195
# File 'lib/varnish/client.rb', line 193

def start
  bool cmd("start")
end

#statsObject

Returns a hash of status information

Ex.:

v = Varnish::Client.new
v.stats
=> {"Total header bytes"=>0, "Cache misses"=>0 ...}


170
171
172
173
174
175
176
177
# File 'lib/varnish/client.rb', line 170

def stats
  result = cmd("stats")
  Hash[*result.split("\n").map { |line|
      stat = line.strip!.split(/\s+/, 2)
      [stat[1], stat[0].to_i]
    }.flatten
  ]
end

#statusObject

Returns the status string from varnish. See also #running? and #stopped?



189
190
191
# File 'lib/varnish/client.rb', line 189

def status
  cmd("status")
end

#stopObject



197
198
199
# File 'lib/varnish/client.rb', line 197

def stop
  bool cmd("stop")
end

#stopped?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/varnish/client.rb', line 205

def stopped?
  bool status =~ /stopped/
end

#vcl(op, *params) ⇒ Object

Manipulate the VCL configuration

.vcl :load, <configname>, <filename>
.vcl :inline, <configname>, <quoted_VCLstring>
.vcl :use, <configname>
.vcl :discard, <configname>
.vcl :list
.vcl :show, <configname>

Returns an array of VCL configurations for :list, and the servers response as string otherwise

Ex.:

v = Varnish::Client.new
v.vcl :list
#=> [["active", 0, "boot"]]

v.vcl :load, "newconf", "/etc/varnish/myconf.vcl"


105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/varnish/client.rb', line 105

def vcl(op, *params)
  response = cmd("vcl.#{op}", *params)
  case op
  when :list
    response.split("\n").map do |line|
      a = line.split(/\s+/, 3)
      [a[0], a[1].to_i, a[2]]
    end
  else
    response
  end
end