-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrlist.c
312 lines (242 loc) · 11.1 KB
/
rlist.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/time.h>
#include "redismodule.h"
static RedisModuleString* pop_intervals_key = NULL;
static RedisModuleString* push_intervals_key = NULL;
static RedisModuleString* last_pop_times_key = NULL;
static RedisModuleString* last_push_times_key = NULL;
int RLSetRL_RedisCommand(RedisModuleCtx* ctx, RedisModuleString** argv, int argc) {
RedisModule_AutoMemory(ctx);
if (argc < 3 || (argc % 2 != 1))
return RedisModule_WrongArity(ctx);
size_t len;
RedisModuleKey* key = NULL;
if (strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.SETPOPINTERVAL", 17) == 0) {
key = RedisModule_OpenKey(ctx, pop_intervals_key, REDISMODULE_READ | REDISMODULE_WRITE);
} else if (strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.SETPUSHINTERVAL", 18) == 0) {
key = RedisModule_OpenKey(ctx, push_intervals_key, REDISMODULE_READ | REDISMODULE_WRITE);
} else {
return RedisModule_ReplyWithError(ctx, "ERR: invalid command and/or args");
}
int type = RedisModule_KeyType(key);
if (type != REDISMODULE_KEYTYPE_HASH && type != REDISMODULE_KEYTYPE_EMPTY) {
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
}
// validate that intervals are valid
long long interval;
for (int i = 2; i < argc; i = i + 2) {
int ret = RedisModule_StringToLongLong(argv[i], &interval);
if (ret != REDISMODULE_OK)
return RedisModule_ReplyWithError(ctx, "ERR: one or more intervals is invalid");
if (interval < 0)
return RedisModule_ReplyWithError(ctx, "ERR: one or more intervals is invalid");
}
// HSET in a loop because of https://github.com/redis/redis/issues/7860
int count = 0;
for (int i = 1; i + 1 < argc; i = i + 2) {
int ret = RedisModule_HashSet(key, REDISMODULE_HASH_NONE, argv[i], argv[i + 1], NULL);
count += ret;
}
return RedisModule_ReplyWithLongLong(ctx, count);
}
int RL_LPopRPushCommand(RedisModuleCtx* ctx, RedisModuleString** argv, int argc) {
RedisModule_AutoMemory(ctx);
if (argc != 3)
return RedisModule_WrongArity(ctx);
RedisModuleKey* src_key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
RedisModuleKey* dst_key = RedisModule_OpenKey(ctx, argv[2], REDISMODULE_READ | REDISMODULE_WRITE);
int src_type = RedisModule_KeyType(src_key);
int dst_type = RedisModule_KeyType(dst_key);
if ((src_type != REDISMODULE_KEYTYPE_LIST && src_type != REDISMODULE_KEYTYPE_EMPTY) ||
(dst_type != REDISMODULE_KEYTYPE_LIST && dst_type != REDISMODULE_KEYTYPE_EMPTY)) {
RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
return REDISMODULE_ERR;
}
RedisModuleString* val = RedisModule_ListPop(src_key, REDISMODULE_LIST_HEAD);
if (val == NULL)
return RedisModule_ReplyWithNull(ctx);
RedisModule_ListPush(dst_key, REDISMODULE_LIST_TAIL, val);
return RedisModule_ReplyWithString(ctx, val);
}
static int dispatch_move(RedisModuleCtx* ctx, RedisModuleString** argv, int argc) {
size_t len;
RedisModuleCallReply* reply = NULL;
if (strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.LPOP", 7) == 0 && argc == 2) {
reply = RedisModule_Call(ctx, "LPOP", "s", argv[1]);
} else if (strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.RPOP", 7) == 0 && argc == 2) {
reply = RedisModule_Call(ctx, "RPOP", "s", argv[1]);
} else if (strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.LPUSH", 8) == 0 && argc == 3) {
reply = RedisModule_Call(ctx, "LPUSH", "ss", argv[1], argv[2]);
} else if (strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.RPUSH", 8) == 0 && argc == 3) {
reply = RedisModule_Call(ctx, "RPUSH", "ss", argv[1], argv[2]);
} else if (strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.RPOPLPUSH", 12) == 0 && argc == 3) {
reply = RedisModule_Call(ctx, "RPOPLPUSH", "ss", argv[1], argv[2]);
} else if (strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.LPOPRPUSH", 12) == 0 && argc == 3) {
return RL_LPopRPushCommand(ctx, argv, argc);
} else {
return RedisModule_ReplyWithError(ctx, "ERR: invalid command and/or args");
}
return RedisModule_ReplyWithCallReply(ctx, reply);
}
static int _get_long_long_from_hash(RedisModuleCtx* ctx, RedisModuleString* hash, RedisModuleString* field,
long long** result) {
if (result == NULL)
return REDISMODULE_ERR;
RedisModuleKey* key = RedisModule_OpenKey(ctx, hash, REDISMODULE_READ);
if (key == NULL) {
// hash does not exist
*result = NULL;
return REDISMODULE_OK;
}
RedisModuleString* val_str = NULL;
if (RedisModule_HashGet(key, REDISMODULE_HASH_NONE, field, &val_str, NULL) != REDISMODULE_OK) {
// RedisModule_HashGet returns REDISMODULE_OK on success and
// REDISMODULE_ERR if the key is not a hash.
return REDISMODULE_ERR;
}
if (val_str == NULL) {
// hash exists but field does not exist
*result = NULL;
return REDISMODULE_OK;
}
long long val;
if (RedisModule_StringToLongLong(val_str, &val) != REDISMODULE_OK)
return REDISMODULE_ERR;
*result = &val;
return REDISMODULE_OK;
}
static long long get_long_long_from_hash(RedisModuleCtx* ctx, RedisModuleString* hash, RedisModuleString* field) {
long long result = 0;
long long* val_ptr = NULL;
if (_get_long_long_from_hash(ctx, hash, field, &val_ptr) != REDISMODULE_OK)
return -1;
if (val_ptr != NULL)
result = *val_ptr;
return result;
}
int RL_PopOrPush_RedisCommand(RedisModuleCtx* ctx, RedisModuleString** argv, int argc) {
RedisModule_AutoMemory(ctx);
if (argc != 2 && argc != 3)
return RedisModule_WrongArity(ctx);
size_t len;
RedisModuleString* intervals_key = NULL;
RedisModuleString* ts_key = NULL;
if ((strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.LPOP", 7) == 0 ||
strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.RPOP", 7) == 0) &&
argc == 2) {
intervals_key = pop_intervals_key;
ts_key = last_pop_times_key;
} else if ((strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.LPUSH", 8) == 0 ||
strncasecmp(RedisModule_StringPtrLen(argv[0], &len), "RL.RPUSH", 8) == 0) &&
argc == 3) {
intervals_key = push_intervals_key;
ts_key = last_push_times_key;
} else {
return RedisModule_ReplyWithError(ctx, "ERR: invalid command and/or args");
}
long long interval = get_long_long_from_hash(ctx, intervals_key, argv[1]);
if (interval < 0)
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
if (interval == 0)
return dispatch_move(ctx, argv, argc);
long long last_timestamp = get_long_long_from_hash(ctx, ts_key, argv[1]);
if (last_timestamp < 0)
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
struct timeval tv;
gettimeofday(&tv, NULL);
long long ms_epoch = (long long)(tv.tv_sec) * 1000 + (long long)(tv.tv_usec) / 1000;
if (ms_epoch >= (last_timestamp + interval)) {
RedisModuleKey* key = RedisModule_OpenKey(ctx, ts_key, REDISMODULE_WRITE);
RedisModule_HashSet(key, REDISMODULE_HASH_NONE, argv[1],
RedisModule_CreateStringFromLongLong(ctx, ms_epoch), NULL);
return dispatch_move(ctx, argv, argc);
}
return RedisModule_ReplyWithNull(ctx);
}
int RL_PopAndPush_RedisCommand(RedisModuleCtx* ctx, RedisModuleString** argv, int argc) {
RedisModule_AutoMemory(ctx);
if (argc != 3)
return RedisModule_WrongArity(ctx);
long long pop_interval = get_long_long_from_hash(ctx, pop_intervals_key, argv[1]);
if (pop_interval < 0)
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
long long push_interval = get_long_long_from_hash(ctx, push_intervals_key, argv[2]);
if (push_interval < 0)
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
if (pop_interval == 0 && push_interval == 0)
return dispatch_move(ctx, argv, argc);
long long last_pop_timestamp = get_long_long_from_hash(ctx, last_pop_times_key, argv[1]);
if (last_pop_timestamp < 0)
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
long long last_push_timestamp = get_long_long_from_hash(ctx, last_push_times_key, argv[2]);
if (last_push_timestamp < 0)
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
struct timeval tv;
gettimeofday(&tv, NULL);
long long ms_epoch = (long long)(tv.tv_sec) * 1000 + (long long)(tv.tv_usec) / 1000;
if (ms_epoch >= (last_pop_timestamp + pop_interval) && ms_epoch >= (last_push_timestamp + push_interval)) {
RedisModuleKey* key_pop = RedisModule_OpenKey(ctx, last_pop_times_key, REDISMODULE_WRITE);
RedisModuleKey* key_push = RedisModule_OpenKey(ctx, last_push_times_key, REDISMODULE_WRITE);
RedisModuleString* ts = RedisModule_CreateStringFromLongLong(ctx, ms_epoch);
RedisModule_HashSet(key_pop, REDISMODULE_HASH_NONE, argv[1], ts, NULL);
RedisModule_HashSet(key_push, REDISMODULE_HASH_NONE, argv[2], ts, NULL);
return dispatch_move(ctx, argv, argc);
}
return RedisModule_ReplyWithNull(ctx);
}
int RedisModule_OnLoad(RedisModuleCtx* ctx, RedisModuleString** argv, int argc) {
REDISMODULE_NOT_USED(argv);
REDISMODULE_NOT_USED(argc);
if (RedisModule_Init(ctx, "RL", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "RL.SETPOPINTERVAL", RLSetRL_RedisCommand, "write fast deny-oom", 1, -1,
2) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "RL.SETPUSHINTERVAL", RLSetRL_RedisCommand, "write fast deny-oom", 1, -1,
2) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "RL.LPOP", RL_PopOrPush_RedisCommand, "write fast deny-oom", 1, 1, 1) ==
REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "RL.RPOP", RL_PopOrPush_RedisCommand, "write fast deny-oom", 1, 1, 1) ==
REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "RL.LPUSH", RL_PopOrPush_RedisCommand, "write fast deny-oom", 1, 1, 1) ==
REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "RL.RPUSH", RL_PopOrPush_RedisCommand, "write fast deny-oom", 1, 1, 1) ==
REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "RL.RPOPLPUSH", RL_PopAndPush_RedisCommand, "write fast deny-oom", 1, 2,
1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx, "RL.LPOPRPUSH", RL_PopAndPush_RedisCommand, "write fast deny-oom", 1, 2,
1) == REDISMODULE_ERR)
return REDISMODULE_ERR;
pop_intervals_key = RedisModule_CreateString(ctx, "rl::popintervals", 16);
if (pop_intervals_key == NULL)
return REDISMODULE_ERR;
push_intervals_key = RedisModule_CreateString(ctx, "rl::pushintervals", 17);
if (push_intervals_key == NULL)
return REDISMODULE_ERR;
last_pop_times_key = RedisModule_CreateString(ctx, "rl::lastpoptimes", 16);
if (last_pop_times_key == NULL)
return REDISMODULE_ERR;
last_push_times_key = RedisModule_CreateString(ctx, "rl::lastpushtimes", 17);
if (last_push_times_key == NULL)
return REDISMODULE_ERR;
return REDISMODULE_OK;
}
int RedisModule_OnUnload(RedisModuleCtx* ctx) {
if (pop_intervals_key != NULL)
RedisModule_FreeString(ctx, pop_intervals_key);
if (push_intervals_key != NULL)
RedisModule_FreeString(ctx, push_intervals_key);
if (last_pop_times_key != NULL)
RedisModule_FreeString(ctx, last_pop_times_key);
if (last_push_times_key != NULL)
RedisModule_FreeString(ctx, last_push_times_key);
return REDISMODULE_OK;
}