Permalink
Browse files

Allow the Redis server and timeout to be set in VCL via redis.init_re…

…dis()

By default libvmod-redis attempts to connect to the Redis server
at 127.0.0.1, port 6379 with a connect timeout of 200 ms.

This patch allows the Redis hostname, port and timeout to be configured
via VCL using redis.init_redis(hostname, port, timeout_ms);

VCL example:
  import redis;
  sub vcl_init {
    redis.init_redis("localhost", 6379, 200);
  }

Signed-off-by: Noah Williamsson <noah.williamsson@gmail.com>
  • Loading branch information...
1 parent caa623e commit 3b3465771b884ca1f032494446f6f174f0c32e9e @noahwilliamsson noahwilliamsson committed May 31, 2012
Showing with 51 additions and 5 deletions.
  1. +11 −0 README.md
  2. +39 −5 src/vmod_redis.c
  3. +1 −0 src/vmod_redis.vcc
View
@@ -14,6 +14,17 @@ So far the module builds and runs on FreeBSD--on other platforms, you are on you
Functions and procedures
------------------------
+*redis.init_redis(host, port, timeout_ms)*
+
+Use the redis server at the given _host_ and _port_ with a timeout
+of _timeout__ms_ milliseconds.
+If _port_ is less than or equal to zero, the default port of 6379 is used.
+If _timeout__ms_ is less than or equal to zero, a default timeout of 200ms is used.
+
+This function is supposed to be called from the Varnish subroutine _vcl__init_.
+If the call is left out, the module will attempt to connect to the Redis server
+at 127.0.0.1:6379 with a connect timeout of 200ms.
+
*redis.send(command)*
Sends the given _command_ to redis; the response will be ignored.
View
@@ -49,6 +49,32 @@ make_key()
{
(void)pthread_key_create(&redis_key, NULL);
}
+
+static config_t *
+make_config(const char *host, int port, int timeout_ms)
+{
+ config_t *cfg;
+
+ LOG_T("make_config(%s,%d,%d)\n", host, port, timeout_ms);
+
+ cfg = malloc(sizeof(config_t));
+ if(cfg == NULL)
+ return NULL;
+
+ if(port <= 0)
+ port = 6379;
+
+ if(timeout_ms <= 0)
+ timeout_ms = REDIS_TIMEOUT_MS;
+
+ cfg->host = strdup(host);
+ cfg->port = port;
+
+ cfg->timeout.tv_sec = timeout_ms / 1000;
+ cfg->timeout.tv_usec = (timeout_ms % 1000) * 1000;
+
+ return cfg;
+}
int
init_function(struct vmod_priv *priv, const struct VCL_conf *conf)
@@ -60,17 +86,25 @@ init_function(struct vmod_priv *priv, const struct VCL_conf *conf)
(void)pthread_once(&redis_key_once, make_key);
if (priv->priv == NULL) {
- priv->priv = cfg = malloc(sizeof(config_t));
+ priv->priv = make_config("127.0.0.1", 6379, REDIS_TIMEOUT_MS);
priv->free = free;
- cfg->host = strdup("127.0.0.1");
- cfg->port = 6379;
- cfg->timeout.tv_sec = REDIS_TIMEOUT_MS / 1000;
- cfg->timeout.tv_usec = (REDIS_TIMEOUT_MS % 1000) * 1000;
}
return (0);
}
+void
+vmod_init_redis(struct sess *sp, struct vmod_priv *priv, const char *host, int port, int timeout_ms)
+{
+ config_t *old_cfg = priv->priv;
+
+ priv->priv = make_config(host, port, timeout_ms);
+ if(priv->priv && old_cfg) {
+ free(old_cfg->host);
+ free(old_cfg);
+ }
+}
+
static redisReply *
redis_common(struct sess *sp, struct vmod_priv *priv, const char *command)
{
View
@@ -1,4 +1,5 @@
Module redis
Init init_function
+Function VOID init_redis(PRIV_VCL, STRING, INT, INT)
Function VOID send(PRIV_VCL, STRING)
Function STRING call(PRIV_VCL, STRING)

0 comments on commit 3b34657

Please sign in to comment.