subroutine & action - varnish
subroutine, code section, called in the flow, manipulate http header and other things,
action, indicate the next step, used inside subroutine, in format "return (action);" ,
see: https://www.varnish-cache.org/docs/3.0/reference/vcl.html#subroutines
------
format of subroutine
sub xxx {
...
return (action);
}
------
call a subrouine
format:
call xxx;
what we do:
we just re-define subroutine logic, it will be used in the flow automatically,
------
terminate of subroutine
use:
return(action);
'action' is the result which indicate the next step,
------
multi subroutine
if more than one subroutine with the same name is defined, they will be concated into a single subroutine in the order defined,
------
action
actions:
* error code [reason]
return error with specified reason, and abandon the request,
* pass
switch to pass mode,
if current subroutine is not vcl_pass, control will be pass to vcl_pass,
* pipe
switch to pipe mode,
if current subroutine is not vcl_pipe, control will be pass to vcl_pipe,
* lookup
lookup the requested object in the cache,
if found, control will be pass to vcl_hit, otheriwse to vcl_miss,
* deliver
deliver data to client,
if current subroutine is not vcl_deliver, control will be pass to vcl_deliver,
* restart
restart the request, and increase the restart counter, if the counter > max_restarts, an error will be send,
* fetch
retrieve the requested object from the backend,
if current subroutine is not vcl_fetch, control will be pass to vcl_fetch,
* ok
means ok, used in vcl_init & vcl_fini,
* hit_for_pass
used in vcl_fetch,
create a hit_for_pass object, set the object's ttl to the current value of beresp.tll,
current request will be handled by vcl_deliver,
but subsequence request will go directly to vcl_pass according to hit_for_pass object, means use vcl_pass and cache will not be used until the hit_for_pass object's ttl expired,
*
return the same action:
if a subroutine return a action the same as it self, that means the request is already proceed and finished in this way,
e.g.
in vcl_pipe(), return (pipe); , means request is proceed by the way pipe,
------
subroutines
* vcl_init
* vcl_recv
* vcl_pipe
* vcl_pass
* vcl_hash
* vcl_hit
* vcl_miss
* vcl_fetch
* vcl_deliver
* vcl_error
* vcl_fini
------
vcl_init
for init, called before any request come,
possible action:
* ok
vcl will go on
*
------
vcl_recv
called when request is received and parsed,
decide:
* whether to serve this request
* how to do it
* which backend to use
possible action:
* error code
* pass
* pipe
* lookup
*
------
vcl_pipe
request is passed to backend, varnish act just like a pipe between client & backend for furture data until the connection is closed,
possible action:
* error code
* pipe
indicate proceed & finished in pipe mode,
*
------
vcl_pass
similar as vcl_pipe, but the response from backend don't enter cache, means the response will not be cached,
the subsequence request of the same connection is proceed normally,
possible action:
* error code
* pass
indicate proceed & finished in pipe mode,
* restart
*
------
vcl_hash
add data to hash,
call hash_data() to add a data,
possible action:
* hash
Proceed,
*
------
vcl_hit
called after a cache is found in lookup,
possible action:
* deliver
* error code
* pass
* restart
*
------
vcl_miss
called after a cache not found in lookup,
decide:
* whether to attempt to retrieve the document from backend,
* which backend to use
possible action:
* error code
* pass
* fetch
------
vcl_fetch
called after a document is fetched from backend,
possible action:
* deliver
add object to cache, then deliver it,
* error code
* hit_for_pass
* restart
*
------
vcl_deliver
called before a object is deliver to the client,
possible action:
* deliver
deliver object to client,
* error code
* restart
*
------
vcl_error
called when an error occur,
possible action:
* deliver
deliver the error to client,
* restart
*
------
vcl_fini
clean up VCL, called after all request have exited VCL,
possible action:
* ok
vcl will be discarded,
*
------