-
Notifications
You must be signed in to change notification settings - Fork 12
/
redis_nginx_adapter.c
277 lines (226 loc) · 8.13 KB
/
redis_nginx_adapter.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <ngx_core.h>
#include <ngx_http.h>
#include <signal.h>
#include "redis_nginx_adapter.h"
#define AUTH_COMMAND "AUTH %s"
#define SELECT_DATABASE_COMMAND "SELECT %d"
#define PING_DATABASE_COMMAND "PING"
int redis_nginx_event_attach(redisAsyncContext *ac);
void redis_nginx_cleanup(void *privdata);
void redis_nginx_ping_callback(redisAsyncContext *ac, void *rep, void *privdata);
void
redis_nginx_init(void)
{
signal(SIGPIPE, SIG_IGN);
}
void
redis_nginx_select_callback(redisAsyncContext *ac, void *rep, void *privdata)
{
redisAsyncContext **context = privdata;
redisReply *reply = rep;
if ((reply == NULL) || (reply->type == REDIS_REPLY_ERROR)) {
if (context != NULL) {
*context = NULL;
}
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: could not select redis database");
redisAsyncFree(ac);
}
}
void
redis_nginx_auth_callback(redisAsyncContext *ac, void *rep, void *privdata)
{
redisAsyncContext **context = privdata;
redisReply *reply = rep;
if ((reply == NULL) || (reply->type == REDIS_REPLY_ERROR)) {
if (context != NULL) {
*context = NULL;
}
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: could not authenticate to redis");
redisAsyncFree(ac);
}
}
redisAsyncContext *
redis_nginx_open_context_internal(const char *path, const char *host, int port, int database, const char* password, redisAsyncContext **context)
{
redisAsyncContext *ac = NULL;
if ((context == NULL) || (*context == NULL) || (*context)->err) {
if (path != NULL) {
ac = redisAsyncConnectUnix(path);
} else {
ac = redisAsyncConnect(host, port);
}
if (ac == NULL) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: could not allocate the redis context for %s:%d", host, port);
return NULL;
}
if (ac->err) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: could not create the redis context for %s:%d - %s", host, port, ac->errstr);
redisAsyncFree(ac);
return NULL;
}
redis_nginx_event_attach(ac);
if (context != NULL) {
*context = ac;
}
if (password != NULL) {
redisAsyncCommand(ac, redis_nginx_auth_callback, context, AUTH_COMMAND, password);
}
redisAsyncCommand(ac, redis_nginx_select_callback, context, SELECT_DATABASE_COMMAND, database);
} else {
ac = *context;
}
return ac;
}
redisAsyncContext *
redis_nginx_open_context(const char *host, int port, int database, const char* password, redisAsyncContext **context)
{
return redis_nginx_open_context_internal(NULL, host, port, database, password, context);
}
redisAsyncContext *
redis_nginx_open_context_unix(const char *path, int database, const char* password, redisAsyncContext **context)
{
return redis_nginx_open_context_internal(path, NULL, 0, database, password, context);
}
void
redis_nginx_force_close_context(redisAsyncContext **context)
{
if ((context != NULL) && (*context != NULL)) {
redisAsyncContext *ac = *context;
if (!ac->err) {
redisAsyncFree(ac);
}
*context = NULL;
}
}
void
redis_nginx_close_context(redisAsyncContext **context)
{
if ((context != NULL) && (*context != NULL)) {
redisAsyncContext *ac = *context;
if (!ac->err) {
redisAsyncCommand(ac, redis_nginx_ping_callback, NULL, PING_DATABASE_COMMAND);
}
*context = NULL;
}
}
void
redis_nginx_ping_callback(redisAsyncContext *ac, void *rep, void *privdata)
{
void *data = ac->data;
void (*callback) (void *) = privdata;
redisAsyncDisconnect(ac);
if (callback != NULL) {
callback(data);
}
}
void
redis_nginx_read_event(ngx_event_t *ev)
{
ngx_connection_t *connection = (ngx_connection_t *) ev->data;
redisAsyncHandleRead(connection->data);
}
void
redis_nginx_write_event(ngx_event_t *ev)
{
ngx_connection_t *connection = (ngx_connection_t *) ev->data;
redisAsyncHandleWrite(connection->data);
}
int redis_nginx_fd_is_valid(int fd) {
return (fd > 0) && ((fcntl(fd, F_GETFL) != -1) || (errno != EBADF));
}
void
redis_nginx_add_read(void *privdata)
{
ngx_connection_t *connection = (ngx_connection_t *) privdata;
if (!connection->read->active && redis_nginx_fd_is_valid(connection->fd)) {
connection->read->handler = redis_nginx_read_event;
connection->read->log = connection->log;
if (ngx_add_event(connection->read, NGX_READ_EVENT, NGX_CLEAR_EVENT) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: could not add read event to redis");
}
}
}
void
redis_nginx_del_read(void *privdata)
{
ngx_connection_t *connection = (ngx_connection_t *) privdata;
if (connection->read->active && redis_nginx_fd_is_valid(connection->fd)) {
if (ngx_del_event(connection->read, NGX_READ_EVENT, NGX_DISABLE_EVENT) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: could not delete read event to redis");
}
}
}
void
redis_nginx_add_write(void *privdata)
{
ngx_connection_t *connection = (ngx_connection_t *) privdata;
if (!connection->write->active && redis_nginx_fd_is_valid(connection->fd)) {
connection->write->handler = redis_nginx_write_event;
connection->write->log = connection->log;
if (ngx_add_event(connection->write, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: could not add write event to redis");
}
}
}
void
redis_nginx_del_write(void *privdata)
{
ngx_connection_t *connection = (ngx_connection_t *) privdata;
if (connection->write->active && redis_nginx_fd_is_valid(connection->fd)) {
if (ngx_del_event(connection->write, NGX_WRITE_EVENT, NGX_DISABLE_EVENT) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: could not delete write event to redis");
}
}
}
void
redis_nginx_cleanup(void *privdata)
{
if (privdata) {
ngx_connection_t *connection = (ngx_connection_t *) privdata;
redisAsyncContext *ac = (redisAsyncContext *) connection->data;
if (ac->err) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: connection to redis failed - %s", ac->errstr);
/**
* If the context had an error but the fd still valid is because another context got the same fd from OS.
* So we clean the reference to this fd on redisAsyncContext and on ngx_connection, avoiding close a socket in use.
*/
if (redis_nginx_fd_is_valid(ac->c.fd)) {
ac->c.fd = -1;
connection->fd = NGX_INVALID_FILE;
}
}
if ((connection->fd != NGX_INVALID_FILE)) {
redis_nginx_del_read(privdata);
redis_nginx_del_write(privdata);
ngx_close_connection(connection);
} else {
ngx_free_connection(connection);
}
ac->ev.data = NULL;
}
}
int
redis_nginx_event_attach(redisAsyncContext *ac)
{
ngx_connection_t *connection;
redisContext *c = &(ac->c);
/* Nothing should be attached when something is already attached */
if (ac->ev.data != NULL) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: context already attached");
return REDIS_ERR;
}
connection = ngx_get_connection(c->fd, ngx_cycle->log);
if (connection == NULL) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "redis_nginx_adapter: could not get a connection for fd #%d", c->fd);
return REDIS_ERR;
}
/* Register functions to start/stop listening for events */
ac->ev.addRead = redis_nginx_add_read;
ac->ev.delRead = redis_nginx_del_read;
ac->ev.addWrite = redis_nginx_add_write;
ac->ev.delWrite = redis_nginx_del_write;
ac->ev.cleanup = redis_nginx_cleanup;
ac->ev.data = connection;
connection->data = ac;
return REDIS_OK;
}