aboutsummaryrefslogtreecommitdiff
path: root/src/vmod-tbf.3
blob: 6ba3d3a847ae955325995d21f2d89a4353aeb60c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
.\" This file is part of Vmod-tbf -*- nroff -*-
.\" Copyright (C) 2013 Sergey Poznyakoff
.\"
.\" Vmod-tbf is free software; you can redistribute it and/or modify
.\" it under the terms of the GNU General Public License as published by
.\" the Free Software Foundation; either version 3, or (at your option)
.\" any later version.
.\"
.\" Vmod-tbf is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public License
.\" along with vmod-tbf.  If not, see <http://www.gnu.org/licenses/>.
.TH VMOD-TBF 1 "October 19, 2013" "VMOD-TBF" "User Reference"
.SH NAME
vmod-tbf \- token bucket filtering for Varnish
.SH SYNOPSIS
.B import tbf;

.BI "VOID tbf.open(STRING " dbdir ", STRING " params ");"

.B VOID tbf.close();

.BI "BOOL tbf.rate(STRING " key ", INT " COST ", DURATION " interval ", INT " burst_size ");"

.BI "BOOL tbf.check(STRING " key ", STRING " rate ");"

.BI "REAL tbf.getla(INT " sample ");"

.BI "INT tbf.systime();"

.BI "STRING tbf.strftime(STRING " format ", INT " timestamp ");"

.BI "VOID tbf.sleep(DURATION " time ");"

