forked from zephirworks/libvmod-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmod_redis.c
198 lines (158 loc) · 3.92 KB
/
vmod_redis.c
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
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include "vrt.h"
#include "bin/varnishd/cache.h"
#include "vcc_if.h"
#include <pthread.h>
#include <hiredis/hiredis.h>
#define REDIS_TIMEOUT_MS 200 /* 200 milliseconds */
#define LOG_E(...) fprintf(stderr, __VA_ARGS__);
#ifdef DEBUG
# define LOG_T(...) fprintf(stderr, __VA_ARGS__);
#else
# define LOG_T(...) do {} while(0);
#endif
typedef struct redisConfig {
char *host;
int port;
struct timeval timeout;
} config_t;
static pthread_key_t redis_key;
static pthread_once_t redis_key_once = PTHREAD_ONCE_INIT;
static void __match_proto__()
vmod_log(struct sess *sp, const char *fmt, ...)
{
char buf[8192], *p;
va_list ap;
va_start(ap, fmt);
p = VRT_StringList(buf, sizeof buf, fmt, ap);
va_end(ap);
if (p != NULL)
WSP(sp, SLT_VCL_Log, "%s", buf);
}
static void
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)
{
config_t *cfg;
LOG_T("redis init called\n");
(void)pthread_once(&redis_key_once, make_key);
if (priv->priv == NULL) {
priv->priv = make_config("127.0.0.1", 6379, REDIS_TIMEOUT_MS);
priv->free = free;
}
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)
{
config_t *cfg = priv->priv;
redisContext *c;
redisReply *reply = NULL;
LOG_T("redis(%x): running %s %p\n", pthread_self(), command, priv->priv);
if ((c = pthread_getspecific(redis_key)) == NULL) {
c = redisConnectWithTimeout(cfg->host, cfg->port, cfg->timeout);
if (c->err) {
LOG_E("redis error (connect): %s\n", c->errstr);
}
(void)pthread_setspecific(redis_key, c);
}
reply = redisCommand(c, command);
if (reply == NULL && c->err == REDIS_ERR_EOF) {
c = redisConnectWithTimeout(cfg->host, cfg->port, cfg->timeout);
if (c->err) {
LOG_E("redis error (reconnect): %s\n", c->errstr);
redisFree(c);
} else {
redisFree(pthread_getspecific(redis_key));
(void)pthread_setspecific(redis_key, c);
reply = redisCommand(c, command);
}
}
if (reply == NULL) {
LOG_E("redis error (command): err=%d errstr=%s\n", c->err, c->errstr);
}
return reply;
}
void
vmod_send(struct sess *sp, struct vmod_priv *priv, const char *command)
{
redisReply *reply = redis_common(sp, priv, command);
if (reply != NULL) {
freeReplyObject(reply);
}
}
const char *
vmod_call(struct sess *sp, struct vmod_priv *priv, const char *command)
{
redisReply *reply = NULL;
const char *ret = NULL;
char *digits;
reply = redis_common(sp, priv, command);
if (reply == NULL) {
goto done;
}
switch (reply->type) {
case REDIS_REPLY_STATUS:
ret = strdup(reply->str);
break;
case REDIS_REPLY_ERROR:
ret = strdup(reply->str);
break;
case REDIS_REPLY_INTEGER:
digits = malloc(21); /* sizeof(long long) == 8; 20 digits + NUL */
if(digits)
sprintf(digits, "%lld", reply->integer);
ret = digits;
break;
case REDIS_REPLY_NIL:
ret = NULL;
break;
case REDIS_REPLY_STRING:
ret = strdup(reply->str);
break;
case REDIS_REPLY_ARRAY:
ret = strdup("array");
break;
default:
ret = strdup("unexpected");
}
done:
if (reply) {
freeReplyObject(reply);
}
return ret;
}