aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2018-01-30 23:00:05 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2018-01-30 23:06:11 +0200
commit4fef83230e851189fd3541f095c47a952968a411 (patch)
tree021413ec3602ca4d0e0a0ee0f588d0a2782155ae
parente946449ccf15a0f44f402b03812d31d5f8ef4f7b (diff)
downloadvmod-dbrw-4fef83230e851189fd3541f095c47a952968a411.tar.gz
vmod-dbrw-4fef83230e851189fd3541f095c47a952968a411.tar.bz2
Introduce connection idle timeout.
* NEWS: Updated. * configure.ac: Version 2.2.90 * doc/vmod-dbrw.3: Document connection idle timeout. * doc/vmod-dbrw.texi: Likewise. * src/dbrw.h (dbrw_backend) <sql_idle_timeout>: New method. (dbrw_config)<idle_timeout>: New member. (dbrw_connection)<timestamp>: New member. (sql_idle_timeout): New proto. * src/mysql.c (vmod_mysql_data) <mysql>: Change storage. All uses changed. (s_mysql_idle_timeout): New function. * src/sql.c (sql_idle_timeout): New function. (sql_query): Update connection timestamp. (sql_idle_timeout): New function. * src/vmod_dbrw.c (dbrw_connection_get): Force disconnect if connection remained idle for too long. Initialize cfg->idle_timeout. (vmod_config): New parameter 'timeout='
-rw-r--r--NEWS21
-rw-r--r--configure.ac4
-rw-r--r--doc/vmod-dbrw.314
-rw-r--r--doc/vmod-dbrw.texi17
-rw-r--r--src/dbrw.h8
-rw-r--r--src/mysql.c68
-rw-r--r--src/sql.c11
-rw-r--r--src/vmod_dbrw.c38
8 files changed, 130 insertions, 51 deletions
diff --git a/NEWS b/NEWS
index 6895824..913b0f9 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,2 @@
1vmod-dbrw -- history of user-visible changes. 2017-08-10 1vmod-dbrw -- history of user-visible changes. 2018-01-30
2Copyright (C) 2013-2017 Sergey Poznyakoff
3See the end of file for copying conditions. 2See the end of file for copying conditions.
@@ -6,2 +5,18 @@ Please send vmod-dbrw bug reports to <gray@gnu.org>
6 5
6Version 2.2.90 (Git)
7
8* SQL idle timeout
9
10For MySQL backend, the default connection idle timeout is set equal to
11the value of the MySQL variable 'wait_timeout'. For Postgres, default
12idle timeout is not yet implemented.
13
14Idle timeout can be configured using the timeout configuration option,
15e.g.:
16
17 dbrw.config("mysql", "database=dbrw;user=proxy;timeout=600",
18 {"select dest,pattern,value,flags from rewrite where
19 locate(url,'$url') = 1 order by weight asc;"});
20
21
7Version 2.2, 2017-08-10 22Version 2.2, 2017-08-10
@@ -33,3 +48,3 @@ Copyright information:
33 48
34Copyright (C) 2013-2017 Sergey Poznyakoff 49Copyright (C) 2013-2018 Sergey Poznyakoff
35 50
diff --git a/configure.ac b/configure.ac
index 81a5390..7a1272c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,3 +1,3 @@
1# This file is part of vmod-dbrw -*- autoconf -*- 1# This file is part of vmod-dbrw -*- autoconf -*-
2# Copyright (C) 2013-2017 Sergey Poznyakoff 2# Copyright (C) 2013-2018 Sergey Poznyakoff
3# 3#
@@ -16,3 +16,3 @@
16AC_PREREQ(2.69) 16AC_PREREQ(2.69)
17AC_INIT([vmod-dbrw], 2.2, [gray@gnu.org]) 17AC_INIT([vmod-dbrw], 2.2.90, [gray@gnu.org])
18AC_CONFIG_AUX_DIR([build-aux]) 18AC_CONFIG_AUX_DIR([build-aux])
diff --git a/doc/vmod-dbrw.3 b/doc/vmod-dbrw.3
index 139fbab..6f1dba8 100644
--- a/doc/vmod-dbrw.3
+++ b/doc/vmod-dbrw.3
@@ -15,3 +15,3 @@
15.\" along with vmod-dbrw. If not, see <http://www.gnu.org/licenses/>. 15.\" along with vmod-dbrw. If not, see <http://www.gnu.org/licenses/>.
16.TH VMOD-DBRW 1 "August 5, 2017" "VMOD-DBRW" "User Reference" 16.TH VMOD-DBRW 1 "January 30, 2018" "VMOD-DBRW" "User Reference"
17.SH NAME 17.SH NAME
@@ -101,2 +101,14 @@ is a full pathname to the certificate authority file.
101.TP 101.TP
102\fBtimeout\fR=\fIN\fR
103Sets idle timeout for a single connection (seconds). The connection
104will be closed and opened again if the module is to use it after
105\fIN\fR or more seconds since its last use. Set \fBtimeout=-1\fR to
106disable idle timeout (sessions will remain open until the SQL
107server closes them). Set \fBtimeout=0\fR to close the connection after
108each request (not recommended).
109
110The default depends on the selected SQL backend. For MySQL, it equals
111the value of the \fBwait_timeout\fR global variable. For PostgreSQL,
112it is \fB-1\fR.
113.TP
102\fBoptions\fR=\fISTRING\fR 114\fBoptions\fR=\fISTRING\fR
diff --git a/doc/vmod-dbrw.texi b/doc/vmod-dbrw.texi
index 82adda2..40fc84d 100644
--- a/doc/vmod-dbrw.texi
+++ b/doc/vmod-dbrw.texi
@@ -32,3 +32,3 @@ Boston, MA 02110-1301 USA
32 32
33Copyright @copyright{} 2013-2017 Sergey Poznyakoff 33Copyright @copyright{} 2013-2018 Sergey Poznyakoff
34 34
@@ -312,2 +312,17 @@ authority file.
312 312
313@kwindex timeout
314@cindex timeout, idle, SQL
315@cindex idle timeout, SQL
316@item timeout=@var{n}
317Sets idle timeout for a single connection. The connection will be
318closed and opened again if the module is to use it after @var{n} or
319more seconds since its last use. Set @samp{timeout=-1} to disable idle
320timeout (session will remain open until the SQL server closes it). Set
321@samp{timeout=0} to close the connection after each request (not
322recommended).
323
324The default depends on the selected SQL backend. For MySQL, it equals
325the value of the @samp{wait_timeout} global variable. For PostgreSQL,
326it is @samp{-1}.
327
313@kindex options 328@kindex options
diff --git a/src/dbrw.h b/src/dbrw.h
index 438d963..7a0ee52 100644
--- a/src/dbrw.h
+++ b/src/dbrw.h
@@ -1,3 +1,3 @@
1/* This file is part of vmod-dbrw 1/* This file is part of vmod-dbrw
2 Copyright (C) 2013-2017 Sergey Poznyakoff 2 Copyright (C) 2013-2018 Sergey Poznyakoff
3 3
@@ -45,2 +45,3 @@ struct dbrw_backend {
45 unsigned); 45 unsigned);
46 int (*sql_idle_timeout)(struct dbrw_connection *);
46}; 47};
@@ -68,2 +69,3 @@ struct dbrw_config {
68 char status[HTTP_STATUS_LEN+1]; 69 char status[HTTP_STATUS_LEN+1];
70 int idle_timeout;
69 VTAILQ_ENTRY(dbrw_config) list; 71 VTAILQ_ENTRY(dbrw_config) list;
@@ -79,3 +81,4 @@ struct dbrw_connection {
79 size_t matchsize; /* Total number of entries in match map */ 81 size_t matchsize; /* Total number of entries in match map */
80 void *data; /* Backend-specific data */ 82 time_t timestamp; /* Last used at */
83 void *data; /* Backend-specific data */
81 VTAILQ_ENTRY(dbrw_connection) list; 84 VTAILQ_ENTRY(dbrw_connection) list;
@@ -107,2 +110,3 @@ void sql_destroy(struct dbrw_connection *pd);
107const char *sql_get_column(struct dbrw_connection *pd, unsigned row, unsigned col); 110const char *sql_get_column(struct dbrw_connection *pd, unsigned row, unsigned col);
111int sql_idle_timeout(struct dbrw_connection *conn);
108 112
diff --git a/src/mysql.c b/src/mysql.c
index d7406ca..d97200a 100644
--- a/src/mysql.c
+++ b/src/mysql.c
@@ -1,3 +1,3 @@
1/* This file is part of vmod-dbrw 1/* This file is part of vmod-dbrw
2 Copyright (C) 2013-2014 Sergey Poznyakoff 2 Copyright (C) 2013-2018 Sergey Poznyakoff
3 3
@@ -23,3 +23,3 @@ struct vmod_mysql_data
23{ 23{
24 MYSQL *mysql; 24 MYSQL mysql;
25 MYSQL_RES *result; 25 MYSQL_RES *result;
@@ -35,3 +35,3 @@ check_errno(struct dbrw_connection *conn)
35 35
36 switch (mysql_errno(mp->mysql)) { 36 switch (mysql_errno(&mp->mysql)) {
37 case CR_SERVER_GONE_ERROR: 37 case CR_SERVER_GONE_ERROR:
@@ -40,3 +40,3 @@ check_errno(struct dbrw_connection *conn)
40 case ER_ABORTING_CONNECTION: 40 case ER_ABORTING_CONNECTION:
41 dbrw_error("query failed: %s", mysql_error(mp->mysql)); 41 dbrw_error("query failed: %s", mysql_error(&mp->mysql));
42 sql_disconnect(conn); 42 sql_disconnect(conn);
@@ -60,3 +60,3 @@ check_errno(struct dbrw_connection *conn)
60 case ER_NOT_ALLOWED_COMMAND: 60 case ER_NOT_ALLOWED_COMMAND:
61 dbrw_error("query failed: %s", mysql_error(mp->mysql)); 61 dbrw_error("query failed: %s", mysql_error(&mp->mysql));
62 dbrw_error("disabling MySQL connection"); 62 dbrw_error("disabling MySQL connection");
@@ -66,3 +66,3 @@ check_errno(struct dbrw_connection *conn)
66 default: 66 default:
67 dbrw_error("query failed: %s", mysql_error(mp->mysql)); 67 dbrw_error("query failed: %s", mysql_error(&mp->mysql));
68 } 68 }
@@ -90,3 +90,2 @@ s_mysql_destroy(struct dbrw_connection *conn)
90 free(mp->buffer); 90 free(mp->buffer);
91 free(mp->mysql);
92 free(mp); 91 free(mp);
@@ -104,10 +103,3 @@ s_mysql_connect(struct dbrw_connection *conn)
104 103
105 mp->mysql = malloc(sizeof(MYSQL)); 104 mysql_init(&mp->mysql);
106 if (!mp->mysql) {
107 dbrw_error("not enough memory");
108 conn->state = state_disabled;
109 return -1;
110 }
111
112 mysql_init(mp->mysql);
113 105
@@ -125,3 +117,3 @@ s_mysql_connect(struct dbrw_connection *conn)
125 if (s) 117 if (s)
126 mysql_options(mp->mysql, MYSQL_READ_DEFAULT_FILE, s); 118 mysql_options(&mp->mysql, MYSQL_READ_DEFAULT_FILE, s);
127 119
@@ -129,3 +121,3 @@ s_mysql_connect(struct dbrw_connection *conn)
129 if (s) 121 if (s)
130 mysql_options(mp->mysql, MYSQL_READ_DEFAULT_GROUP, s); 122 mysql_options(&mp->mysql, MYSQL_READ_DEFAULT_GROUP, s);
131 123
@@ -133,5 +125,5 @@ s_mysql_connect(struct dbrw_connection *conn)
133 if (s) 125 if (s)
134 mysql_ssl_set(mp->mysql, NULL, NULL, s, NULL, NULL); 126 mysql_ssl_set(&mp->mysql, NULL, NULL, s, NULL, NULL);
135 debug(conn->conf, 1, ("connecting to database")); 127 debug(conn->conf, 1, ("connecting to database"));
136 if (!mysql_real_connect(mp->mysql, 128 if (!mysql_real_connect(&mp->mysql,
137 host, 129 host,
@@ -143,3 +135,3 @@ s_mysql_connect(struct dbrw_connection *conn)
143 CLIENT_MULTI_RESULTS)) { 135 CLIENT_MULTI_RESULTS)) {
144 dbrw_error("cannot connect: %s", mysql_error(mp->mysql)); 136 dbrw_error("cannot connect: %s", mysql_error(&mp->mysql));
145 return -1; 137 return -1;
@@ -155,3 +147,3 @@ s_mysql_disconnect(struct dbrw_connection *conn)
155 struct vmod_mysql_data *mp = conn->data; 147 struct vmod_mysql_data *mp = conn->data;
156 mysql_close(mp->mysql); 148 mysql_close(&mp->mysql);
157 return 0; 149 return 0;
@@ -168,4 +160,3 @@ s_mysql_query(struct dbrw_connection *conn, const char *query)
168 for (i = 0; i < 10; i++) { 160 for (i = 0; i < 10; i++) {
169 mysql = mp->mysql; 161 rc = mysql_query(&mp->mysql, query);
170 rc = mysql_query(mysql, query);
171 if (rc) { 162 if (rc) {
@@ -179,3 +170,3 @@ s_mysql_query(struct dbrw_connection *conn, const char *query)
179 } 170 }
180 mp->result = mysql_store_result(mp->mysql); 171 mp->result = mysql_store_result(&mp->mysql);
181 if (mp->result) { 172 if (mp->result) {
@@ -183,5 +174,5 @@ s_mysql_query(struct dbrw_connection *conn, const char *query)
183 rc = 0; 174 rc = 0;
184 } else if (mysql_field_count(mp->mysql)) { 175 } else if (mysql_field_count(&mp->mysql)) {
185 dbrw_error("cannot store result: %s", 176 dbrw_error("cannot store result: %s",
186 mysql_error(mp->mysql)); 177 mysql_error(&mp->mysql));
187 conn->state = state_error; 178 conn->state = state_error;
@@ -269,3 +260,3 @@ s_mysql_escape (struct dbrw_connection *conn, const char *arg)
269 260
270 mysql_real_escape_string(mp->mysql, mp->buffer, arg, len); 261 mysql_real_escape_string(&mp->mysql, mp->buffer, arg, len);
271 262
@@ -277,2 +268,22 @@ s_mysql_escape (struct dbrw_connection *conn, const char *arg)
277 268