.SH DESCRIPTION
The
.B vmod-tbf
module implements \fBtoken bucket filtering\fR for
.BR "Varnish Cache" .
It also implements several unrelated functions.
.SS Rate control functions
.PP
In this implementation of the token bucket algorithm, each bucket is
associated with a \fBkey\fR (a string value uniquely identifying this
bucket).  The algorithm works as follows:
.TP
.B \(bu
A token is added to the bucket at a constant rate of 1 token
per \fIinterval\fR microseconds.
.TP
.B \(bu
A bucket can hold at most
\fIburst_size\fR tokens.  Any tokens arriving when the bucket is full
are discarded.
.TP
.B \(bu
When \fIcost\fR items of data arrive, \fIcost\fR tokens are removed
from the bucket and the data are accepted.
.TP
.B \(bu
If fewer than \fIcost\fR tokens are available, no tokens are removed from
the bucket and the data are not accepted.
.PP
This keeps the data traffic at a constant rate or \fIcost\fR items
per \fIinterval\fR microseconds with bursts of up to \fIburst_size\fR
items.  Such bursts occur when no data was being arrived for
\fIburst_size*interval\fR or more microseconds.
.PP
The function \fBtbf.rate\fR provides a low-level interface to this
algorithm.  Its arguments correspond exactly to the values used in
the above description.  The \fBkey\fR argument identifies the bucket.
The function returns \fBTRUE\fR if the data are accepted and
\fBFALSE\fR otherwise.  The sample usage is:
.PP
.EX
sub vcl_recv {
    if (!tbf.rate("ip:" + client.ip, 1, 0.1s, 20)) {
        error 429 "Request rate exceeded."
    }
}
.EE 
.PP
This example will keep the incoming requests at the rate of 10 requests
per second, allowing for bursts of up to 20 requests after each 2
second (or longer) period of inactivity.
.PP
The \fBtbf.check\fR function provides a higher-level interface.  Its
first argument identifies the bucket.  The \fIrate\fR argument is a
textual rate specification in the form:
.PP
.BI "" N req/ K "" U
.PP
where \fIN\fR and \fIK\fR are floating-point numbers, and \fIU\fR is
an optional unit specifier: \fBs\fR, \fBm\fR, \fBh\fR or \fBd\fR for
seconds, minutes, hours or days, correspondingly.  The parts of the
rate specification can be separated by any amount of whitespace.
.PP
For example, the following statement will limit the request rate to
ten and a half requests per second:
.PP
.EX
sub vcl_recv {
    if (!tbf.check(client.ip, "10.5 req/1s")) {
        error 429 "Request rate exceeded."
    }
}
.EE
.SS Storage
.PP
Buckets are kept in a Berkeley database file.  The \fBtbf.open\fR function
controls its location and permissions.  The \fBdbdir\fR argument
supplies the full pathname to the directory where the database is
located.  The \fBparams\fR argument is a semicolon separated list of
the following parameters:
.TP
.BI dbname= NAME
Sets the name of the database file.  Default is \fBtbf.bdb\fR.
.TP
.BR truncate " or " trunc
Truncate the database if it already exists.
.TP
.BI mode= OCT
Set the file mode.  \fIOCT\fR is an octal number.  The default file
mode is \fB640\fR.  Note that this parameter takes effect only when
creating the file.  If the database file already exists by the time
\fBtbf.open\fR is called, its mode will not be altered.
.TP
.BI sync= N
Synchronize the database with the disk after each \fBN\fR writes.
.TP
.BI debug= N
Set debugging level.
.PP
Normally, this function should be called only once, from the
\fBvcl_init\fR subroutine:
.PP
.EX
sub vcl_init {
    tbf.open("/var/run/varnish/tbf.db", "mode=600");
}
.EE
.PP
Note that the directory where the database file is located must be
writable for the user \fBVarnish\fR runs as.
.PP
Unless the \fBtbf.open\fR function was called, both \fBtbf.rate\fR and
\fBtbf.check\fR will attempt to use the database located in
\fIlocalstatedir\fB/vmod-tbf\fR, where \fIlocalstatedir\fR is the
directory for modifiable single-machine data, which is set when
configuring the package (e.g. \fB/var/run\fR or the like).
.PP
If the database directory does not exist, \fBtbf.open\fR will attempt
to create it, deducing its mode from the database file mode (see the
\fBmode=\fR parameter above) by setting executable 
bit in each triplet that has read or write bit set (e.g. \fB640\fR
will become \fB750\fR).
.PP
The \fBtbf.close\fR function flushes the data and closes the database.
It is normally called from the \fBvcl_fini\fR subroutine:
.PP
.EX
sub vcl_fini {
    tbf.close();
}
.EE
.SS Other functions
.PP
Several functions are provided that do not exactly belong to the
TBF algorithm, but which may come useful when implementing rate
control.
.PP
The \fBtbf.getla\fR function returns the system load average.  Its
argument identifies the interval for which to compute it: 1, 5 or
15 minutes.
.PP
The \fBtbf.systime\fR function returns the current time of day as the
number of seconds since the Epoch (1970-01-01 00:00:00 UTC).
.PP
The \fBtbf.strftime\fR function formats the \fBtimestamp\fR according
to the specification in \fBformat\fR.  See
.BR strftime (3),
for a description of available formats.  For example, the following
statements assigns the current year to the \f(CWX\-Year\fR header:
.PP
.EX
set req.http.X-Year = tbf.strftime("%Y", systime());
.EE
.PP
The \fBtbf.sleep\fR function suspends execution for a specified amount
of time.
.\"
.\" The MANCGI variable is set by man.cgi script on Ulysses.
.\" The download.inc file contains the default DOWNLOAD section
.\" for man-based doc pages.
.if "\V[MANCGI]"WEBDOC" \{\
.	ds package vmod-tbf
.	ds version 1.0
.	so download.inc
\}
.SH "SEE ALSO"
.BR vcl (7),
.BR varnishd (1).
.SH AUTHORS
Sergey Poznyakoff
.SH "BUG REPORTS"
Report bugs to <gray@gnu.org>.
.SH COPYRIGHT
Copyright \(co 2013 Sergey Poznyakoff
.br
.na
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
.br
.ad
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
.\" Local variables:
.\" eval: (add-hook 'write-file-hooks 'time-stamp)
.\" time-stamp-start: ".TH [A-Z_][A-Z0-9_.-]* [0-9] \""
.\" time-stamp-format: "%:B %:d, %:y"
.\" time-stamp-end: "\""
.\" time-stamp-line-limit: 20
.\" end:

Return to:

Send suggestions and report system problems to the System administrator.