-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiredisproxy.c
200 lines (164 loc) · 4.61 KB
/
hiredisproxy.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
#include "../redismodule.h"
#include "../rmutil/util.h"
#include "../rmutil/strings.h"
#include "../rmutil/test_util.h"
#include "../hiredis/hiredis.h"
redisContext *c;
/*
Output must be freed
*/
char *GetPassword(RedisModuleCtx *ctx)
{
size_t len;
char *result = NULL;
RedisModuleString *param1 = RedisModule_CreateString(ctx, "GET", 3);
RedisModuleString *param2 = RedisModule_CreateString(ctx, "REQUIREPASS", 11);
RedisModuleCallReply *reply = RedisModule_Call(ctx, "CONFIG", "ss", param1, param2);
if(reply != NULL) {
RedisModuleCallReply *subreply;
subreply = RedisModule_CallReplyArrayElement(reply,1);
const char *ptr = RedisModule_CallReplyStringPtr(subreply,&len);
if(ptr !=NULL) {
result = strndup(ptr, len);
}
}
return result;
}
/*
Output must be freed
*/
char *GetSubreplyString(RedisModuleCallReply *roleReply, int idx)
{
size_t len;
RedisModuleCallReply *subreply;
subreply = RedisModule_CallReplyArrayElement(roleReply,idx);
const char *ptr = RedisModule_CallReplyStringPtr(subreply,&len);
if(ptr !=NULL) {
return strndup(ptr, len);
}
return NULL;
}
int GetSubreplyInt(RedisModuleCallReply *roleReply, int idx)
{
RedisModuleCallReply *subreply;
subreply = RedisModule_CallReplyArrayElement(roleReply,idx);
return (int)RedisModule_CallReplyInteger(subreply);
}
int HiredisConnect(RedisModuleCtx *ctx)
{
// Hi-redis init
struct timeval timeout = { 0, 500000 }; // 0.5 seconds
// Calling ROLE and parsing reply
RedisModuleCallReply *reply = RedisModule_Call(ctx, "ROLE", "");
RMUTIL_ASSERT_NOERROR(reply);
unsigned int reply_len = (unsigned int)RedisModule_CallReplyLength(reply);
if(reply_len != 5) {
return 0; // Not a normal response size for ROLE when slave, see http://redis.io/commands/role
}
char *hostname = GetSubreplyString(reply, 1);
int port = GetSubreplyInt(reply, 2);
// Calling CONFIG GET and grabbing reply as string
char *password = GetPassword(ctx);
// printf("host = [%s:%d], password = [%s]\n", hostname, port, password);
c = redisConnectWithTimeout(hostname, port, timeout);
free(hostname);
free(password);
if (c == NULL || c->err) {
if (c) {
redisFree(c);
printf("Connection error: %s\n", c->errstr);
} else {
printf("Connection error: can't allocate redis context\n");
}
return 0;
}
redisSetTimeout(c, timeout);
// TODO: Unclear if this is needed
// redisEnableKeepAlive(c);
return 1;
}
/* HIREDIS.PROXY
*/
int HiredisProxy(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
// Hi-redis init
redisReply *reply;
// init auto memory for created strings
RedisModule_AutoMemory(ctx);
if(c == NULL && HiredisConnect(ctx) == 0) {
RedisModule_ReplyWithError(ctx, "Unable to connect to master");
return REDISMODULE_ERR;
}
// First attempt on existing connection, second attempt retrying to connect
int attempts = 2;
while(1) {
switch(argc) {
case 2:
reply = redisCommand(c, RedisModule_StringPtrLen(argv[1], NULL));
break;
case 3:
reply = redisCommand(c, "%s %s",
RedisModule_StringPtrLen(argv[1], NULL),
RedisModule_StringPtrLen(argv[2], NULL)
);
break;
case 4:
reply = redisCommand(c, "%s %s %s",
RedisModule_StringPtrLen(argv[1], NULL),
RedisModule_StringPtrLen(argv[2], NULL),
RedisModule_StringPtrLen(argv[3], NULL)
);
break;
case 5:
reply = redisCommand(c, "%s %s %s %s",
RedisModule_StringPtrLen(argv[1], NULL),
RedisModule_StringPtrLen(argv[2], NULL),
RedisModule_StringPtrLen(argv[3], NULL),
RedisModule_StringPtrLen(argv[3], NULL)
);
break;
default:
return RedisModule_WrongArity(ctx);
}
// success or no more attempts?
attempts--;
if(reply != NULL || !attempts) {
break;
}
// reconnect?
if(reply == NULL) {
if(HiredisConnect(ctx) == 0) {
printf("Failed to reconnect.\n");
}
else {
printf("Reconnected.\n");
}
}
}
if(reply == NULL) {
RedisModule_ReplyWithError(ctx, "Connection with server was lost");
return REDISMODULE_ERR;
}
else {
if(reply->str != NULL) {
RedisModule_ReplyWithSimpleString(ctx, reply->str);
}
else {
RedisModule_ReplyWithNull(ctx);
}
freeReplyObject(reply);
return REDISMODULE_OK;
}
}
int RedisModule_OnLoad(RedisModuleCtx *ctx) {
// Register the module itself
if (RedisModule_Init(ctx, "hiredis", 1, REDISMODULE_APIVER_1) ==
REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
// register hiredis.proxy - the default registration syntax
if (RedisModule_CreateCommand(ctx, "hiredis.proxy", HiredisProxy, "readonly",
1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
return REDISMODULE_OK;
}