aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2013-07-22 21:56:25 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2013-07-22 21:56:25 +0300
commit88126134cd43e8da9ade49d18c757c6a41b777bc (patch)
treeaa4e755c7997c61fcf5ceb69204074e53826b219
parent7ca01d78447f87d7b61c26b0e3dba6b91296e471 (diff)
downloadvmod-tbf-88126134cd43e8da9ade49d18c757c6a41b777bc.tar.gz
vmod-tbf-88126134cd43e8da9ade49d18c757c6a41b777bc.tar.bz2
Add new functions.
* configure.ac: Check for sys/sysinfo.h, sysinfo and getloadavg * src/Makefile.am: Add new sources * src/getla.c: New file * src/time.c: New file * src/vmod_tbf.vcc (getla, systime) (strftime): New functions. * tests/Makefile.am: Add new testcase. * tests/time00.vtc: New testcase.
-rw-r--r--configure.ac4
-rw-r--r--src/Makefile.am2
-rw-r--r--src/getla.c65
-rw-r--r--src/time.c49
-rw-r--r--src/vmod_tbf.c2
-rw-r--r--src/vmod_tbf.vcc3
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/time00.vtc23
8 files changed, 149 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac
index 6c3d58b..661adc8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -42,6 +42,10 @@ PKG_PROG_PKG_CONFIG
# Checks for header files.
AC_HEADER_STDC
+AC_CHECK_HEADERS(sys/sysinfo.h)
+
+# Check for functions
+AC_CHECK_FUNCS(sysinfo getloadavg)
# Check for python
AC_CHECK_PROGS(PYTHON, [python], [
diff --git a/src/Makefile.am b/src/Makefile.am
index d037e98..1d7f1ed 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -26,6 +26,8 @@ libvmod_tbf_la_LDFLAGS=-module -export-dynamic -avoid-version
libvmod_tbf_la_LIBADD=
libvmod_tbf_la_SOURCES = \
+ getla.c\
+ time.c\
vmod_tbf.c\
vcc_if.c vcc_if.h
diff --git a/src/getla.c b/src/getla.c
new file mode 100644
index 0000000..75d229b
--- /dev/null
+++ b/src/getla.c
@@ -0,0 +1,65 @@
+/* This file is part of vmod-tbf
+ 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/>.
+*/
+#define _BSD_SOURCE
+#include <config.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <syslog.h>
+#if defined(HAVE_SYSINFO) && defined(HAVE_SYS_SYSINFO_H)
+# include <sys/sysinfo.h>
+#endif
+#include "vrt.h"
+#include "vcc_if.h"
+
+double
+vmod_getla(struct sess *sp, int what)
+{
+ switch (what) {
+ case 1:
+ what = 0;
+ break;
+ case 5:
+ what = 1;
+ break;
+ case 15:
+ what = 2;
+ break;
+ default:
+ what = 0;
+ }
+
+#if defined(HAVE_GETLOADAVG)
+ double loadavg[3];
+
+ if (getloadavg(loadavg, 3) != 3) {
+ syslog(LOG_DAEMON|LOG_CRIT, "tbf.getla cannot get values");
+ return 0.0;
+ }
+ return loadavg[what];
+#elif defined(HAVE_SYSINFO) && defined(HAVE_SYS_SYSINFO_H)
+ struct sysinfo info;
+
+ if (sysinfo(&info)) {
+ syslog(LOG_DAEMON|LOG_CRIT, "tbf.getla cannot get values");
+ return 0.0;
+ }
+ return info.loads[what] / 65536.0;
+#else
+ syslog(LOG_DAEMON|LOG_CRIT, "tbf.getla is not implemented");
+ return 0.0;
+#endif
+}
diff --git a/src/time.c b/src/time.c
new file mode 100644
index 0000000..01919de
--- /dev/null
+++ b/src/time.c
@@ -0,0 +1,49 @@
+/* This file is part of vmod-tbf
+ 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/>.
+*/
+#include <config.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include "vrt.h"
+#include "vcc_if.h"
+#include "bin/varnishd/cache.h"
+
+int
+vmod_systime(struct sess *sp)
+{
+ return time(NULL);
+}
+
+const char *
+vmod_strftime(struct sess *sp, const char *format, int timestamp)
+{
+ time_t ts = (time_t) timestamp;
+ size_t u, n;
+ char *p;
+
+ u = WS_Reserve(sp->wrk->ws, 0);
+ p = sp->wrk->ws->f;
+ n = strftime(p, u, format, gmtime(&ts));
+ if (n == 0) {
+ WS_Release(sp->wrk->ws, 0);
+ return NULL;
+ }
+
+ WS_Release(sp->wrk->ws, n + 1);
+
+ return p;
+}
diff --git a/src/vmod_tbf.c b/src/vmod_tbf.c
index 88c9890..dab0987 100644
--- a/src/vmod_tbf.c
+++ b/src/vmod_tbf.c
@@ -348,7 +348,7 @@ vmod_rate(struct sess *sp, const char *key, int cost, double t, int burst_size)
content.data = (void*) bkt;
content.size = sizeof(*bkt);
- rc = db->put (db, NULL, &keydat, &content, 0);
+ rc = db->put(db, NULL, &keydat, &content, 0);
if (rc) {
syslog(LOG_DAEMON|LOG_ERR, "error updating key %s: %s",
key, db_strerror(rc));
diff --git a/src/vmod_tbf.vcc b/src/vmod_tbf.vcc
index 78f14f2..babca68 100644
--- a/src/vmod_tbf.vcc
+++ b/src/vmod_tbf.vcc
@@ -4,3 +4,6 @@ Function VOID open(STRING, STRING)
Function VOID close()
Function VOID sync()
Function BOOL rate(STRING, INT, DURATION, INT)
+Function REAL getla(INT)
+Function INT systime()
+Function STRING strftime(STRING, INT)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4ae5ec2..5826201 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,6 +1,7 @@
VMOD_TESTS = \
test00.vtc\
- test01.vtc
+ test01.vtc\
+ time00.vtc
EXTRA_DIST=$(VMOD_TESTS)
DISTCLEANFILES=tbf.db
diff --git a/tests/time00.vtc b/tests/time00.vtc
new file mode 100644
index 0000000..2b7925b
--- /dev/null
+++ b/tests/time00.vtc
@@ -0,0 +1,23 @@
+varnishtest "Test strftime"
+
+server s1 {
+ rxreq
+ txresp
+} -start
+
+varnish v1 -vcl+backend {
+ import tbf from "${vmod_topbuild}/src/.libs/libvmod_tbf.so";
+ sub vcl_deliver {
+ set resp.http.result = tbf.strftime("%Y-%m-%d", 26697600);
+ }
+} -start
+
+client c1 {
+ txreq
+ rxresp
+ expect resp.http.result == 1970-11-06
+}
+
+client c1 -run
+
+

Return to:

Send suggestions and report system problems to the System administrator.