Redis.xs

filter: Statementstmt 67.3% 1131 / 1679 Branchbran 44.4% 708 / 1592 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 56.2% 1839 / 3271 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/hiredis/dict.c ↑ Summary deps/hiredis/sds.h »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5  
6 #include "EVAPI.h"
7  
8 #include <sys/un.h>
9 #include <fcntl.h>
10 #include <pthread.h>
11  
12 #include "hiredis.h"
13 #include "async.h"
14 /* hiredis's own dict code for its subscription dicts; perl's headers have
15 * already set the feature macros fmacros.h would */
16 #define __HIREDIS_FMACRO_H
17 #include "dict.c"
18 #include "libev_adapter.h"
19 #include "ngx-queue.h"
20  
21 #ifdef EV_REDIS_SSL
22 #include "hiredis_ssl.h"
23 #include <openssl/err.h>
24 #endif
25  
26 typedef struct ev_redis_s ev_redis_t;
27 typedef struct ev_redis_cb_s ev_redis_cb_t;
28 typedef struct ev_redis_wait_s ev_redis_wait_t;
29 typedef struct ev_redis_drain_s ev_redis_drain_t;
30 typedef struct ev_redis_txn_s ev_redis_txn_t;
31  
32 typedef ev_redis_t* EV__Redis;
33 typedef struct ev_loop* EV__Loop;
34  
35 /* SvIV, but values above IV_MAX saturate instead of wrapping negative */
36 typedef IV sat_iv;
37 102 static IV sv_to_sat_iv(SV* sv) {
38 102 IV iv = SvIV(sv);
39 102 ▶ return (SvIsUV(sv) && iv < 0) ? IV_MAX : iv;
Branch: 2 branches
BranchTrueFalseTotal
return (SvIsUV(sv) && iv < 0) ? IV_MAX : iv; 0 102 102
return (SvIsUV(sv) && iv < 0) ? IV_MAX : iv; 0 0 0
40 }
41  
42 #define EV_REDIS_MAGIC 0xDEADBEEF
43 #define EV_REDIS_FREED 0xFEEDFACE
44  
45 /* nulls first: freeing it may run code that sets the field again */
46 #define CLEAR_HANDLER(field) \
47 do { SV* old_ = (SV*)(field); (field) = NULL; if (NULL != old_) SvREFCNT_dec(old_); } while(0)
48  
49 struct ev_redis_s {
50 unsigned int magic;
51 struct ev_loop* loop;
52 SV* loop_sv; /* the EV::Loop object itself, not a reference to it */
53 redisAsyncContext* ac;
54 UV connection_gen; /* each attempt supersedes older draining contexts */
55 SV* error_handler;
56 SV* connect_handler;
57 SV* disconnect_handler;
58 SV* push_handler;
59 struct timeval* connect_timeout;
60 struct timeval* command_timeout;
61 ngx_queue_t cb_queue;
62 ngx_queue_t wait_queue;
63 ngx_queue_t setup_queue; /* on_connect's delayed commands go first */
64 ngx_queue_t resume_setup_queue; /* setup kept for a later connection */
65 int pending_count;
66 int pending_detached; /* of pending_count: owed by a context disconnect() replaced */
67 int waiting_count;
68 UV cb_seq;
69 int max_pending; /* 0 = unlimited */
70 ev_redis_cb_t* current_cb;
71 int resume_waiting_on_reconnect;
72 int waiting_timeout_ms; /* 0 = unlimited */
73 ev_timer waiting_timer;
74 int waiting_timer_active;
75  
76 char* host;
77 int port;
78 char* path;
79 int reconnect;
80 int reconnect_delay_ms;
81 int max_reconnect_attempts; /* 0 = unlimited */
82 int reconnect_attempts;
83 ev_timer reconnect_timer;
84 int reconnect_timer_active;
85 int intentional_disconnect;
86 int priority;
87 int in_cb_cleanup;
88 int in_wait_cleanup;
89 int callback_depth; /* nonzero defers DESTROY's Safefree(self) to check_destroyed() */
90 int in_connect_handler; /* setup commands go ahead of waiting ones */
91 int monitoring;
92 int in_multi; /* confirmed transaction state on ac */
93 int pending_multi; /* transaction boundary replies still outstanding */
94 ev_redis_txn_t* issued_txn;
95 ev_tstamp barrier_since; /* the queue is held for those replies since; 0 = not held */
96 int keepalive; /* seconds, 0 = disabled */
97 int prefer_ipv4;
98 int prefer_ipv6;
99 char* source_addr;
100 unsigned int tcp_user_timeout; /* ms, 0 = OS default */
101 int cloexec;
102 int reuseaddr;
103 ngx_queue_t drain_queue;
104 #ifdef EV_REDIS_SSL
105 redisSSLContext* ssl_ctx;
106 #endif
107 };
108  
109 struct ev_redis_cb_s {
110 SV* cb;
111 redisAsyncContext* ac; /* owner: cb_queue is shared with draining old contexts */
112 ngx_queue_t queue;
113 ev_redis_txn_t* txn;
114 UV seq; /* order of creation: skip_pending spares the newer ones */
115 int sub_count; /* hiredis channel/pattern entries holding it; MONITOR: 1 */
116 unsigned char persist;
117 unsigned char monitor;
118 unsigned char skipped;
119 unsigned char running; /* its callback is on the stack: nested loops can run several */
120 unsigned char detached; /* its context was replaced: it awaits the reply but holds no slot */
121 unsigned char proto_cmd; /* HELLO or RESET: its reply tells the protocol now in use */
122 unsigned char multi; /* transaction boundary, if any */
123 unsigned char watch;
124 };
125  
126 struct ev_redis_wait_s {
127 char** argv;
128 size_t* argvlen;
129 int argc;
130 SV* cb;
131 int persist;
132 ev_redis_txn_t* txn;
133 ngx_queue_t queue;
134 ev_tstamp queued_at;
135 };
136  
137 struct ev_redis_drain_s {
138 redisAsyncContext* ac;
139 UV connection_gen;
140 ngx_queue_t queue;
141 };
142  
143 struct ev_redis_txn_s {
144 unsigned int refs;
145 int started;
146 int multi;
147 int multi_started;
148 int watched;
149 int closed;
150 };
151  
152 174 static ev_redis_txn_t* txn_ref(ev_redis_txn_t* txn) {
153 174 ▶ if (NULL != txn) txn->refs++;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != txn) txn->refs++; 0 174 174
154 174 return txn;
155 }
156  
157 508 static void txn_unref(ev_redis_txn_t* txn) {
158 508 ▶ if (NULL != txn && 0 == --txn->refs) Safefree(txn);
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != txn && 0 == --txn->refs) Safefree(txn); 0 508 508
if (NULL != txn && 0 == --txn->refs) Safefree(txn); 0 0 0
159 508 }
160  
161 0 static void txn_scope_free(pTHX_ void* ptr) {
162 0 txn_unref((ev_redis_txn_t*)ptr);
163 0 }
164  
165 73 static void free_cb_entry(ev_redis_cb_t* cbt) {
166 73 txn_unref(cbt->txn);
167 73 Safefree(cbt);
168 73 }
169  
170 /* Bind the pointer to its original referent; copies lack this marker or
171 * retain the original referent's address. No shared registry on the hot path. */
172 static MGVTBL ev_redis_object_vtbl = {0};
173  
174 72 static void ev_redis_mark_sv(SV* sv) {
175 72 SV* inner = SvRV(sv);
176 72 MAGIC* mg = sv_magicext(inner, NULL, PERL_MAGIC_ext,
177 &ev_redis_object_vtbl, NULL, 0);
178 72 mg->mg_ptr = (char*)inner;
179 72 }
180  
181 500 static int ev_redis_owns_sv(SV* inner) {
182 MAGIC* mg;
183 500 ▶ if (SvTYPE(inner) < SVt_PVMG) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(inner) < SVt_PVMG) return 0; 0 500 500
184 500 ▶ for (mg = SvMAGIC(inner); NULL != mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(inner); NULL != mg; mg = mg->mg_moremagic) { 492 8 500
185 492 ▶ if (mg->mg_virtual == &ev_redis_object_vtbl) return mg->mg_ptr == (char*)inner;
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_virtual == &ev_redis_object_vtbl) return mg->mg_ptr == (char*)inner; 492 0 492
186 }
187 8 return 0;
188 }
189  
190 /* Each queue is FIFO by queued_at; setup priority does not change deadlines. */
191 11 static ngx_queue_t* oldest_waiting(EV__Redis self) {
192 11 ngx_queue_t* q = ngx_queue_head(&self->wait_queue);
193 11 ngx_queue_t* setup = ngx_queue_head(&self->setup_queue);
194 11 ngx_queue_t* resume = ngx_queue_head(&self->resume_setup_queue);
195 11 ▶ if (!ngx_queue_empty(&self->setup_queue)
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->setup_queue) 0 11 11
196 0 ▶ && (ngx_queue_empty(&self->wait_queue)
Branch: 1 branch
BranchTrueFalseTotal
&& (ngx_queue_empty(&self->wait_queue) 0 0 0
197 0 || ngx_queue_data(setup, ev_redis_wait_t, queue)->queued_at
198 0 ▶ < ngx_queue_data(q, ev_redis_wait_t, queue)->queued_at)) q = setup;
Branch: 1 branch
BranchTrueFalseTotal
< ngx_queue_data(q, ev_redis_wait_t, queue)->queued_at)) q = setup; 0 0 0
199 11 ▶ if (!ngx_queue_empty(&self->resume_setup_queue)
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->resume_setup_queue) 0 11 11
200 0 ▶ && (q == ngx_queue_sentinel(&self->wait_queue)
Branch: 1 branch
BranchTrueFalseTotal
&& (q == ngx_queue_sentinel(&self->wait_queue) 0 0 0
201 0 || ngx_queue_data(resume, ev_redis_wait_t, queue)->queued_at
202 0 ▶ < ngx_queue_data(q, ev_redis_wait_t, queue)->queued_at)) q = resume;
Branch: 1 branch
BranchTrueFalseTotal
< ngx_queue_data(q, ev_redis_wait_t, queue)->queued_at)) q = resume; 0 0 0
203 11 return q;
204 }
205  
206 /* A new connection's setup takes precedence over setup kept from an older one. */
207 52 static void demote_setup_waiters(EV__Redis self) {
208 52 ▶ if (!ngx_queue_empty(&self->setup_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->setup_queue)) { 0 52 52
209 0 ngx_queue_add(&self->resume_setup_queue, &self->setup_queue);
210 0 ngx_queue_init(&self->setup_queue);
211 }
212 52 }
213  
214 static SV* err_skipped = NULL;
215 static SV* err_waiting_timeout = NULL;
216 static SV* err_disconnected = NULL;
217  
218 enum {
219 EV_REDIS_MULTI = 1,
220 EV_REDIS_EXEC,
221 EV_REDIS_DISCARD,
222 EV_REDIS_RESET
223 };
224  
225 /* protocol tokens are ASCII: libc case folding differs under tr_TR */
226 977 static int ascii_strcasecmp(const char* a, const char* b) {
227 int ca, cb;
228 do {
229 1354 ca = (unsigned char)*a++;
230 1354 cb = (unsigned char)*b++;
231 1354 ▶ if (ca >= 'A' && ca <= 'Z') ca += 'a' - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (ca >= 'A' && ca <= 'Z') ca += 'a' - 'A'; 1337 17 1354
if (ca >= 'A' && ca <= 'Z') ca += 'a' - 'A'; 622 715 1337
232 1354 ▶ if (cb >= 'A' && cb <= 'Z') cb += 'a' - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (cb >= 'A' && cb <= 'Z') cb += 'a' - 'A'; 1337 17 1354
if (cb >= 'A' && cb <= 'Z') cb += 'a' - 'A'; 0 1337 1337
233 1354 ▶ if (ca != cb) return ca - cb;
Branch: 1 branch
BranchTrueFalseTotal
if (ca != cb) return ca - cb; 960 394 1354
234 394 ▶ } while (0 != ca);
Branch: 1 branch
BranchTrueFalseTotal
} while (0 != ca); 377 17 394
235 17 return 0;
236 }
237  
238 157 static int transaction_command(const char* cmd) {
239 157 char c = cmd[0];
240 157 ▶ if ((c == 'm' || c == 'M') && 0 == ascii_strcasecmp(cmd, "multi")) return EV_REDIS_MULTI;
Branch: 3 branches
BranchTrueFalseTotal
if ((c == 'm' || c == 'M') && 0 == ascii_strcasecmp(cmd, "multi")) return EV_REDIS_MULTI; 157 0 157
if ((c == 'm' || c == 'M') && 0 == ascii_strcasecmp(cmd, "multi")) return EV_REDIS_MULTI; 0 157 157
if ((c == 'm' || c == 'M') && 0 == ascii_strcasecmp(cmd, "multi")) return EV_REDIS_MULTI; 0 0 0
241 157 ▶ if ((c == 'e' || c == 'E') && 0 == ascii_strcasecmp(cmd, "exec")) return EV_REDIS_EXEC;
Branch: 3 branches
BranchTrueFalseTotal
if ((c == 'e' || c == 'E') && 0 == ascii_strcasecmp(cmd, "exec")) return EV_REDIS_EXEC; 157 0 157
if ((c == 'e' || c == 'E') && 0 == ascii_strcasecmp(cmd, "exec")) return EV_REDIS_EXEC; 0 157 157
if ((c == 'e' || c == 'E') && 0 == ascii_strcasecmp(cmd, "exec")) return EV_REDIS_EXEC; 0 0 0
242 157 ▶ if ((c == 'd' || c == 'D') && 0 == ascii_strcasecmp(cmd, "discard")) return EV_REDIS_DISCARD;
Branch: 3 branches
BranchTrueFalseTotal
if ((c == 'd' || c == 'D') && 0 == ascii_strcasecmp(cmd, "discard")) return EV_REDIS_DISCARD; 157 0 157
if ((c == 'd' || c == 'D') && 0 == ascii_strcasecmp(cmd, "discard")) return EV_REDIS_DISCARD; 0 157 157
if ((c == 'd' || c == 'D') && 0 == ascii_strcasecmp(cmd, "discard")) return EV_REDIS_DISCARD; 0 0 0
243 157 ▶ if ((c == 'r' || c == 'R') && 0 == ascii_strcasecmp(cmd, "reset")) return EV_REDIS_RESET;
Branch: 3 branches
BranchTrueFalseTotal
if ((c == 'r' || c == 'R') && 0 == ascii_strcasecmp(cmd, "reset")) return EV_REDIS_RESET; 157 0 157
if ((c == 'r' || c == 'R') && 0 == ascii_strcasecmp(cmd, "reset")) return EV_REDIS_RESET; 0 157 157
if ((c == 'r' || c == 'R') && 0 == ascii_strcasecmp(cmd, "reset")) return EV_REDIS_RESET; 0 0 0
244 157 return 0;
245 }
246  
247 /* these replies cannot safely be queued inside a transaction */
248 0 static int needs_transaction_reply(int persist, const char* cmd) {
249 0 ▶ return persist || ((cmd[0] == 'h' || cmd[0] == 'H') && 0 == ascii_strcasecmp(cmd, "hello"));
Branch: 4 branches
BranchTrueFalseTotal
return persist || ((cmd[0] == 'h' || cmd[0] == 'H') && 0 == ascii_strcasecmp(cmd, "hello")); 0 0 0
return persist || ((cmd[0] == 'h' || cmd[0] == 'H') && 0 == ascii_strcasecmp(cmd, "hello")); 0 0 0
return persist || ((cmd[0] == 'h' || cmd[0] == 'H') && 0 == ascii_strcasecmp(cmd, "hello")); 0 0 0
return persist || ((cmd[0] == 'h' || cmd[0] == 'H') && 0 == ascii_strcasecmp(cmd, "hello")); 0 0 0
250 }
251  
252 /* errors after which the server holds no transaction (a cluster redirect, or
253 * before 6.2 a demotion to read-only replica, discards it without EXECABORT) */
254 0 static int ends_transaction(const redisReply* err) {
255 static const char* const prefix[] = {
256 "EXECABORT", "MOVED ", "ASK ", "TRYAGAIN", "CLUSTERDOWN", "CROSSSLOT"
257 };
258 size_t i;
259 0 ▶ for (i = 0; i < sizeof(prefix) / sizeof(prefix[0]); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof(prefix) / sizeof(prefix[0]); i++) { 0 0 0
260 0 ▶ if (0 == strncmp(err->str, prefix[i], strlen(prefix[i]))) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (0 == strncmp(err->str, prefix[i], strlen(prefix[i]))) return 1; 0 0 0
261 }
262 0 ▶ return NULL != strstr(err->str, "without MULTI") || NULL != strstr(err->str, "EXEC aborted");
Branch: 2 branches
BranchTrueFalseTotal
return NULL != strstr(err->str, "without MULTI") || NULL != strstr(err->str, "EXEC aborted"); 0 0 0
return NULL != strstr(err->str, "without MULTI") || NULL != strstr(err->str, "EXEC aborted"); 0 0 0
263 }
264  
265 15 static int is_unsubscribe_command(const char* cmd) {
266 15 char c = cmd[0];
267 15 ▶ if (c == 'u' || c == 'U') return (0 == ascii_strcasecmp(cmd, "unsubscribe"));
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'u' || c == 'U') return (0 == ascii_strcasecmp(cmd, "unsubscribe")); 15 0 15
if (c == 'u' || c == 'U') return (0 == ascii_strcasecmp(cmd, "unsubscribe")); 0 15 15
268 15 ▶ if (c == 'p' || c == 'P') return (0 == ascii_strcasecmp(cmd, "punsubscribe"));
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'p' || c == 'P') return (0 == ascii_strcasecmp(cmd, "punsubscribe")); 15 0 15
if (c == 'p' || c == 'P') return (0 == ascii_strcasecmp(cmd, "punsubscribe")); 0 15 15
269 15 return 0;
270 }
271  
272 /* hiredis treats SSUBSCRIBE as one-shot: an smessage then fails the
273 * connection (RESP2) or is misrouted to on_push (RESP3). */
274 84 static int is_shard_pubsub_command(const char* cmd) {
275 84 char c = cmd[0];
276 84 ▶ if (c != 's' && c != 'S') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c != 's' && c != 'S') return 0; 73 11 84
if (c != 's' && c != 'S') return 0; 65 8 73
277 38 return (0 == ascii_strcasecmp(cmd, "ssubscribe")) ||
278 19 (0 == ascii_strcasecmp(cmd, "sunsubscribe"));
279 }
280  
281 84 static int is_persistent_command(const char* cmd) {
282 84 char c = cmd[0];
283  
284 84 ▶ if (c == 's' || c == 'S') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 's' || c == 'S') { 73 11 84
if (c == 's' || c == 'S') { 8 65 73
285 19 return (0 == ascii_strcasecmp(cmd, "subscribe"));
286 }
287 65 ▶ if (c == 'u' || c == 'U') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'u' || c == 'U') { 65 0 65
if (c == 'u' || c == 'U') { 0 65 65
288 0 return (0 == ascii_strcasecmp(cmd, "unsubscribe"));
289 }
290 65 ▶ if (c == 'p' || c == 'P') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'p' || c == 'P') { 42 23 65
if (c == 'p' || c == 'P') { 13 29 42
291 36 ▶ if (0 == ascii_strcasecmp(cmd, "psubscribe")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (0 == ascii_strcasecmp(cmd, "psubscribe")) return 1; 0 36 36
292 36 ▶ if (0 == ascii_strcasecmp(cmd, "punsubscribe")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (0 == ascii_strcasecmp(cmd, "punsubscribe")) return 1; 0 36 36
293 36 return 0;
294 }
295 29 ▶ if (c == 'm' || c == 'M') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'm' || c == 'M') { 29 0 29
if (c == 'm' || c == 'M') { 0 29 29
296 0 return (0 == ascii_strcasecmp(cmd, "monitor"));
297 }
298  
299 29 return 0;
300 }
301  
302 250 static int is_monitor_command(const char* cmd) {
303 250 ▶ return (cmd[0] == 'm' || cmd[0] == 'M') && 0 == ascii_strcasecmp(cmd, "monitor");
Branch: 3 branches
BranchTrueFalseTotal
return (cmd[0] == 'm' || cmd[0] == 'M') && 0 == ascii_strcasecmp(cmd, "monitor"); 250 0 250
return (cmd[0] == 'm' || cmd[0] == 'M') && 0 == ascii_strcasecmp(cmd, "monitor"); 0 250 250
return (cmd[0] == 'm' || cmd[0] == 'M') && 0 == ascii_strcasecmp(cmd, "monitor"); 0 0 0
304 }
305  
306 /* the server answers neither it nor (SKIP) the next command, and hiredis
307 * pairs replies with callbacks by order */
308 84 static int is_reply_suppressing(int argc, char** argv) {
309 84 ▶ if (argc == 3 && 0 == ascii_strcasecmp(argv[0], "client") && 0 == ascii_strcasecmp(argv[1], "reply")) {
Branch: 3 branches
BranchTrueFalseTotal
if (argc == 3 && 0 == ascii_strcasecmp(argv[0], "client") && 0 == ascii_strcasecmp(argv[1], "reply")) { 4 80 84
if (argc == 3 && 0 == ascii_strcasecmp(argv[0], "client") && 0 == ascii_strcasecmp(argv[1], "reply")) { 0 4 4
if (argc == 3 && 0 == ascii_strcasecmp(argv[0], "client") && 0 == ascii_strcasecmp(argv[1], "reply")) { 0 0 0
310 0 ▶ return 0 == ascii_strcasecmp(argv[2], "off") || 0 == ascii_strcasecmp(argv[2], "skip");
Branch: 2 branches
BranchTrueFalseTotal
return 0 == ascii_strcasecmp(argv[2], "off") || 0 == ascii_strcasecmp(argv[2], "skip"); 0 0 0
return 0 == ascii_strcasecmp(argv[2], "off") || 0 == ascii_strcasecmp(argv[2], "skip"); 0 0 0
311 }
312 84 ▶ if (0 == ascii_strcasecmp(argv[0], "replconf")) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == ascii_strcasecmp(argv[0], "replconf")) { 0 84 84
313 /* the server takes option, value pairs in order */
314 int j;
315 0 ▶ for (j = 1; j < argc; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < argc; j += 2) { 0 0 0
316 0 ▶ if (0 == ascii_strcasecmp(argv[j], "ack") || 0 == ascii_strcasecmp(argv[j], "getack")) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (0 == ascii_strcasecmp(argv[j], "ack") || 0 == ascii_strcasecmp(argv[j], "getack")) return 1; 0 0 0
if (0 == ascii_strcasecmp(argv[j], "ack") || 0 == ascii_strcasecmp(argv[j], "getack")) return 1; 0 0 0
317 }
318 }
319 84 return 0;
320 }
321  
322 /* hiredis keeps its subscription state through RESET, which ends it on the
323 * server: later replies would be taken for messages or shift callbacks */
324 0 static int may_be_subscribed(EV__Redis self) {
325 ngx_queue_t* q;
326 int i;
327 0 ▶ if (NULL != self->ac && (self->ac->c.flags & REDIS_SUBSCRIBED)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != self->ac && (self->ac->c.flags & REDIS_SUBSCRIBED)) return 1; 0 0 0
if (NULL != self->ac && (self->ac->c.flags & REDIS_SUBSCRIBED)) return 1; 0 0 0
328 0 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 0 0 0
329 0 ngx_queue_t* list = i == 0 ? &self->wait_queue
330 0 ▶ : i == 1 ? &self->setup_queue : &self->resume_setup_queue;
Branch: 2 branches
BranchTrueFalseTotal
: i == 1 ? &self->setup_queue : &self->resume_setup_queue; 0 0 0
: i == 1 ? &self->setup_queue : &self->resume_setup_queue; 0 0 0
331 0 ▶ for (q = ngx_queue_head(list); q != ngx_queue_sentinel(list); q = ngx_queue_next(q)) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = ngx_queue_head(list); q != ngx_queue_sentinel(list); q = ngx_queue_next(q)) { 0 0 0
332 0 ev_redis_wait_t* wt = ngx_queue_data(q, ev_redis_wait_t, queue);
333 /* a queued unsubscribe removes subscriptions; it is not one */
334 0 ▶ if (wt->persist && !is_unsubscribe_command(wt->argv[0])) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (wt->persist && !is_unsubscribe_command(wt->argv[0])) return 1; 0 0 0
if (wt->persist && !is_unsubscribe_command(wt->argv[0])) return 1; 0 0 0
335 }
336 }
337 0 return 0;
338 }
339  
340 /* once hiredis has seen a push it no longer takes RESP2 arrays for pub/sub
341 * messages; HELLO answers in the protocol it switched to, RESET with +RESET
342 * (inside MULTI, +QUEUED switches nothing) */
343 1 static void note_protocol_reply(redisAsyncContext* c, const redisReply* reply) {
344 1 ▶ if (reply->type == REDIS_REPLY_ARRAY
Branch: 1 branch
BranchTrueFalseTotal
if (reply->type == REDIS_REPLY_ARRAY 1 0 1
345 1 ▶ || (reply->type == REDIS_REPLY_STATUS && 0 == ascii_strcasecmp(reply->str, "reset"))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (reply->type == REDIS_REPLY_STATUS && 0 == ascii_strcasecmp(reply->str, "reset"))) { 0 1 1
|| (reply->type == REDIS_REPLY_STATUS && 0 == ascii_strcasecmp(reply->str, "reset"))) { 0 0 0
346 0 c->c.flags &= ~REDIS_SUPPORTS_PUSH;
347 }
348 1 }
349  
350 /* [type, channel, remaining_count] */
351 7 static int is_unsub_reply(redisReply* reply) {
352 const char* s;
353  
354 7 ▶ if (reply->type != REDIS_REPLY_ARRAY && reply->type != REDIS_REPLY_PUSH) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (reply->type != REDIS_REPLY_ARRAY && reply->type != REDIS_REPLY_PUSH) return 0; 0 7 7
if (reply->type != REDIS_REPLY_ARRAY && reply->type != REDIS_REPLY_PUSH) return 0; 0 0 0
355 7 ▶ if (reply->elements < 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (reply->elements < 3) return 0; 0 7 7
356 7 ▶ if (NULL == reply->element[0]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == reply->element[0]) return 0; 0 7 7
357 7 ▶ if (reply->element[0]->type != REDIS_REPLY_STRING &&
Branch: 1 branch
BranchTrueFalseTotal
if (reply->element[0]->type != REDIS_REPLY_STRING && 0 7 7
358 0 ▶ reply->element[0]->type != REDIS_REPLY_STATUS) return 0;
Branch: 1 branch
BranchTrueFalseTotal
reply->element[0]->type != REDIS_REPLY_STATUS) return 0; 0 0 0
359  
360 7 s = reply->element[0]->str;
361 7 ▶ if (s[0] == 'u' || s[0] == 'U') return (0 == ascii_strcasecmp(s, "unsubscribe"));
Branch: 2 branches
BranchTrueFalseTotal
if (s[0] == 'u' || s[0] == 'U') return (0 == ascii_strcasecmp(s, "unsubscribe")); 7 0 7
if (s[0] == 'u' || s[0] == 'U') return (0 == ascii_strcasecmp(s, "unsubscribe")); 0 7 7
362 7 ▶ if (s[0] == 'p' || s[0] == 'P') return (0 == ascii_strcasecmp(s, "punsubscribe"));
Branch: 2 branches
BranchTrueFalseTotal
if (s[0] == 'p' || s[0] == 'P') return (0 == ascii_strcasecmp(s, "punsubscribe")); 7 0 7
if (s[0] == 'p' || s[0] == 'P') return (0 == ascii_strcasecmp(s, "punsubscribe")); 0 7 7
363 7 ▶ if (s[0] == 's' || s[0] == 'S') return (0 == ascii_strcasecmp(s, "sunsubscribe"));
Branch: 2 branches
BranchTrueFalseTotal
if (s[0] == 's' || s[0] == 'S') return (0 == ascii_strcasecmp(s, "sunsubscribe")); 1 6 7
if (s[0] == 's' || s[0] == 'S') return (0 == ascii_strcasecmp(s, "sunsubscribe")); 0 1 1
364 1 return 0;
365 }
366  
367 /* hiredis's callback dictionaries hash SDS bytes with dictGenHashFunction. */
368 15 static dictEntry* sub_entry(dict* d, const char* name, size_t len) {
369 dictEntry* he;
370 15 ▶ if (NULL == d || 0 == d->size) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == d || 0 == d->size) return NULL; 15 0 15
if (NULL == d || 0 == d->size) return NULL; 10 5 15
371 5 for (he = d->table[dictGenHashFunction((const unsigned char*)name, (int)len) & d->sizemask];
372 6 ▶ he; he = he->next) {
Branch: 1 branch
BranchTrueFalseTotal
he; he = he->next) { 1 5 6
373 1 sds key = (sds)dictGetEntryKey(he);
374 1 ▶ if (sdslen(key) == len && 0 == memcmp(key, name, len)) return he;
Branch: 2 branches
BranchTrueFalseTotal
if (sdslen(key) == len && 0 == memcmp(key, name, len)) return he; 1 0 1
if (sdslen(key) == len && 0 == memcmp(key, name, len)) return he; 0 1 1
375 }
376 5 return NULL;
377 }
378  
379 /* The cbt hiredis delivers name to: a re-subscribe replaces the entry, and an
380 * unsubscribe reply drops it only when no subscribe confirmation is pending. */
381 15 static ev_redis_cb_t* sub_owner(redisAsyncContext* ac, int pattern,
382 const char* name, size_t len, redisCallback** sub) {
383 15 ▶ dict* d = pattern ? ac->sub.patterns : ac->sub.channels;
Branch: 1 branch
BranchTrueFalseTotal
dict* d = pattern ? ac->sub.patterns : ac->sub.channels; 0 15 15
384 15 dictEntry* he = sub_entry(d, name, len);
385  
386 15 ▶ if (NULL != sub) *sub = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != sub) *sub = NULL; 15 0 15
387 15 ▶ if (NULL != he) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != he) { 0 15 15
388 0 redisCallback* cb = (redisCallback*)dictGetEntryVal(he);
389 0 ▶ if (NULL != sub) *sub = cb;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != sub) *sub = cb; 0 0 0
390 0 return (ev_redis_cb_t*)cb->privdata;
391 }
392 15 return NULL;
393 }
394  
395 /* A persistent cbt got reply: count the hiredis entry it lost, if any. At
396 * teardown hiredis calls it with NULL once per entry (MONITOR: once). */
397 21 static void sub_note_reply(redisAsyncContext* c, ev_redis_cb_t* cbt, redisReply* reply) {
398 redisReply* name;
399 ev_redis_cb_t* owner;
400 redisCallback* sub;
401 redisLibevEvents* e;
402 int pattern;
403  
404 21 ▶ if (NULL == reply) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == reply) { 14 7 21
405 14 cbt->sub_count--;
406 21 return;
407 }
408 7 ▶ if (!is_unsub_reply(reply)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_unsub_reply(reply)) return; 7 0 7
409 0 name = reply->element[1];
410 0 ▶ if (NULL == name || name->type != REDIS_REPLY_STRING) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == name || name->type != REDIS_REPLY_STRING) return; 0 0 0
if (NULL == name || name->type != REDIS_REPLY_STRING) return; 0 0 0
411 0 ▶ pattern = reply->element[0]->str[0] == 'p' || reply->element[0]->str[0] == 'P';
Branch: 2 branches
BranchTrueFalseTotal
pattern = reply->element[0]->str[0] == 'p' || reply->element[0]->str[0] == 'P'; 0 0 0
pattern = reply->element[0]->str[0] == 'p' || reply->element[0]->str[0] == 'P'; 0 0 0
412 0 owner = sub_owner(c, pattern, name->str, name->len, &sub);
413 0 e = (redisLibevEvents*)c->ev.data;
414 0 ▶ if (NULL != e && NULL != e->unsubs[pattern]) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != e && NULL != e->unsubs[pattern]) { 0 0 0
if (NULL != e && NULL != e->unsubs[pattern]) { 0 0 0
415 0 HV* counts = e->unsubs[pattern];
416 0 SV** count = hv_fetch(counts, name->str, (I32)name->len, 0);
417 0 ▶ if (NULL != count) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != count) { 0 0 0
418 0 IV n = SvIV(*count);
419 0 ▶ if (n > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) { 0 0 0
420 0 sv_setiv(*count, n - 1);
421 /* each extra unsubscribe kept the entry through this reply */
422 0 ▶ if (NULL != sub) sub->pending_subs--;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != sub) sub->pending_subs--; 0 0 0
423 }
424 0 else (void)hv_delete(counts, name->str, (I32)name->len, G_DISCARD);
425 }
426 }
427 0 ▶ if (owner != cbt) cbt->sub_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (owner != cbt) cbt->sub_count--; 0 0 0
428 }
429  
430 /* hiredis records a TLS failure as an I/O error with errno 0 ("Success"): name
431 * the OpenSSL reason, kept in the context as a callback may clear the queue */
432 150 static const char* conn_errstr(const redisAsyncContext* c, const char* fallback) {
433 #ifdef EV_REDIS_SSL
434 150 ▶ if (REDIS_ERR_IO == c->c.err && NULL != c->c.privctx
Branch: 2 branches
BranchTrueFalseTotal
if (REDIS_ERR_IO == c->c.err && NULL != c->c.privctx 34 116 150
if (REDIS_ERR_IO == c->c.err && NULL != c->c.privctx 0 34 34
435 0 ▶ && 0 == strcmp(c->c.errstr, strerror(0))) {
Branch: 1 branch
BranchTrueFalseTotal
&& 0 == strcmp(c->c.errstr, strerror(0))) { 0 0 0
436 0 const char* reason = ERR_reason_error_string(ERR_peek_last_error());
437 0 ▶ if (NULL != reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != reason) { 0 0 0
438 0 char* errstr = (char*)c->c.errstr;
439 0 strncpy(errstr, reason, sizeof(c->c.errstr) - 1);
440 0 errstr[sizeof(c->c.errstr) - 1] = '\0';
441 }
442 }
443 #endif
444 150 ▶ return c->errstr[0] ? c->errstr : fallback;
Branch: 1 branch
BranchTrueFalseTotal
return c->errstr[0] ? c->errstr : fallback; 149 1 150
445 }
446  
447 static HV* ev_redis_stash;
448  
449 /* croak() names the XSUB's calling statement; inside our own subs (AUTOLOAD's
450 * methods, new) name their caller. The catching eval restores PL_curcop. */
451 109 static COP* caller_cop(void) {
452 I32 i;
453 192 ▶ for (i = cxstack_ix; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = cxstack_ix; i >= 0; i--) { 166 26 192
454 166 const PERL_CONTEXT* cx = &cxstack[i];
455 166 ▶ if (CxTYPE(cx) != CXt_SUB) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (CxTYPE(cx) != CXt_SUB) continue; 83 83 166
456 {
457 /* by name too: each ithread has its own stash */
458 83 HV* stash = CvSTASH(cx->blk_sub.cv);
459 83 ▶ const char* name = NULL == stash ? NULL : HvNAME_get(stash);
Branch: 7 branches
BranchTrueFalseTotal
const char* name = NULL == stash ? NULL : HvNAME_get(stash); 83 0 83
const char* name = NULL == stash ? NULL : HvNAME_get(stash); 83 0 83
const char* name = NULL == stash ? NULL : HvNAME_get(stash); 83 0 83
const char* name = NULL == stash ? NULL : HvNAME_get(stash); 0 83 83
const char* name = NULL == stash ? NULL : HvNAME_get(stash); 0 0 0
const char* name = NULL == stash ? NULL : HvNAME_get(stash); 83 0 83
const char* name = NULL == stash ? NULL : HvNAME_get(stash); 0 83 83
460 166 int ours = stash == ev_redis_stash
461 83 ▶ || (NULL != name && strEQ(name, "EV::Redis"));
Branch: 3 branches
BranchTrueFalseTotal
|| (NULL != name && strEQ(name, "EV::Redis")); 36 47 83
|| (NULL != name && strEQ(name, "EV::Redis")); 36 0 36
|| (NULL != name && strEQ(name, "EV::Redis")); 0 36 36
462 83 ▶ return ours ? cx->blk_oldcop : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return ours ? cx->blk_oldcop : NULL; 47 36 83
463 }
464 }
465 26 return NULL;
466 }
467  
468 24 static void croak_caller(const char* pat, ...) {
469 va_list args;
470 SV* msg;
471 24 COP* cop = caller_cop();
472 24 ▶ if (NULL != cop) PL_curcop = cop;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cop) PL_curcop = cop; 9 15 24
473 24 va_start(args, pat);
474 24 msg = sv_2mortal(vnewSVpvf(pat, &args));
475 24 va_end(args);
476 24 Perl_croak(aTHX_ "%" SVf, SVfARG(msg));
477 }
478  
479 /* hiredis and OpenSSL take C strings: a NUL would cut it short */
480 66 static const char* c_string(SV* sv, const char* what) {
481 STRLEN len;
482 66 const char* s = SvPV(sv, len);
483 66 ▶ if (strlen(s) != len) croak_caller("%s contains a NUL byte", what);
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(s) != len) croak_caller("%s contains a NUL byte", what); 6 60 66
484 60 return s;
485 }
486  
487 /* a UTF-8 lead byte of 0xC4 or more starts a character above 0xFF */
488 2 static int has_wide_char(SV* sv) {
489 2 STRLEN len = SvCUR(sv);
490 2 const U8* s = (const U8*)SvPVX(sv);
491 7 ▶ while (len--) {
Branch: 1 branch
BranchTrueFalseTotal
while (len--) { 6 1 7
492 6 ▶ if (*s++ >= 0xC4) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (*s++ >= 0xC4) return 1; 1 5 6
493 }
494 1 return 0;
495 }
496  
497 0 static void print_error_line(const char* prefix, SV* sv) {
498 STRLEN len;
499 0 const char* s = SvPV(sv, len);
500 0 ▶ PerlIO_puts(Perl_error_log, prefix);
Branch: 4 branches
BranchTrueFalseTotal
PerlIO_puts(Perl_error_log, prefix); 0 0 0
PerlIO_puts(Perl_error_log, prefix); 0 0 0
PerlIO_puts(Perl_error_log, prefix); 0 0 0
PerlIO_puts(Perl_error_log, prefix); 0 0 0
501 0 ▶ PerlIO_write(Perl_error_log, s, len);
Branch: 4 branches
BranchTrueFalseTotal
PerlIO_write(Perl_error_log, s, len); 0 0 0
PerlIO_write(Perl_error_log, s, len); 0 0 0
PerlIO_write(Perl_error_log, s, len); 0 0 0
PerlIO_write(Perl_error_log, s, len); 0 0 0
502 0 ▶ if (0 == len || '\n' != s[len - 1]) PerlIO_puts(Perl_error_log, "\n");
Branch: 6 branches
BranchTrueFalseTotal
if (0 == len || '\n' != s[len - 1]) PerlIO_puts(Perl_error_log, "\n"); 0 0 0
if (0 == len || '\n' != s[len - 1]) PerlIO_puts(Perl_error_log, "\n"); 0 0 0
if (0 == len || '\n' != s[len - 1]) PerlIO_puts(Perl_error_log, "\n"); 0 0 0
if (0 == len || '\n' != s[len - 1]) PerlIO_puts(Perl_error_log, "\n"); 0 0 0
if (0 == len || '\n' != s[len - 1]) PerlIO_puts(Perl_error_log, "\n"); 0 0 0
if (0 == len || '\n' != s[len - 1]) PerlIO_puts(Perl_error_log, "\n"); 0 0 0
503 0 }
504  
505 /* a die from $SIG{__WARN__} would unwind through hiredis and leave it in REDIS_IN_CALLBACK */
506 2 static void warn_exception(const char* what, SV* err) {
507 2 dSP;
508 SV* msg;
509 2 ENTER;
510 2 SAVETMPS;
511 2 save_scalar(PL_errgv); /* G_EVAL clears $@ in place, and err may be it */
512 2 msg = sv_newmortal();
513 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
514 2 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 2 2
515 2 mPUSHp(what, strlen(what));
516 2 PUSHs(err);
517 2 PUSHs(msg);
518 2 PUTBACK;
519 2 call_pv("EV::Redis::_warn", G_DISCARD | G_EVAL);
520 2 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 2 0 2
if (SvTRUE(ERRSV)) { 0 2 2
521 0 ▶ if (SvOK(msg)) print_error_line("", msg);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(msg)) print_error_line("", msg); 0 0 0
522 0 ▶ else PerlIO_printf(Perl_error_log, "EV::Redis: exception in %s\n", what);
Branch: 4 branches
BranchTrueFalseTotal
else PerlIO_printf(Perl_error_log, "EV::Redis: exception in %s\n", what); 0 0 0
else PerlIO_printf(Perl_error_log, "EV::Redis: exception in %s\n", what); 0 0 0
else PerlIO_printf(Perl_error_log, "EV::Redis: exception in %s\n", what); 0 0 0
else PerlIO_printf(Perl_error_log, "EV::Redis: exception in %s\n", what); 0 0 0
523 0 ▶ print_error_line("EV::Redis: reporting it died: ", ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
print_error_line("EV::Redis: reporting it died: ", ERRSV); 0 0 0
524 }
525 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
526 2 LEAVE;
527 2 }
528  
529 122 static void emit_error(EV__Redis self, SV* error) {
530 122 SV* handler = self->error_handler;
531 122 ▶ if (NULL == handler) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == handler) return; 0 122 122
532 /* pin: the handler may clear itself ($r->on_error(undef)) mid-call */
533 122 SvREFCNT_inc_simple_void_NN(handler);
534  
535 122 dSP;
536  
537 122 ENTER;
538 122 SAVETMPS;
539 122 save_scalar(PL_errgv); /* G_EVAL must not clobber the caller's $@ */
540  
541 122 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 122 122
542 122 ▶ XPUSHs(error);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(error); 0 122 122
543 122 PUTBACK;
544  
545 122 call_sv(handler, G_DISCARD | G_EVAL);
546 122 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 122 0 122
if (SvTRUE(ERRSV)) { 0 122 122
547 0 ▶ warn_exception("error handler", ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
warn_exception("error handler", ERRSV); 0 0 0
548 }
549  
550 122 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 122 122
551 122 LEAVE;
552 122 SvREFCNT_dec(handler);
553 }
554  
555 55 static void emit_error_str(EV__Redis self, const char* error) {
556 55 ▶ if (NULL == self->error_handler) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->error_handler) return; 0 55 55
557 55 emit_error(self, sv_2mortal(newSVpv(error, 0)));
558 }
559  
560 65 static void invoke_callback_error(SV* cb, SV* error_sv) {
561 65 dSP;
562 65 ENTER;
563 65 SAVETMPS;
564 65 save_scalar(PL_errgv);
565 65 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 65 65
566 65 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 65 65
567 65 PUSHs(sv_newmortal()); /* not &PL_sv_undef: $_[0] must be writable */
568 /* newSVsv: sv_mortalcopy may steal a TEMP's string, and error_sv serves every
569 * callback of a batch; a copy also keeps @_ off shared READONLY SVs */
570 65 PUSHs(sv_2mortal(newSVsv(error_sv)));
571 65 PUTBACK;
572 65 call_sv(cb, G_DISCARD | G_EVAL);
573 65 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 65 0 65
if (SvTRUE(ERRSV)) { 2 63 65
574 2 ▶ warn_exception("command callback", ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
warn_exception("command callback", ERRSV); 2 0 2
575 }
576 65 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 65 0 65
577 65 LEAVE;
578 65 }
579  
580 /* Call after callback_depth--; returns 1 if a deferred DESTROY freed self. */
581 431 static int check_destroyed(EV__Redis self) {
582 431 ▶ if (self->magic == EV_REDIS_FREED &&
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED && 73 358 431
583 73 ▶ self->callback_depth == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
self->callback_depth == 0 && 72 1 73
584 72 ▶ self->current_cb == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
self->current_cb == NULL) { 72 0 72
585 72 Safefree(self);
586 72 return 1;
587 }
588 359 return 0;
589 }
590  
591 136 static void free_c_fields(EV__Redis self) {
592 136 txn_unref(self->issued_txn);
593 136 self->issued_txn = NULL;
594 136 ▶ if (NULL != self->host) { Safefree(self->host); self->host = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->host) { Safefree(self->host); self->host = NULL; } 40 96 136
595 136 ▶ if (NULL != self->path) { Safefree(self->path); self->path = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->path) { Safefree(self->path); self->path = NULL; } 13 123 136
596 136 ▶ if (NULL != self->source_addr) { Safefree(self->source_addr); self->source_addr = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->source_addr) { Safefree(self->source_addr); self->source_addr = NULL; } 1 135 136
597 136 ▶ if (NULL != self->connect_timeout) { Safefree(self->connect_timeout); self->connect_timeout = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->connect_timeout) { Safefree(self->connect_timeout); self->connect_timeout = NULL; } 5 131 136
598 136 ▶ if (NULL != self->command_timeout) { Safefree(self->command_timeout); self->command_timeout = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->command_timeout) { Safefree(self->command_timeout); self->command_timeout = NULL; } 15 121 136
599 #ifdef EV_REDIS_SSL
600 136 ▶ if (NULL != self->ssl_ctx) { redisFreeSSLContext(self->ssl_ctx); self->ssl_ctx = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ssl_ctx) { redisFreeSSLContext(self->ssl_ctx); self->ssl_ctx = NULL; } 4 132 136
601 #endif
602 136 }
603  
604 /* Global destruction curses objects and clears RVs in arena order, so a
605 * non-default loop may be gone first; the default loop never is. */
606 496 static int loop_alive(EV__Redis self) {
607 496 ▶ if (NULL == self->loop) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) return 0; 0 496 496
608 496 ▶ if (!PL_dirty || self->loop == EV_DEFAULT_UC) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!PL_dirty || self->loop == EV_DEFAULT_UC) return 1; 8 488 496
if (!PL_dirty || self->loop == EV_DEFAULT_UC) return 1; 8 0 8
609 0 ▶ return NULL != self->loop_sv && SvOBJECT(self->loop_sv);
Branch: 2 branches
BranchTrueFalseTotal
return NULL != self->loop_sv && SvOBJECT(self->loop_sv); 0 0 0
return NULL != self->loop_sv && SvOBJECT(self->loop_sv); 0 0 0
610 }
611  
612 258 static void stop_waiting_timer(EV__Redis self) {
613 258 ▶ if (self->waiting_timer_active && loop_alive(self)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->waiting_timer_active && loop_alive(self)) { 0 258 258
if (self->waiting_timer_active && loop_alive(self)) { 0 0 0
614 0 ev_timer_stop(self->loop, &self->waiting_timer);
615 0 self->waiting_timer_active = 0;
616 }
617 258 }
618  
619 154 static void stop_reconnect_timer(EV__Redis self) {
620 154 ▶ if (self->reconnect_timer_active && loop_alive(self)) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->reconnect_timer_active && loop_alive(self)) { 4 150 154
if (self->reconnect_timer_active && loop_alive(self)) { 4 0 4
621 4 ev_timer_stop(self->loop, &self->reconnect_timer);
622 4 self->reconnect_timer_active = 0;
623 }
624 154 }
625  
626 /* ~23 days; fits a 32-bit int */
627 #define MAX_TIMEOUT_MS 2000000000
628 /* Linux caps TCP_KEEPIDLE here, and an out-of-range setsockopt breaks the connection */
629 #define MAX_KEEPALIVE 32767
630  
631 39 static void validate_timeout_ms(IV ms, const char* name) {
632 39 ▶ if (ms < 0) croak_caller("%s must be non-negative", name);
Branch: 1 branch
BranchTrueFalseTotal
if (ms < 0) croak_caller("%s must be non-negative", name); 0 39 39
633 39 ▶ if (ms > MAX_TIMEOUT_MS) croak_caller("%s too large (max %d ms)", name, MAX_TIMEOUT_MS);
Branch: 1 branch
BranchTrueFalseTotal
if (ms > MAX_TIMEOUT_MS) croak_caller("%s too large (max %d ms)", name, MAX_TIMEOUT_MS); 0 39 39
634 39 }
635  
636 20 static SV* timeout_accessor(struct timeval** tv_ptr, SV* timeout_ms, const char* name) {
637 20 ▶ if (NULL != timeout_ms && SvOK(timeout_ms)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != timeout_ms && SvOK(timeout_ms)) { 20 0 20
if (NULL != timeout_ms && SvOK(timeout_ms)) { 20 0 20
638 20 IV ms = sv_to_sat_iv(timeout_ms);
639 20 validate_timeout_ms(ms, name);
640 20 ▶ if (NULL == *tv_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == *tv_ptr) { 20 0 20
641 20 Newx(*tv_ptr, 1, struct timeval);
642 }
643 20 (*tv_ptr)->tv_sec = (long)(ms / 1000);
644 20 (*tv_ptr)->tv_usec = (long)((ms % 1000) * 1000);
645 }
646  
647 20 ▶ if (NULL != *tv_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != *tv_ptr) { 20 0 20
648 20 return newSViv((IV)(*tv_ptr)->tv_sec * 1000 + (*tv_ptr)->tv_usec / 1000);
649 }
650 0 return &PL_sv_undef;
651 }
652  
653 /* The old handler is handed back for the caller to release last: its closure
654 * may hold the only strong reference to self. */
655 108 static SV* handler_accessor(SV** handler_ptr, SV* handler, int has_handler_arg, SV** old) {
656 108 ▶ int is_code = has_handler_arg && NULL != handler
Branch: 1 branch
BranchTrueFalseTotal
int is_code = has_handler_arg && NULL != handler 108 0 108
657 216 ▶ && SvROK(handler) && SvTYPE(SvRV(handler)) == SVt_PVCV;
Branch: 3 branches
BranchTrueFalseTotal
&& SvROK(handler) && SvTYPE(SvRV(handler)) == SVt_PVCV; 108 0 108
&& SvROK(handler) && SvTYPE(SvRV(handler)) == SVt_PVCV; 105 3 108
&& SvROK(handler) && SvTYPE(SvRV(handler)) == SVt_PVCV; 105 0 105
658  
659 /* before the swap: a die from $SIG{__WARN__} would leak *old */
660 108 ▶ if (has_handler_arg && NULL != handler && SvOK(handler) && !is_code
Branch: 4 branches
BranchTrueFalseTotal
if (has_handler_arg && NULL != handler && SvOK(handler) && !is_code 108 0 108
if (has_handler_arg && NULL != handler && SvOK(handler) && !is_code 108 0 108
if (has_handler_arg && NULL != handler && SvOK(handler) && !is_code 108 0 108
if (has_handler_arg && NULL != handler && SvOK(handler) && !is_code 3 105 108
661 3 ▶ && ckWARN(WARN_MISC)) {
Branch: 1 branch
BranchTrueFalseTotal
&& ckWARN(WARN_MISC)) { 2 1 3
662 2 Perl_warner(aTHX_ packWARN(WARN_MISC),
663 "EV::Redis: handler is not a code reference, cleared");
664 }
665  
666 107 *old = *handler_ptr;
667 /* a copy: handler may be the caller's variable, reassigned later */
668 107 ▶ *handler_ptr = is_code ? newSVsv(handler) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
*handler_ptr = is_code ? newSVsv(handler) : NULL; 105 2 107
669  
670 107 return (NULL != *handler_ptr)
671 105 ? SvREFCNT_inc(*handler_ptr)
672 212 ▶ : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
: &PL_sv_undef; 105 2 107
673 }
674  
675 /* Once the loop is destroyed nothing may touch it: adapters free themselves
676 * without stopping watchers, and loop-bound methods croak. */
677 492 static void detach_dead_loop(EV__Redis self) {
678 ngx_queue_t* q;
679 492 ▶ if (NULL == self->loop || loop_alive(self)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self->loop || loop_alive(self)) return; 492 0 492
if (NULL == self->loop || loop_alive(self)) return; 492 0 492
680 0 ▶ if (NULL != self->ac && NULL != self->ac->ev.data) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != self->ac && NULL != self->ac->ev.data) { 0 0 0
if (NULL != self->ac && NULL != self->ac->ev.data) { 0 0 0
681 0 ((redisLibevEvents*)self->ac->ev.data)->loop = NULL;
682 }
683 0 for (q = ngx_queue_head(&self->drain_queue);
684 0 ▶ q != ngx_queue_sentinel(&self->drain_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->drain_queue); 0 0 0
685 0 q = ngx_queue_next(q)) {
686 0 redisAsyncContext* dc = ngx_queue_data(q, ev_redis_drain_t, queue)->ac;
687 0 ▶ if (NULL != dc->ev.data) ((redisLibevEvents*)dc->ev.data)->loop = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != dc->ev.data) ((redisLibevEvents*)dc->ev.data)->loop = NULL; 0 0 0
688 }
689 0 self->loop = NULL;
690 }
691  
692 /* A method's invocant (typemap T_EVREDIS): a live object made by new */
693 426 static EV__Redis ev_redis_from_sv(SV* sv) {
694 SV* inner;
695 EV__Redis self;
696 426 ▶ if (!SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) { 0 426 426
697 0 croak_caller("not an EV::Redis object");
698 }
699 426 inner = SvRV(sv);
700 426 ▶ if (!SvOBJECT(inner) || (SvSTASH(inner) != ev_redis_stash
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT(inner) || (SvSTASH(inner) != ev_redis_stash 426 0 426
if (!SvOBJECT(inner) || (SvSTASH(inner) != ev_redis_stash 1 425 426
701 1 ▶ && !sv_derived_from(sv, "EV::Redis"))) {
Branch: 1 branch
BranchTrueFalseTotal
&& !sv_derived_from(sv, "EV::Redis"))) { 0 1 1
702 0 croak_caller("not an EV::Redis object");
703 }
704 426 ▶ if (SvTYPE(inner) >= SVt_PVAV || !SvOK(inner) || 0 == SvIV(inner)) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTYPE(inner) >= SVt_PVAV || !SvOK(inner) || 0 == SvIV(inner)) { 425 1 426
if (SvTYPE(inner) >= SVt_PVAV || !SvOK(inner) || 0 == SvIV(inner)) { 425 0 425
if (SvTYPE(inner) >= SVt_PVAV || !SvOK(inner) || 0 == SvIV(inner)) { 1 424 425
705 2 croak_caller("EV::Redis object is destroyed or was not made by new()");
706 }
707 424 ▶ if (!ev_redis_owns_sv(inner)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ev_redis_owns_sv(inner)) { 4 420 424
708 4 croak_caller("EV::Redis object is destroyed or was not made by new()");
709 }
710 420 self = INT2PTR(EV__Redis, SvIV(inner));
711 420 detach_dead_loop(self);
712 /* Perl code re-entered mid-XSUB (warn hooks, magic) may drop the last
713 * reference; hold the referent until the XSUB returns. */
714 420 sv_2mortal(SvREFCNT_inc_simple_NN(SvRV(sv)));
715 420 return self;
716 }
717  
718 81 static void call_void_handler(SV* handler, const char* what) {
719 81 dSP;
720 81 ▶ if (NULL == handler) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == handler) return; 42 39 81
721 39 SvREFCNT_inc_simple_void_NN(handler);
722 39 ENTER;
723 39 SAVETMPS;
724 39 save_scalar(PL_errgv);
725 39 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 39 39
726 39 PUTBACK;
727 39 call_sv(handler, G_DISCARD | G_EVAL);
728 39 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 39 0 39
if (SvTRUE(ERRSV)) 0 39 39
729 0 ▶ warn_exception(what, ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
warn_exception(what, ERRSV); 0 0 0
730 39 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 39 39
731 39 LEAVE;
732 39 SvREFCNT_dec(handler);
733 }
734  
735 /* a context freed with data == NULL frees its cbts without unlinking them */
736 1 static void unlink_cbts_of(EV__Redis self, const redisAsyncContext* ac) {
737 ngx_queue_t* q;
738 ngx_queue_t* next;
739 1 for (q = ngx_queue_head(&self->cb_queue);
740 2 ▶ q != ngx_queue_sentinel(&self->cb_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->cb_queue); 1 1 2
741 1 q = next) {
742 1 next = ngx_queue_next(q);
743 1 ▶ if (ngx_queue_data(q, ev_redis_cb_t, queue)->ac == ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_data(q, ev_redis_cb_t, queue)->ac == ac) { 1 0 1
744 1 ngx_queue_remove(q);
745 1 ngx_queue_init(q);
746 }
747 }
748 1 }
749  
750 /* disconnect() replaced ac: its replies still arrive, but must not hold
751 * max_pending slots of the next connection */
752 2 static void detach_pending_of(EV__Redis self, const redisAsyncContext* ac) {
753 ngx_queue_t* q;
754 2 for (q = ngx_queue_head(&self->cb_queue);
755 4 ▶ q != ngx_queue_sentinel(&self->cb_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->cb_queue); 2 2 4
756 2 q = ngx_queue_next(q)) {
757 2 ev_redis_cb_t* cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
758 2 ▶ if (cbt->ac != ac || cbt->persist || cbt->skipped || cbt->detached) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (cbt->ac != ac || cbt->persist || cbt->skipped || cbt->detached) continue; 2 0 2
if (cbt->ac != ac || cbt->persist || cbt->skipped || cbt->detached) continue; 2 0 2
if (cbt->ac != ac || cbt->persist || cbt->skipped || cbt->detached) continue; 2 0 2
if (cbt->ac != ac || cbt->persist || cbt->skipped || cbt->detached) continue; 0 2 2
759 2 cbt->detached = 1;
760 2 self->pending_detached++;
761 }
762 2 }
763  
764 57 static void drop_pending(EV__Redis self, const ev_redis_cb_t* cbt) {
765 57 self->pending_count--;
766 57 ▶ if (cbt->detached) self->pending_detached--;
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->detached) self->pending_detached--; 2 55 57
767 57 }
768  
769 /* a timeout sets err before hiredis fails the outstanding replies, an I/O
770 * error DISCONNECTING */
771 75 static int ac_dying(const redisAsyncContext* ac) {
772 75 ▶ return ac->c.err || (ac->c.flags & (REDIS_DISCONNECTING | REDIS_FREEING));
Branch: 2 branches
BranchTrueFalseTotal
return ac->c.err || (ac->c.flags & (REDIS_DISCONNECTING | REDIS_FREEING)); 70 5 75
return ac->c.err || (ac->c.flags & (REDIS_DISCONNECTING | REDIS_FREEING)); 0 70 70
773 }
774  
775 58 static int at_max_pending(const EV__Redis self) {
776 58 return self->max_pending > 0
777 58 ▶ && self->pending_count - self->pending_detached >= self->max_pending;
Branch: 2 branches
BranchTrueFalseTotal
&& self->pending_count - self->pending_detached >= self->max_pending; 0 58 58
&& self->pending_count - self->pending_detached >= self->max_pending; 0 0 0
778 }
779  
780 /* only_ac (NULL = all) spares cbts a draining old context still references.
781 * Collected first: callbacks may modify cb_queue. */
782 115 static void remove_cb_queue_sv(EV__Redis self, SV* error_sv,
783 const redisAsyncContext* only_ac) {
784 ngx_queue_t local_queue;
785 ngx_queue_t* q;
786 ngx_queue_t* next;
787 ev_redis_cb_t* cbt;
788  
789 115 ▶ if (self->in_cb_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_cb_cleanup) { 0 115 115
790 0 return;
791 }
792  
793 115 self->in_cb_cleanup = 1;
794  
795 115 ngx_queue_init(&local_queue);
796 115 for (q = ngx_queue_head(&self->cb_queue);
797 116 ▶ q != ngx_queue_sentinel(&self->cb_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->cb_queue); 1 115 116
798 1 q = next) {
799 1 next = ngx_queue_next(q);
800 1 cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
801  
802 1 ▶ if (cbt->running) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->running) continue; 0 1 1
803 1 ▶ if (NULL != only_ac && cbt->ac != only_ac) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != only_ac && cbt->ac != only_ac) continue; 1 0 1
if (NULL != only_ac && cbt->ac != only_ac) continue; 0 1 1
804  
805 1 ngx_queue_remove(q);
806 1 ngx_queue_insert_tail(&local_queue, q);
807 1 ▶ if (!cbt->persist) drop_pending(self, cbt);
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) drop_pending(self, cbt); 0 1 1
808 }
809  
810 116 ▶ while (!ngx_queue_empty(&local_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&local_queue)) { 1 115 116
811 1 q = ngx_queue_head(&local_queue);
812 1 cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
813 1 ngx_queue_remove(q);
814  
815 1 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 1 0 1
816 1 ▶ if (NULL != error_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != error_sv) { 1 0 1
817 1 invoke_callback_error(cbt->cb, error_sv);
818 }
819 1 SvREFCNT_dec(cbt->cb);
820 }
821 1 free_cb_entry(cbt);
822 }
823  
824 115 self->in_cb_cleanup = 0;
825 }
826  
827 17 static void free_wait_entry(ev_redis_wait_t* wt) {
828 int i;
829 48 ▶ for (i = 0; i < wt->argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < wt->argc; i++) { 31 17 48
830 31 Safefree(wt->argv[i]);
831 }
832 17 Safefree(wt->argv);
833 17 Safefree(wt->argvlen);
834 17 ▶ if (NULL != wt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != wt->cb) { 8 9 17
835 8 SvREFCNT_dec(wt->cb);
836 }
837 17 txn_unref(wt->txn);
838 17 Safefree(wt);
839 17 }
840  
841 /* Moves the waiting commands to list; call it before any callback or handler
842 * runs, so what those queue meanwhile stays queued, with a timer of its own. */
843 169 static void take_wait_queue(EV__Redis self, ngx_queue_t* list) {
844 ngx_queue_t* q;
845 180 ▶ while (self->waiting_count) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->waiting_count) { 11 169 180
846 11 q = oldest_waiting(self);
847 11 ngx_queue_remove(q);
848 11 ngx_queue_insert_tail(list, q);
849 11 self->waiting_count--;
850 }
851 169 stop_waiting_timer(self);
852 169 }
853  
854 /* Shared by setup and backlog: only transactions with submitted commands fail. */
855 3 static void take_txn_wait_queue(EV__Redis self, ngx_queue_t* list) {
856 ngx_queue_t* lists[3];
857 ngx_queue_t* q;
858 ngx_queue_t* next;
859 ev_redis_wait_t* wt;
860 int i;
861 3 lists[0] = &self->wait_queue;
862 3 lists[1] = &self->setup_queue;
863 3 lists[2] = &self->resume_setup_queue;
864 12 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 9 3 12
865 9 for (q = ngx_queue_head(lists[i]);
866 12 ▶ q != ngx_queue_sentinel(lists[i]);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(lists[i]); 3 9 12
867 3 q = next) {
868 3 next = ngx_queue_next(q);
869 3 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
870 3 ▶ if (NULL != wt->txn && wt->txn->started) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != wt->txn && wt->txn->started) { 0 3 3
if (NULL != wt->txn && wt->txn->started) { 0 0 0
871 0 ngx_queue_remove(q);
872 0 ngx_queue_insert_tail(list, q);
873 0 self->waiting_count--;
874 }
875 }
876 }
877 3 ▶ if (!self->waiting_count) stop_waiting_timer(self);
Branch: 1 branch
BranchTrueFalseTotal
if (!self->waiting_count) stop_waiting_timer(self); 0 3 3
878 3 }
879  
880 /* A retained, open transaction still owns subsequent issued commands. */
881 253 static void rederive_span(EV__Redis self) {
882 ngx_queue_t* lists[3];
883 ngx_queue_t* q;
884 int i;
885 253 txn_unref(self->issued_txn);
886 253 self->issued_txn = NULL;
887 253 lists[0] = &self->wait_queue;
888 253 lists[1] = &self->setup_queue;
889 253 lists[2] = &self->resume_setup_queue;
890 1012 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 759 253 1012
891 759 for (q = ngx_queue_head(lists[i]);
892 812 ▶ q != ngx_queue_sentinel(lists[i]);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(lists[i]); 53 759 812
893 53 q = ngx_queue_next(q)) {
894 53 ev_redis_txn_t* txn = ngx_queue_data(q, ev_redis_wait_t, queue)->txn;
895 53 ▶ if (NULL != txn && !txn->closed) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != txn && !txn->closed) { 0 53 53
if (NULL != txn && !txn->closed) { 0 0 0
896 0 self->issued_txn = txn_ref(txn);
897 0 return;
898 }
899 }
900 }
901 }
902  
903 /* in_wait_cleanup makes skip_waiting and skip_pending from these callbacks
904 * leave the rest to this batch */
905 224 static void fail_wait_list(EV__Redis self, ngx_queue_t* list, SV* error_sv) {
906 ngx_queue_t* q;
907 ev_redis_wait_t* wt;
908 224 int nested = self->in_wait_cleanup;
909  
910 224 self->in_wait_cleanup = 1;
911 235 ▶ while (!ngx_queue_empty(list)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(list)) { 11 224 235
912 11 q = ngx_queue_head(list);
913 11 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
914 11 ngx_queue_remove(q);
915 11 ▶ if (NULL != wt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != wt->cb) { 8 3 11
916 8 invoke_callback_error(wt->cb, error_sv);
917 }
918 11 free_wait_entry(wt);
919 }
920 224 self->in_wait_cleanup = nested;
921 224 }
922  
923 94 static void clear_wait_queue_sv(EV__Redis self, SV* error_sv) {
924 ngx_queue_t list;
925 94 ngx_queue_init(&list);
926 94 take_wait_queue(self, &list);
927 94 rederive_span(self);
928 94 fail_wait_list(self, &list, error_sv);
929 94 }
930  
931 /* redisFree() hook of a tracked context: hiredis hands back its own node */
932 28 static void drain_node_free(void* privdata) {
933 28 ev_redis_drain_t* dn = (ev_redis_drain_t*)privdata;
934 28 ngx_queue_remove(&dn->queue);
935 28 Safefree(dn);
936 28 }
937  
938 /* A context hiredis may tear down after self->ac is nulled (deferred
939 * disconnect, failed connect); it still holds data == self and cbts. */
940 28 static void track_draining(EV__Redis self, redisAsyncContext* ac) {
941 ev_redis_drain_t* dn;
942 /* a handler run by the teardown may call disconnect() again */
943 28 ▶ if (NULL != ac->c.privdata) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != ac->c.privdata) return; 0 28 28
944 28 Newx(dn, 1, ev_redis_drain_t);
945 28 dn->ac = ac;
946 28 dn->connection_gen = self->connection_gen;
947 28 ngx_queue_insert_tail(&self->drain_queue, &dn->queue);
948 28 ac->c.privdata = dn;
949 28 ac->c.free_privdata = drain_node_free;
950 }
951  
952 53 static void untrack_draining(redisAsyncContext* ac) {
953 53 ev_redis_drain_t* dn = (ev_redis_drain_t*)ac->c.privdata;
954 53 ▶ if (NULL == dn) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == dn) return; 35 18 53
955 18 ac->c.privdata = NULL;
956 18 ac->c.free_privdata = NULL;
957 18 drain_node_free(dn);
958 }
959  
960 static void pre_connect_common(EV__Redis self, redisOptions* opts);
961 static int post_connect_setup(EV__Redis self, const char* err_prefix);
962 static void do_reconnect(EV__Redis self);
963 static void send_next_waiting(EV__Redis self);
964 static void schedule_waiting_timer(EV__Redis self);
965 static void expire_waiting_commands(EV__Redis self);
966 static void schedule_reconnect(EV__Redis self);
967 static void EV__redis_connect_cb(redisAsyncContext* c, int status);
968 static void EV__redis_disconnect_cb(const redisAsyncContext* c, int status);
969 static void EV__redis_push_cb(redisAsyncContext* ac, void* reply_ptr);
970 static SV* EV__redis_decode_reply(redisReply* reply, int* truncated);
971 /* bounds C-stack recursion on deeply nested replies from a hostile server */
972 #define EV_REDIS_MAX_REPLY_DEPTH 512
973 static SV* decode_reply_depth(redisReply* reply, int depth, int* truncated);
974  
975 55 static void clear_connection_params(EV__Redis self) {
976 55 ▶ if (NULL != self->host) { Safefree(self->host); self->host = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->host) { Safefree(self->host); self->host = NULL; } 2 53 55
977 55 ▶ if (NULL != self->path) { Safefree(self->path); self->path = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->path) { Safefree(self->path); self->path = NULL; } 0 55 55
978 55 }
979  
980 /* C-entry callbacks need their own ENTER/SAVETMPS, or mortals pile up until EV::run returns */
981  
982 75 static void reconnect_timer_cb(EV_P_ ev_timer* w, int revents) {
983 75 EV__Redis self = (EV__Redis)w->data;
984  
985 (void)loop;
986 (void)revents;
987  
988 75 ▶ if (NULL == self || self->magic != EV_REDIS_MAGIC) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 75 0 75
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 0 75 75
989  
990 75 ENTER;
991 75 SAVETMPS;
992 75 self->reconnect_timer_active = 0;
993 75 self->callback_depth++;
994 75 do_reconnect(self);
995 75 self->callback_depth--;
996 75 check_destroyed(self);
997 75 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 56 19 75
998 75 LEAVE;
999 }
1000  
1001 93 static void schedule_reconnect(EV__Redis self) {
1002 ev_tstamp delay;
1003  
1004 93 ▶ if (!self->reconnect) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->reconnect) return; 6 87 93
1005 87 ▶ if (self->intentional_disconnect) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->intentional_disconnect) return; 0 87 87
1006 87 ▶ if (NULL == self->loop) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) return; 0 87 87
1007 /* a handler already connected again or scheduled this */
1008 87 ▶ if (NULL != self->ac || self->reconnect_timer_active) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != self->ac || self->reconnect_timer_active) return; 87 0 87
if (NULL != self->ac || self->reconnect_timer_active) return; 1 86 87
1009 86 ▶ if (self->max_reconnect_attempts > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (self->max_reconnect_attempts > 0 && 31 55 86
1010 31 ▶ self->reconnect_attempts >= self->max_reconnect_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
self->reconnect_attempts >= self->max_reconnect_attempts) { 7 24 31
1011 7 clear_wait_queue_sv(self, sv_2mortal(newSVpv("reconnect error: max attempts reached", 0)));
1012 7 emit_error_str(self, "reconnect error: max attempts reached");
1013 7 return;
1014 }
1015  
1016 79 self->reconnect_attempts++;
1017 79 delay = self->reconnect_delay_ms / 1000.0;
1018  
1019 79 ev_timer_init(&self->reconnect_timer, reconnect_timer_cb, delay, 0);
1020 79 self->reconnect_timer.data = (void*)self;
1021 /* failures and their handlers may have left the loop's clock stale */
1022 79 ev_now_update(self->loop);
1023 79 ev_timer_start(self->loop, &self->reconnect_timer);
1024 79 self->reconnect_timer_active = 1;
1025 }
1026  
1027 /* the next command to dispatch waits only for outstanding transaction replies */
1028 188 static int barrier_holds(EV__Redis self) {
1029 ngx_queue_t* q;
1030 ev_redis_wait_t* wt;
1031 188 ▶ if (!self->pending_multi || !self->waiting_count || NULL == self->ac
Branch: 3 branches
BranchTrueFalseTotal
if (!self->pending_multi || !self->waiting_count || NULL == self->ac 0 188 188
if (!self->pending_multi || !self->waiting_count || NULL == self->ac 0 0 0
if (!self->pending_multi || !self->waiting_count || NULL == self->ac 0 0 0
1032 188 ▶ || !(self->ac->c.flags & REDIS_CONNECTED) || ac_dying(self->ac)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
|| !(self->ac->c.flags & REDIS_CONNECTED) || ac_dying(self->ac)) return 0; 0 0 0
|| !(self->ac->c.flags & REDIS_CONNECTED) || ac_dying(self->ac)) return 0; 0 0 0
1033 0 ▶ if (!ngx_queue_empty(&self->setup_queue)) q = ngx_queue_head(&self->setup_queue);
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->setup_queue)) q = ngx_queue_head(&self->setup_queue); 0 0 0
1034 0 ▶ else if (!ngx_queue_empty(&self->resume_setup_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!ngx_queue_empty(&self->resume_setup_queue)) { 0 0 0
1035 0 q = ngx_queue_head(&self->resume_setup_queue);
1036 }
1037 0 else q = ngx_queue_head(&self->wait_queue);
1038 0 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
1039 0 return needs_transaction_reply(wt->persist, wt->argv[0]);
1040 }
1041  
1042 /* Time held for transaction replies counts toward no waiting deadline: it is
1043 * added to every queued_at once the hold ends. Returns 1 while it holds, 2
1044 * when a hold has just ended; touches no timer. */
1045 188 static int barrier_update(EV__Redis self) {
1046 ngx_queue_t* lists[3];
1047 ngx_queue_t* q;
1048 ev_tstamp now;
1049 int i;
1050  
1051 188 ▶ if (NULL == self->loop) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) return 0; 0 188 188
1052 188 ▶ if (barrier_holds(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (barrier_holds(self)) { 0 188 188
1053 /* ev_now is stale outside ev_run; the end-of-hold math needs the truth */
1054 0 ▶ if (0 == self->barrier_since) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == self->barrier_since) { 0 0 0
1055 0 ev_now_update(self->loop);
1056 0 self->barrier_since = ev_now(self->loop);
1057 }
1058 0 return 1;
1059 }
1060 188 ▶ if (0 == self->barrier_since) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (0 == self->barrier_since) return 0; 188 0 188
1061 0 ev_now_update(self->loop);
1062 0 now = ev_now(self->loop);
1063 0 lists[0] = &self->wait_queue;
1064 0 lists[1] = &self->setup_queue;
1065 0 lists[2] = &self->resume_setup_queue;
1066 0 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 0 0 0
1067 0 ▶ for (q = ngx_queue_head(lists[i]); q != ngx_queue_sentinel(lists[i]); q = ngx_queue_next(q)) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = ngx_queue_head(lists[i]); q != ngx_queue_sentinel(lists[i]); q = ngx_queue_next(q)) { 0 0 0
1068 0 ev_redis_wait_t* wt = ngx_queue_data(q, ev_redis_wait_t, queue);
1069 0 wt->queued_at += now - (wt->queued_at > self->barrier_since
1070 0 ▶ ? wt->queued_at : self->barrier_since);
Branch: 1 branch
BranchTrueFalseTotal
? wt->queued_at : self->barrier_since); 0 0 0
1071 }
1072 }
1073 0 self->barrier_since = 0;
1074 0 return 2;
1075 }
1076  
1077 /* the waiting timer is not armed during a hold: re-arm it once one ends */
1078 188 static int sync_barrier(EV__Redis self) {
1079 188 int state = barrier_update(self);
1080 188 ▶ if (2 == state) schedule_waiting_timer(self);
Branch: 1 branch
BranchTrueFalseTotal
if (2 == state) schedule_waiting_timer(self); 0 188 188
1081 188 return 1 == state;
1082 }
1083  
1084 /* Head refetch each iteration: callbacks may modify the queues. */
1085 0 static void expire_waiting_commands(EV__Redis self) {
1086 ngx_queue_t* q;
1087 ev_redis_wait_t* wt;
1088 ev_tstamp now;
1089 ev_tstamp timeout;
1090  
1091 0 int nested = self->in_wait_cleanup;
1092  
1093 0 ▶ if (sync_barrier(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (sync_barrier(self)) return; 0 0 0
1094 0 now = ev_now(self->loop);
1095 /* snapshot: callbacks may change waiting_timeout_ms mid-batch */
1096 0 timeout = self->waiting_timeout_ms / 1000.0;
1097  
1098 /* like fail_wait_list: skip_waiting and the waiting half of skip_pending
1099 * from these callbacks leave the rest to this batch */
1100 0 self->in_wait_cleanup = 1;
1101 0 ▶ while (self->waiting_count) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->waiting_count) { 0 0 0
1102 0 q = oldest_waiting(self);
1103 0 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
1104  
1105 0 ▶ if (now - wt->queued_at >= timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (now - wt->queued_at >= timeout) { 0 0 0
1106 0 ngx_queue_remove(q);
1107 0 self->waiting_count--;
1108  
1109 0 ▶ if (NULL != wt->txn) rederive_span(self);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != wt->txn) rederive_span(self); 0 0 0
1110 0 ▶ if (NULL != wt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != wt->cb) { 0 0 0
1111 0 invoke_callback_error(wt->cb, err_waiting_timeout);
1112 }
1113  
1114 0 free_wait_entry(wt);
1115 }
1116 else {
1117 /* FIFO with monotonic queued_at: nothing later has expired */
1118 0 break;
1119 }
1120 }
1121 0 self->in_wait_cleanup = nested;
1122 }
1123  
1124 0 static void waiting_timer_cb(EV_P_ ev_timer* w, int revents) {
1125 0 EV__Redis self = (EV__Redis)w->data;
1126  
1127 (void)loop;
1128 (void)revents;
1129  
1130 0 ▶ if (NULL == self || self->magic != EV_REDIS_MAGIC) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 0 0 0
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 0 0 0
1131  
1132 0 ENTER;
1133 0 SAVETMPS;
1134 0 self->waiting_timer_active = 0;
1135 0 self->callback_depth++;
1136 0 expire_waiting_commands(self);
1137 /* connect_cb sends the backlog once on_connect has run */
1138 0 ▶ if (self->magic == EV_REDIS_MAGIC && NULL != self->ac
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic == EV_REDIS_MAGIC && NULL != self->ac 0 0 0
if (self->magic == EV_REDIS_MAGIC && NULL != self->ac 0 0 0
1139 0 ▶ && (self->ac->c.flags & REDIS_CONNECTED)) send_next_waiting(self);
Branch: 1 branch
BranchTrueFalseTotal
&& (self->ac->c.flags & REDIS_CONNECTED)) send_next_waiting(self); 0 0 0
1140 0 schedule_waiting_timer(self);
1141 0 self->callback_depth--;
1142 0 check_destroyed(self);
1143 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1144 0 LEAVE;
1145 }
1146  
1147 17 static void schedule_waiting_timer(EV__Redis self) {
1148 ngx_queue_t* q;
1149 ev_redis_wait_t* wt;
1150 ev_tstamp now, expires_at, delay;
1151  
1152 17 stop_waiting_timer(self);
1153  
1154 17 ▶ if (NULL == self->loop) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) return; 0 17 17
1155 17 ▶ if (self->waiting_timeout_ms <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timeout_ms <= 0) return; 17 0 17
1156 0 ▶ if (!self->waiting_count) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->waiting_count) return; 0 0 0
1157 0 ▶ if (1 == barrier_update(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (1 == barrier_update(self)) return; 0 0 0
1158  
1159 0 q = oldest_waiting(self);
1160 0 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
1161  
1162 0 now = ev_now(self->loop);
1163 0 expires_at = wt->queued_at + self->waiting_timeout_ms / 1000.0;
1164 0 delay = expires_at - now;
1165 0 ▶ if (delay < 0) delay = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay < 0) delay = 0; 0 0 0
1166  
1167 0 ev_timer_init(&self->waiting_timer, waiting_timer_cb, delay, 0);
1168 0 self->waiting_timer.data = (void*)self;
1169 0 ev_timer_start(self->loop, &self->waiting_timer);
1170 0 self->waiting_timer_active = 1;
1171 }
1172  
1173 75 static void do_reconnect(EV__Redis self) {
1174 redisOptions opts;
1175 75 memset(&opts, 0, sizeof(opts));
1176  
1177 75 ▶ if (NULL == self->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) { 0 75 75
1178 56 return;
1179 }
1180  
1181 75 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 75 75
1182 0 return;
1183 }
1184  
1185 75 self->intentional_disconnect = 0;
1186 75 self->monitoring = 0;
1187 75 pre_connect_common(self, &opts);
1188  
1189 75 ▶ if (NULL != self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->path) { 54 21 75
1190 54 REDIS_OPTIONS_SET_UNIX(&opts, self->path);
1191 }
1192 21 ▶ else if (NULL != self->host) {
Branch: 1 branch
BranchTrueFalseTotal
else if (NULL != self->host) { 21 0 21
1193 21 REDIS_OPTIONS_SET_TCP(&opts, self->host, self->port);
1194 }
1195 else {
1196 0 emit_error_str(self, "reconnect error: no connection parameters");
1197 0 return;
1198 }
1199  
1200 75 self->ac = redisAsyncConnectWithOptions(&opts);
1201 75 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 0 75 75
1202 0 emit_error_str(self, "reconnect error: cannot allocate memory");
1203 0 schedule_reconnect(self);
1204 0 return;
1205 }
1206  
1207 75 ▶ if (REDIS_OK != post_connect_setup(self, "reconnect error")) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != post_connect_setup(self, "reconnect error")) { 56 19 75
1208 56 schedule_reconnect(self);
1209 56 return;
1210 }
1211 }
1212  
1213 63 static void EV__redis_connect_cb(redisAsyncContext* c, int status) {
1214 63 EV__Redis self = (EV__Redis)c->data;
1215 ngx_queue_t doomed;
1216 int owned;
1217  
1218 63 ▶ if (NULL == self || self->magic != EV_REDIS_MAGIC) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 63 0 63
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 0 63 63
1219  
1220 /* Not owned: disconnect() already tracked c; re-tracking would strand a
1221 * node, and touching self would clobber a replacement connection. */
1222 63 owned = (self->ac == c);
1223  
1224 63 ENTER;
1225 63 SAVETMPS;
1226 63 self->callback_depth++;
1227  
1228 63 ▶ if (REDIS_OK != status) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != status) { 11 52 63
1229 11 ▶ if (owned) {
Branch: 1 branch
BranchTrueFalseTotal
if (owned) { 11 0 11
1230 /* hiredis fires c's pending replies and frees it after we return */
1231 11 track_draining(self, c);
1232 11 self->ac = NULL;
1233 11 self->monitoring = 0;
1234 11 ngx_queue_init(&doomed);
1235 11 ▶ if (!self->reconnect || !self->resume_waiting_on_reconnect
Branch: 2 branches
BranchTrueFalseTotal
if (!self->reconnect || !self->resume_waiting_on_reconnect 7 4 11
if (!self->reconnect || !self->resume_waiting_on_reconnect 0 7 7
1236 0 ▶ || self->intentional_disconnect) {
Branch: 1 branch
BranchTrueFalseTotal
|| self->intentional_disconnect) { 0 0 0
1237 11 take_wait_queue(self, &doomed);
1238 }
1239 11 rederive_span(self);
1240 11 emit_error_str(self, conn_errstr(c, "connect failed"));
1241 11 fail_wait_list(self, &doomed, sv_2mortal(newSVpv(
1242 conn_errstr(c, "connect failed"), 0)));
1243 11 schedule_reconnect(self);
1244 }
1245 }
1246 52 ▶ else if (owned) {
Branch: 1 branch
BranchTrueFalseTotal
else if (owned) { 52 0 52
1247 52 self->reconnect_attempts = 0;
1248 52 demote_setup_waiters(self);
1249  
1250 52 ▶ if (NULL != self->connect_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->connect_handler) { 29 23 52
1251 29 int prev = self->in_connect_handler;
1252 29 ev_redis_txn_t* backlog_txn = self->issued_txn;
1253 29 self->issued_txn = NULL;
1254 29 self->in_connect_handler = 1;
1255 29 call_void_handler(self->connect_handler, "connect handler");
1256 29 self->in_connect_handler = prev;
1257 /* Fresh setup precedes, and is independent of, the retained backlog. */
1258 29 ▶ if (NULL == self->issued_txn && self->magic == EV_REDIS_MAGIC && self->ac == c) {
Branch: 3 branches
BranchTrueFalseTotal
if (NULL == self->issued_txn && self->magic == EV_REDIS_MAGIC && self->ac == c) { 29 0 29
if (NULL == self->issued_txn && self->magic == EV_REDIS_MAGIC && self->ac == c) { 29 0 29
if (NULL == self->issued_txn && self->magic == EV_REDIS_MAGIC && self->ac == c) { 29 0 29
1259 29 rederive_span(self);
1260 }
1261 29 txn_unref(backlog_txn);
1262 }
1263  
1264 52 send_next_waiting(self);
1265 }
1266  
1267 63 redisLibevResumeParked(c);
1268 63 self->callback_depth--;
1269 63 check_destroyed(self);
1270 63 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 11 52 63
1271 63 LEAVE;
1272 }
1273  
1274 52 static void EV__redis_disconnect_cb(const redisAsyncContext* c, int status) {
1275 52 EV__Redis self = (EV__Redis)c->data;
1276 ev_redis_drain_t* dn;
1277 UV connection_gen;
1278 SV* error_sv;
1279 ngx_queue_t doomed;
1280 52 int should_reconnect = 0;
1281 int was_intentional;
1282 int will_reconnect;
1283 int keep;
1284  
1285 52 ▶ if (NULL == self || self->magic != EV_REDIS_MAGIC) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 52 0 52
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 0 52 52
1286  
1287 52 dn = (ev_redis_drain_t*)c->c.privdata;
1288 52 ▶ connection_gen = NULL != dn ? dn->connection_gen : self->connection_gen;
Branch: 1 branch
BranchTrueFalseTotal
connection_gen = NULL != dn ? dn->connection_gen : self->connection_gen; 17 35 52
1289 /* c is being freed: a DESTROY from on_disconnect must not free it again */
1290 52 untrack_draining((redisAsyncContext*)c);
1291  
1292 /* a replacement may already have failed, or been disconnected too: NULL
1293 * ac does not make an older draining context the current connection */
1294 52 ▶ if (connection_gen != self->connection_gen
Branch: 1 branch
BranchTrueFalseTotal
if (connection_gen != self->connection_gen 52 0 52
1295 52 ▶ || (self->ac != NULL && self->ac != c)) {
Branch: 2 branches
BranchTrueFalseTotal
|| (self->ac != NULL && self->ac != c)) { 50 2 52
|| (self->ac != NULL && self->ac != c)) { 0 50 50
1296 0 return;
1297 }
1298  
1299 52 was_intentional = self->intentional_disconnect;
1300 52 self->intentional_disconnect = 0;
1301  
1302 52 self->ac = NULL;
1303 52 self->monitoring = 0;
1304 52 (void)sync_barrier(self);
1305 52 ENTER;
1306 52 SAVETMPS;
1307 52 self->callback_depth++;
1308  
1309 52 ▶ if (REDIS_OK == status) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK == status) { 16 36 52
1310 16 error_sv = err_disconnected;
1311 }
1312 else {
1313 36 error_sv = sv_2mortal(newSVpv(
1314 conn_errstr(c, "disconnected"), 0));
1315 36 ▶ if (!was_intentional) {
Branch: 1 branch
BranchTrueFalseTotal
if (!was_intentional) { 35 1 36
1316 35 should_reconnect = 1;
1317 }
1318 }
1319  
1320 /* the settings as the connection is lost decide what is kept; a
1321 * transaction does not survive it, so its waiting fragments fail with it
1322 * instead of replaying orphaned */
1323 52 ngx_queue_init(&doomed);
1324 52 ▶ keep = should_reconnect && self->reconnect && self->resume_waiting_on_reconnect;
Branch: 3 branches
BranchTrueFalseTotal
keep = should_reconnect && self->reconnect && self->resume_waiting_on_reconnect; 35 17 52
keep = should_reconnect && self->reconnect && self->resume_waiting_on_reconnect; 15 20 35
keep = should_reconnect && self->reconnect && self->resume_waiting_on_reconnect; 3 12 15
1325 52 ▶ if (!keep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!keep) { 49 3 52
1326 49 take_wait_queue(self, &doomed);
1327 }
1328 else {
1329 3 take_txn_wait_queue(self, &doomed);
1330 }
1331 /* before any callback issues: it must see the span the takes left */
1332 52 rederive_span(self);
1333  
1334 52 ▶ if (REDIS_OK != status) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != status) { 36 16 52
1335 36 emit_error_str(self, conn_errstr(c, "disconnected"));
1336 }
1337 52 call_void_handler(self->disconnect_handler, "disconnect handler");
1338  
1339 /* a handler destroyed the object: only these are left to fail */
1340 52 ▶ if (self->magic != EV_REDIS_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) { 0 52 52
1341 0 fail_wait_list(self, &doomed, error_sv);
1342 0 self->callback_depth--;
1343 0 check_destroyed(self);
1344 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1345 0 LEAVE;
1346 0 return;
1347 }
1348  
1349 /* a handler tried connecting again, even if that attempt already failed:
1350 * keep cb_queue and the commands queued since */
1351 52 ▶ if (connection_gen != self->connection_gen
Branch: 1 branch
BranchTrueFalseTotal
if (connection_gen != self->connection_gen 52 0 52
1352 52 ▶ || (self->ac != NULL && self->ac != c)) {
Branch: 2 branches
BranchTrueFalseTotal
|| (self->ac != NULL && self->ac != c)) { 0 52 52
|| (self->ac != NULL && self->ac != c)) { 0 0 0
1353 0 fail_wait_list(self, &doomed, error_sv);
1354 0 self->callback_depth--;
1355 0 check_destroyed(self);
1356 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1357 0 LEAVE;
1358 0 return;
1359 }
1360  
1361 52 remove_cb_queue_sv(self, error_sv, c);
1362  
1363 52 ▶ will_reconnect = should_reconnect && !self->intentional_disconnect && self->reconnect;
Branch: 3 branches
BranchTrueFalseTotal
will_reconnect = should_reconnect && !self->intentional_disconnect && self->reconnect; 35 17 52
will_reconnect = should_reconnect && !self->intentional_disconnect && self->reconnect; 35 0 35
will_reconnect = should_reconnect && !self->intentional_disconnect && self->reconnect; 15 20 35
1364 /* a handler called off the reconnect: the kept ones go too, before any
1365 * callback runs, so what the callbacks queue stays */
1366 52 ▶ if (keep && !will_reconnect) {
Branch: 2 branches
BranchTrueFalseTotal
if (keep && !will_reconnect) { 3 49 52
if (keep && !will_reconnect) { 0 3 3
1367 0 take_wait_queue(self, &doomed);
1368 0 rederive_span(self);
1369 }
1370 52 fail_wait_list(self, &doomed, error_sv);
1371  
1372 52 ▶ if (will_reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (will_reconnect) { 15 37 52
1373 15 schedule_reconnect(self);
1374 }
1375  
1376 52 self->callback_depth--;
1377 52 check_destroyed(self);
1378 52 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 36 16 52
1379 52 LEAVE;
1380 }
1381  
1382 2 static void EV__redis_push_cb(redisAsyncContext* ac, void* reply_ptr) {
1383 2 EV__Redis self = (EV__Redis)ac->data;
1384 2 redisReply* reply = (redisReply*)reply_ptr;
1385  
1386 2 ▶ if (NULL == self || self->magic != EV_REDIS_MAGIC) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 2 0 2
if (NULL == self || self->magic != EV_REDIS_MAGIC) return; 0 2 2
1387 2 SV* handler = self->push_handler;
1388 2 ▶ if (NULL == handler || NULL == reply) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == handler || NULL == reply) return; 2 0 2
if (NULL == handler || NULL == reply) return; 0 2 2
1389 2 SvREFCNT_inc_simple_void_NN(handler);
1390  
1391 2 self->callback_depth++;
1392  
1393 {
1394 2 dSP;
1395  
1396 2 ENTER;
1397 2 SAVETMPS;
1398 2 save_scalar(PL_errgv);
1399  
1400 2 int truncated = 0;
1401 2 SV* decoded = sv_2mortal(EV__redis_decode_reply(reply, &truncated));
1402  
1403 2 ▶ if (truncated) {
Branch: 1 branch
BranchTrueFalseTotal
if (truncated) { 1 1 2
1404 /* on_push takes the message only, so a dropped one errors here */
1405 1 emit_error_str(self, "reply exceeds maximum nesting depth");
1406 }
1407 else {
1408 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
1409 1 ▶ XPUSHs(decoded);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(decoded); 0 1 1
1410 1 PUTBACK;
1411  
1412 1 call_sv(handler, G_DISCARD | G_EVAL);
1413 1 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 1 0 1
if (SvTRUE(ERRSV)) { 0 1 1
1414 0 ▶ warn_exception("push handler", ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
warn_exception("push handler", ERRSV); 0 0 0
1415 }
1416 }
1417  
1418 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
1419 2 LEAVE;
1420 }
1421  
1422 2 SvREFCNT_dec(handler);
1423 2 redisLibevResumeParked(ac);
1424 2 self->callback_depth--;
1425 2 check_destroyed(self);
1426 }
1427  
1428 130 static void pre_connect_common(EV__Redis self, redisOptions* opts) {
1429 130 self->connection_gen++;
1430 130 ▶ if (NULL != self->connect_timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->connect_timeout) { 5 125 130
1431 5 opts->connect_timeout = self->connect_timeout;
1432 }
1433 130 ▶ if (NULL != self->command_timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->command_timeout) { 16 114 130
1434 16 opts->command_timeout = self->command_timeout;
1435 }
1436 130 ▶ if (self->prefer_ipv4) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->prefer_ipv4) { 1 129 130
1437 1 opts->options |= REDIS_OPT_PREFER_IPV4;
1438 }
1439 129 ▶ else if (self->prefer_ipv6) {
Branch: 1 branch
BranchTrueFalseTotal
else if (self->prefer_ipv6) { 1 128 129
1440 1 opts->options |= REDIS_OPT_PREFER_IPV6;
1441 }
1442 /* not REDIS_OPT_SET_SOCK_CLOEXEC: its c->flags bit is REDIS_REUSEADDR, so
1443 * each would turn on the other; post_connect_setup sets FD_CLOEXEC */
1444 130 ▶ if (self->reuseaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reuseaddr) { 0 130 130
1445 0 opts->options |= REDIS_OPT_REUSEADDR;
1446 }
1447 130 ▶ if (NULL != self->source_addr && NULL == self->path) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != self->source_addr && NULL == self->path) { 3 127 130
if (NULL != self->source_addr && NULL == self->path) { 3 0 3
1448 3 opts->endpoint.tcp.source_addr = self->source_addr;
1449 }
1450 130 }
1451  
1452 67 static int setup_failed(EV__Redis self, SV* err) {
1453 /* own copy: on_error's @_ aliases err, so the handler can change it */
1454 67 SV* wait_err = sv_2mortal(newSVsv(err));
1455 ngx_queue_t doomed;
1456 67 redisAsyncFree(self->ac);
1457 67 self->ac = NULL;
1458 67 ngx_queue_init(&doomed);
1459 67 ▶ if (!self->reconnect || !self->resume_waiting_on_reconnect
Branch: 2 branches
BranchTrueFalseTotal
if (!self->reconnect || !self->resume_waiting_on_reconnect 65 2 67
if (!self->reconnect || !self->resume_waiting_on_reconnect 52 13 65
1460 52 ▶ || self->intentional_disconnect) {
Branch: 1 branch
BranchTrueFalseTotal
|| self->intentional_disconnect) { 0 52 52
1461 15 take_wait_queue(self, &doomed);
1462 }
1463 67 rederive_span(self);
1464 67 emit_error(self, err);
1465 67 fail_wait_list(self, &doomed, wait_err);
1466 67 return REDIS_ERR;
1467 }
1468  
1469 /* On failure: frees and nulls self->ac, then emits the error. */
1470 130 static int post_connect_setup(EV__Redis self, const char* err_prefix) {
1471 130 self->ac->data = (void*)self;
1472 130 self->in_multi = 0;
1473 130 self->pending_multi = 0;
1474 130 (void)sync_barrier(self);
1475 130 ▶ if (self->ac->c.fd != REDIS_INVALID_FD) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ac->c.fd != REDIS_INVALID_FD) { 66 64 130
1476 66 fcntl(self->ac->c.fd, F_SETFD, self->cloexec ? FD_CLOEXEC : 0);
1477 }
1478  
1479 /* failed inside the connect call: fd may be -1, never hand it to libev */
1480 130 ▶ if (self->ac->err) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ac->err) { 67 63 130
1481 67 return setup_failed(self, sv_2mortal(newSVpvf("%s: %s",
1482 err_prefix, self->ac->errstr)));
1483 }
1484  
1485 #ifdef EV_REDIS_SSL
1486 63 ▶ if (NULL != self->ssl_ctx
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ssl_ctx 1 62 63
1487 1 ▶ && REDIS_OK != redisInitiateSSLWithContext(&self->ac->c, self->ssl_ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
&& REDIS_OK != redisInitiateSSLWithContext(&self->ac->c, self->ssl_ctx)) { 0 1 1
1488 0 ▶ return setup_failed(self, sv_2mortal(newSVpvf("%s: SSL initiation failed: %s",
Branch: 1 branch
BranchTrueFalseTotal
return setup_failed(self, sv_2mortal(newSVpvf("%s: SSL initiation failed: %s", 0 0 0
1489 err_prefix, self->ac->errstr[0] ? self->ac->errstr : "unknown error")));
1490 }
1491 #endif
1492  
1493 /* on failure it sets c.err, which breaks the first write */
1494 63 ▶ if (self->keepalive > 0 && self->ac->c.connection_type == REDIS_CONN_TCP
Branch: 2 branches
BranchTrueFalseTotal
if (self->keepalive > 0 && self->ac->c.connection_type == REDIS_CONN_TCP 0 63 63
if (self->keepalive > 0 && self->ac->c.connection_type == REDIS_CONN_TCP 0 0 0
1495 0 ▶ && REDIS_OK != redisEnableKeepAliveWithInterval(&self->ac->c, self->keepalive)) {
Branch: 1 branch
BranchTrueFalseTotal
&& REDIS_OK != redisEnableKeepAliveWithInterval(&self->ac->c, self->keepalive)) { 0 0 0
1496 0 return setup_failed(self, sv_2mortal(newSVpvf("%s: %s",
1497 err_prefix, self->ac->c.errstr)));
1498 }
1499 /* hiredis closes the fd when this fails */
1500 63 ▶ if (self->tcp_user_timeout > 0 && self->ac->c.connection_type == REDIS_CONN_TCP
Branch: 2 branches
BranchTrueFalseTotal
if (self->tcp_user_timeout > 0 && self->ac->c.connection_type == REDIS_CONN_TCP 2 61 63
if (self->tcp_user_timeout > 0 && self->ac->c.connection_type == REDIS_CONN_TCP 1 1 2
1501 1 ▶ && REDIS_OK != redisSetTcpUserTimeout(&self->ac->c, self->tcp_user_timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
&& REDIS_OK != redisSetTcpUserTimeout(&self->ac->c, self->tcp_user_timeout)) { 0 1 1
1502 0 return setup_failed(self, sv_2mortal(newSVpvf("%s: %s",
1503 err_prefix, self->ac->c.errstr)));
1504 }
1505  
1506 63 ▶ if (REDIS_OK != redisLibevAttach(self->loop, self->ac)) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != redisLibevAttach(self->loop, self->ac)) { 0 63 63
1507 0 return setup_failed(self, sv_2mortal(newSVpvf("%s: cannot attach libev",
1508 err_prefix)));
1509 }
1510 63 redisLibevUseNetWrite(&self->ac->c);
1511  
1512 63 ▶ if (self->priority != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->priority != 0) { 1 62 63
1513 1 redisLibevSetPriority(self->ac, self->priority);
1514 }
1515  
1516 63 redisAsyncSetConnectCallbackNC(self->ac, EV__redis_connect_cb);
1517 63 redisAsyncSetDisconnectCallback(self->ac, EV__redis_disconnect_cb);
1518 63 ▶ if (NULL != self->push_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->push_handler) { 0 63 63
1519 0 redisAsyncSetPushCallback(self->ac, EV__redis_push_cb);
1520 }
1521  
1522 63 return REDIS_OK;
1523 }
1524  
1525 /* on_error may drop the last reference to self or connect again */
1526 55 static void connect_setup_or_retry(EV__Redis self) {
1527 55 self->callback_depth++;
1528 55 ▶ if (REDIS_OK != post_connect_setup(self, "connect error")) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != post_connect_setup(self, "connect error")) { 11 44 55
1529 11 schedule_reconnect(self);
1530 }
1531 55 self->callback_depth--;
1532 55 check_destroyed(self);
1533 55 }
1534  
1535 575 static SV* decode_reply_depth(redisReply* reply, int depth, int* truncated) {
1536 SV* res;
1537  
1538 575 switch (reply->type) {
1539 43 case REDIS_REPLY_STRING:
1540 case REDIS_REPLY_ERROR:
1541 case REDIS_REPLY_STATUS:
1542 case REDIS_REPLY_BIGNUM:
1543 case REDIS_REPLY_VERB:
1544 43 res = newSVpvn(reply->str, reply->len);
1545 43 break;
1546  
1547 8 case REDIS_REPLY_INTEGER:
1548 #if IVSIZE < 8
1549 /* a 32-bit IV: an NV stringifies with 15 digits, so from 1e15 on
1550 * only a string keeps every digit */
1551 if (reply->integer > IV_MAX || reply->integer < IV_MIN) {
1552 if (reply->integer >= 1000000000000000LL || reply->integer <= -1000000000000000LL) {
1553 char buf[24];
1554 snprintf(buf, sizeof(buf), "%lld", reply->integer);
1555 res = newSVpv(buf, 0);
1556 }
1557 else {
1558 res = newSVnv((NV)reply->integer);
1559 }
1560 break;
1561 }
1562 #endif
1563 8 res = newSViv((IV)reply->integer);
1564 8 break;
1565  
1566 0 case REDIS_REPLY_DOUBLE:
1567 0 res = newSVnv(reply->dval);
1568 0 break;
1569  
1570 0 case REDIS_REPLY_BOOL:
1571 0 res = newSViv(reply->integer ? 1 : 0);
1572 0 break;
1573  
1574 0 case REDIS_REPLY_NIL:
1575 0 res = newSV(0);
1576 0 break;
1577  
1578 524 case REDIS_REPLY_ARRAY:
1579 case REDIS_REPLY_MAP:
1580 case REDIS_REPLY_SET:
1581 case REDIS_REPLY_ATTR:
1582 case REDIS_REPLY_PUSH: {
1583 524 AV* av = newAV();
1584 size_t i;
1585 524 ▶ if (depth >= EV_REDIS_MAX_REPLY_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= EV_REDIS_MAX_REPLY_DEPTH) { 1 523 524
1586 1 *truncated = 1;
1587 1 res = newRV_noinc((SV*)av);
1588 1 break;
1589 }
1590 523 ▶ if (reply->elements > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (reply->elements > 0) { 522 1 523
1591 522 av_extend(av, (SSize_t)(reply->elements - 1));
1592 1072 ▶ for (i = 0; i < reply->elements; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < reply->elements; i++) { 550 522 1072
1593 550 ▶ if (NULL != reply->element[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != reply->element[i]) { 550 0 550
1594 550 av_push(av, decode_reply_depth(reply->element[i], depth + 1, truncated));
1595 }
1596 else {
1597 0 av_push(av, newSV(0));
1598 }
1599 }
1600 }
1601 523 res = newRV_noinc((SV*)av);
1602 523 break;
1603 }
1604  
1605 0 default:
1606 0 res = newSV(0);
1607 0 break;
1608 }
1609  
1610 575 return res;
1611 }
1612  
1613 25 static SV* EV__redis_decode_reply(redisReply* reply, int* truncated) {
1614 25 *truncated = 0;
1615 25 return decode_reply_depth(reply, 0, truncated);
1616 }
1617  
1618 79 static void EV__redis_reply_cb_body(redisAsyncContext* c, void* reply, void* privdata) {
1619 79 EV__Redis self = (EV__Redis)c->data;
1620 ev_redis_cb_t* cbt;
1621 ev_redis_cb_t* prev_cb;
1622 SV* sv_reply;
1623 SV* sv_err;
1624  
1625 79 cbt = (ev_redis_cb_t*)privdata;
1626 79 ▶ if (cbt->persist) sub_note_reply(c, cbt, (redisReply*)reply);
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->persist) sub_note_reply(c, cbt, (redisReply*)reply); 21 58 79
1627 79 ▶ if (cbt->proto_cmd && NULL != reply) note_protocol_reply(c, (redisReply*)reply);
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->proto_cmd && NULL != reply) note_protocol_reply(c, (redisReply*)reply); 1 78 79
if (cbt->proto_cmd && NULL != reply) note_protocol_reply(c, (redisReply*)reply); 1 0 1
1628 /* a monitor stream holds no errors: the server refused MONITOR, and hiredis
1629 * must not re-queue the record for the next reply */
1630 79 ▶ if (cbt->monitor && NULL != reply && ((redisReply*)reply)->type == REDIS_REPLY_ERROR) {
Branch: 3 branches
BranchTrueFalseTotal
if (cbt->monitor && NULL != reply && ((redisReply*)reply)->type == REDIS_REPLY_ERROR) { 0 79 79
if (cbt->monitor && NULL != reply && ((redisReply*)reply)->type == REDIS_REPLY_ERROR) { 0 0 0
if (cbt->monitor && NULL != reply && ((redisReply*)reply)->type == REDIS_REPLY_ERROR) { 0 0 0
1631 0 c->c.flags &= ~REDIS_MONITORING;
1632 0 cbt->sub_count = 0;
1633 0 ▶ if (NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c) self->monitoring = 0;
Branch: 3 branches
BranchTrueFalseTotal
if (NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c) self->monitoring = 0; 0 0 0
if (NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c) self->monitoring = 0; 0 0 0
if (NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c) self->monitoring = 0; 0 0 0
1634 }
1635 79 ▶ if (cbt->multi && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c) {
Branch: 4 branches
BranchTrueFalseTotal
if (cbt->multi && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c) { 0 79 79
if (cbt->multi && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c) { 0 0 0
if (cbt->multi && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c) { 0 0 0
if (cbt->multi && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c) { 0 0 0
1636 0 redisReply* rr = (redisReply*)reply;
1637 0 ▶ if (NULL != rr) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != rr) { 0 0 0
1638 0 self->pending_multi--;
1639 0 ▶ if (!self->pending_multi) (void)sync_barrier(self);
Branch: 1 branch
BranchTrueFalseTotal
if (!self->pending_multi) (void)sync_barrier(self); 0 0 0
1640 0 ▶ if (cbt->multi == EV_REDIS_MULTI) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->multi == EV_REDIS_MULTI) { 0 0 0
1641 0 ▶ if (rr->type == REDIS_REPLY_STATUS && 0 == ascii_strcasecmp(rr->str, "ok")) {
Branch: 2 branches
BranchTrueFalseTotal
if (rr->type == REDIS_REPLY_STATUS && 0 == ascii_strcasecmp(rr->str, "ok")) { 0 0 0
if (rr->type == REDIS_REPLY_STATUS && 0 == ascii_strcasecmp(rr->str, "ok")) { 0 0 0
1642 0 self->in_multi = 1;
1643 }
1644 }
1645 /* other refusals of EXEC, DISCARD or RESET leave it open */
1646 0 ▶ else if (rr->type != REDIS_REPLY_ERROR || ends_transaction(rr)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (rr->type != REDIS_REPLY_ERROR || ends_transaction(rr)) { 0 0 0
else if (rr->type != REDIS_REPLY_ERROR || ends_transaction(rr)) { 0 0 0
1647 0 self->in_multi = 0;
1648 }
1649 }
1650 }
1651 /* A refused first WATCH creates no state; earlier successful WATCHes remain. */
1652 79 ▶ if (cbt->watch && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c
Branch: 4 branches
BranchTrueFalseTotal
if (cbt->watch && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c 0 79 79
if (cbt->watch && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c 0 0 0
if (cbt->watch && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c 0 0 0
if (cbt->watch && NULL != self && self->magic == EV_REDIS_MAGIC && self->ac == c 0 0 0
1653 0 ▶ && NULL != reply && NULL != cbt->txn) {
Branch: 2 branches
BranchTrueFalseTotal
&& NULL != reply && NULL != cbt->txn) { 0 0 0
&& NULL != reply && NULL != cbt->txn) { 0 0 0
1654 0 ▶ if (((redisReply*)reply)->type != REDIS_REPLY_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (((redisReply*)reply)->type != REDIS_REPLY_ERROR) { 0 0 0
1655 0 cbt->txn->watched = 1;
1656 }
1657 0 ▶ else if (!cbt->txn->watched && !cbt->txn->multi_started) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!cbt->txn->watched && !cbt->txn->multi_started) { 0 0 0
else if (!cbt->txn->watched && !cbt->txn->multi_started) { 0 0 0
1658 0 cbt->txn->started = 0;
1659 0 cbt->txn->closed = 1;
1660 0 ▶ if (self->issued_txn == cbt->txn) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->issued_txn == cbt->txn) { 0 0 0
1661 0 txn_unref(self->issued_txn);
1662 0 self->issued_txn = NULL;
1663 }
1664 }
1665 }
1666  
1667 79 ▶ if (cbt->skipped) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->skipped) { 0 79 79
1668 0 ▶ int resume_waiting = cbt->multi && NULL != reply && NULL != self
Branch: 2 branches
BranchTrueFalseTotal
int resume_waiting = cbt->multi && NULL != reply && NULL != self 0 0 0
int resume_waiting = cbt->multi && NULL != reply && NULL != self 0 0 0
1669 0 ▶ && self->magic == EV_REDIS_MAGIC && self->ac == c && !self->pending_multi;
Branch: 4 branches
BranchTrueFalseTotal
&& self->magic == EV_REDIS_MAGIC && self->ac == c && !self->pending_multi; 0 0 0
&& self->magic == EV_REDIS_MAGIC && self->ac == c && !self->pending_multi; 0 0 0
&& self->magic == EV_REDIS_MAGIC && self->ac == c && !self->pending_multi; 0 0 0
&& self->magic == EV_REDIS_MAGIC && self->ac == c && !self->pending_multi; 0 0 0
1670 0 ▶ if (!cbt->persist || cbt->sub_count <= 0) free_cb_entry(cbt);
Branch: 2 branches
BranchTrueFalseTotal
if (!cbt->persist || cbt->sub_count <= 0) free_cb_entry(cbt); 0 0 0
if (!cbt->persist || cbt->sub_count <= 0) free_cb_entry(cbt); 0 0 0
1671 0 ▶ if (resume_waiting) {
Branch: 1 branch
BranchTrueFalseTotal
if (resume_waiting) { 0 0 0
1672 0 self->callback_depth++;
1673 0 send_next_waiting(self);
1674 0 self->callback_depth--;
1675 0 check_destroyed(self);
1676 }
1677 0 return;
1678 }
1679  
1680 /* DESTROY detached this context */
1681 79 ▶ if (self == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self == NULL) { 0 79 79
1682 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
1683 0 invoke_callback_error(cbt->cb,
1684 sv_2mortal(newSVpv(conn_errstr(c, "disconnected"), 0)));
1685 0 SvREFCNT_dec(cbt->cb);
1686 0 cbt->cb = NULL;
1687 }
1688 0 ▶ if (!cbt->persist || cbt->sub_count <= 0) free_cb_entry(cbt);
Branch: 2 branches
BranchTrueFalseTotal
if (!cbt->persist || cbt->sub_count <= 0) free_cb_entry(cbt); 0 0 0
if (!cbt->persist || cbt->sub_count <= 0) free_cb_entry(cbt); 0 0 0
1689 0 return;
1690 }
1691  
1692 79 ▶ if (self->magic == EV_REDIS_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED) { 0 79 79
1693 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
1694 0 self->callback_depth++;
1695 0 invoke_callback_error(cbt->cb, sv_2mortal(newSVpv(conn_errstr(c, "disconnected"), 0)));
1696 0 self->callback_depth--;
1697 0 SvREFCNT_dec(cbt->cb);
1698 0 cbt->cb = NULL;
1699 }
1700 0 ▶ if (!cbt->persist || cbt->sub_count <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!cbt->persist || cbt->sub_count <= 0) { 0 0 0
if (!cbt->persist || cbt->sub_count <= 0) { 0 0 0
1701 0 ngx_queue_remove(&cbt->queue);
1702 0 free_cb_entry(cbt);
1703 }
1704 0 check_destroyed(self);
1705 0 return;
1706 }
1707  
1708 /* corrupt self: leave its queue alone */
1709 79 ▶ if (self->magic != EV_REDIS_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) { 0 79 79
1710 0 ▶ if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb); 0 0 0
1711 0 free_cb_entry(cbt);
1712 0 return;
1713 }
1714  
1715 79 prev_cb = self->current_cb;
1716 79 self->current_cb = cbt;
1717 79 cbt->running = 1;
1718 79 self->callback_depth++;
1719  
1720 79 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 79 0 79
1721 79 ▶ if (NULL == reply) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == reply) { 56 23 79
1722 56 sv_err = sv_2mortal(newSVpv(
1723 conn_errstr(c, "disconnected"), 0));
1724 56 invoke_callback_error(cbt->cb, sv_err);
1725 }
1726 else {
1727 23 dSP;
1728  
1729 23 ENTER;
1730 23 SAVETMPS;
1731 23 save_scalar(PL_errgv);
1732  
1733 23 int truncated = 0;
1734  
1735 23 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 23 23
1736 23 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 23 23
1737 23 sv_reply = sv_2mortal(EV__redis_decode_reply((redisReply*)reply, &truncated));
1738 23 ▶ if (((redisReply*)reply)->type == REDIS_REPLY_ERROR || truncated) {
Branch: 2 branches
BranchTrueFalseTotal
if (((redisReply*)reply)->type == REDIS_REPLY_ERROR || truncated) { 23 0 23
if (((redisReply*)reply)->type == REDIS_REPLY_ERROR || truncated) { 0 23 23
1739 0 PUSHs(sv_newmortal());
1740 0 ▶ PUSHs(truncated
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(truncated 0 0 0
1741 ? sv_2mortal(newSVpv("reply exceeds maximum nesting depth", 0))
1742 : sv_reply);
1743 }
1744 else {
1745 23 PUSHs(sv_reply);
1746 }
1747 23 PUTBACK;
1748  
1749 23 call_sv(cbt->cb, G_DISCARD | G_EVAL);
1750 23 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 23 0 23
if (SvTRUE(ERRSV)) { 0 23 23
1751 0 ▶ warn_exception("command callback", ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
warn_exception("command callback", ERRSV); 0 0 0
1752 }
1753  
1754 23 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 23 0 23
1755 23 LEAVE;
1756 }
1757 }
1758  
1759 79 self->callback_depth--;
1760 79 self->current_cb = prev_cb;
1761 79 cbt->running = 0;
1762  
1763 /* DESTROY ran in the callback: hiredis frees the context without repushing
1764 * this record */
1765 79 ▶ if (self->magic == EV_REDIS_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED) { 1 78 79
1766 1 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 1 0 1
1767 1 SvREFCNT_dec(cbt->cb);
1768 1 cbt->cb = NULL;
1769 }
1770 1 ▶ if (!cbt->persist || cbt->monitor || cbt->sub_count <= 0) free_cb_entry(cbt);
Branch: 3 branches
BranchTrueFalseTotal
if (!cbt->persist || cbt->monitor || cbt->sub_count <= 0) free_cb_entry(cbt); 0 1 1
if (!cbt->persist || cbt->monitor || cbt->sub_count <= 0) free_cb_entry(cbt); 0 0 0
if (!cbt->persist || cbt->monitor || cbt->sub_count <= 0) free_cb_entry(cbt); 0 0 0
1771 1 check_destroyed(self);
1772 1 return;
1773 }
1774  
1775 /* disconnect() from the callback: hiredis frees c without re-queuing the
1776 * MONITOR record, so this is its last call */
1777 78 ▶ if (cbt->monitor && NULL != reply && (c->c.flags & REDIS_FREEING)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cbt->monitor && NULL != reply && (c->c.flags & REDIS_FREEING)) { 0 78 78
if (cbt->monitor && NULL != reply && (c->c.flags & REDIS_FREEING)) { 0 0 0
if (cbt->monitor && NULL != reply && (c->c.flags & REDIS_FREEING)) { 0 0 0
1778 0 ngx_queue_remove(&cbt->queue);
1779 0 self->callback_depth++;
1780 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
1781 0 invoke_callback_error(cbt->cb, sv_2mortal(newSVpv(
1782 conn_errstr(c, "disconnected"), 0)));
1783 0 SvREFCNT_dec(cbt->cb);
1784 }
1785 0 free_cb_entry(cbt);
1786 0 self->callback_depth--;
1787 0 check_destroyed(self);
1788 0 return;
1789 }
1790  
1791 78 ▶ if (cbt->skipped) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->skipped) { 0 78 78
1792 /* skip_pending already reinitialised the node and adjusted pending_count */
1793 0 ngx_queue_remove(&cbt->queue);
1794 0 ▶ if (cbt->persist && cbt->sub_count > 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->persist && cbt->sub_count > 0) return; 0 0 0
if (cbt->persist && cbt->sub_count > 0) return; 0 0 0
1795 0 free_cb_entry(cbt);
1796 0 self->callback_depth++;
1797 0 send_next_waiting(self);
1798 0 self->callback_depth--;
1799 0 check_destroyed(self);
1800 0 return;
1801 }
1802  
1803 78 ▶ if (cbt->persist) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->persist) { 21 57 78
1804 21 ▶ if (cbt->sub_count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->sub_count <= 0) { 14 7 21
1805 14 ngx_queue_remove(&cbt->queue);
1806 14 self->callback_depth++;
1807 14 ▶ if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb); 14 0 14
1808 14 free_cb_entry(cbt);
1809 14 self->callback_depth--;
1810 14 check_destroyed(self);
1811 }
1812 21 return;
1813 }
1814  
1815 57 ▶ if (0 == cbt->persist) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == cbt->persist) { 57 0 57
1816 /* Unqueue before SvREFCNT_dec: freeing the closure may run DESTROY,
1817 * whose sweep would double-free a still-queued cbt. */
1818 57 ngx_queue_remove(&cbt->queue);
1819 57 drop_pending(self, cbt);
1820 57 self->callback_depth++;
1821 57 ▶ if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb); 57 0 57
1822 57 free_cb_entry(cbt);
1823 /* NULL reply: connection dying, disconnect_cb owns the wait queue;
1824 * unless it is an older one and a newer connection took over */
1825 57 ▶ if (reply != NULL || (NULL != self->ac && self->ac != c)) {
Branch: 3 branches
BranchTrueFalseTotal
if (reply != NULL || (NULL != self->ac && self->ac != c)) { 41 16 57
if (reply != NULL || (NULL != self->ac && self->ac != c)) { 35 6 41
if (reply != NULL || (NULL != self->ac && self->ac != c)) { 0 35 35
1826 16 send_next_waiting(self);
1827 }
1828 57 self->callback_depth--;
1829 57 check_destroyed(self);
1830 }
1831 }
1832  
1833 79 static void EV__redis_reply_cb(redisAsyncContext* c, void* reply, void* privdata) {
1834 79 ENTER;
1835 79 SAVETMPS;
1836 79 EV__redis_reply_cb_body(c, reply, privdata);
1837 79 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 56 23 79
1838 79 LEAVE;
1839 79 redisLibevResumeParked(c);
1840 79 }
1841  
1842 /* A resubscribe replaces unsubscribe_sent: retain the confirmation still due. */
1843 0 static void retain_unsubscribe(redisLibevEvents* e, int pattern,
1844 const char* name, size_t len, redisCallback* sub) {
1845 HV* counts;
1846 0 ▶ if (!sub->unsubscribe_sent) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub->unsubscribe_sent) return; 0 0 0
1847 0 counts = e->unsubs[pattern];
1848 0 ▶ if (NULL == counts) counts = e->unsubs[pattern] = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == counts) counts = e->unsubs[pattern] = newHV(); 0 0 0
1849 0 ▶ if (!hv_exists(counts, name, (I32)len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(counts, name, (I32)len)) { 0 0 0
1850 0 (void)hv_store(counts, name, (I32)len, newSViv(1), 0);
1851 }
1852 }
1853  
1854 0 static void track_unsubscribe(redisLibevEvents* e, int pattern,
1855 const char* name, size_t len, redisCallback* sub, int named) {
1856 0 HV* counts = e->unsubs[pattern];
1857 0 ▶ SV** count = NULL != counts ? hv_fetch(counts, name, (I32)len, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV** count = NULL != counts ? hv_fetch(counts, name, (I32)len, 0) : NULL; 0 0 0
1858 0 ▶ if (NULL != count) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != count) { 0 0 0
1859 0 sv_setiv(*count, SvIV(*count) + 1);
1860 0 sub->pending_subs++;
1861 }
1862 0 ▶ else if (sub->unsubscribe_sent) {
Branch: 1 branch
BranchTrueFalseTotal
else if (sub->unsubscribe_sent) { 0 0 0
1863 0 ▶ if (NULL == counts) counts = e->unsubs[pattern] = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == counts) counts = e->unsubs[pattern] = newHV(); 0 0 0
1864 0 (void)hv_store(counts, name, (I32)len, newSViv(2), 0);
1865 0 sub->pending_subs++;
1866 }
1867 0 ▶ if (named) sub->unsubscribe_sent = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (named) sub->unsubscribe_sent = 1; 0 0 0
1868 0 }
1869  
1870 /* Keep named confirmations in the dictionary even when no subscription exists.
1871 * Extra confirmations borrow pending_subs slots until their preceding reply. */
1872 0 static int prepare_unsubscribe(EV__Redis self, int pattern,
1873 int argc, const char** argv, const size_t* argvlen) {
1874 0 redisAsyncContext* ac = self->ac;
1875 0 redisLibevEvents* e = (redisLibevEvents*)ac->ev.data;
1876 0 ▶ dict* d = pattern ? ac->sub.patterns : ac->sub.channels;
Branch: 1 branch
BranchTrueFalseTotal
dict* d = pattern ? ac->sub.patterns : ac->sub.channels; 0 0 0
1877 dictEntry* he;
1878 int i;
1879  
1880 0 ▶ if (argc == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (argc == 1) { 0 0 0
1881 dictIterator it;
1882 0 dictInitIterator(&it, d);
1883 0 ▶ while (NULL != (he = dictNext(&it))) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != (he = dictNext(&it))) { 0 0 0
1884 0 redisCallback* sub = (redisCallback*)dictGetEntryVal(he);
1885 0 ▶ if (!sub->unsubscribe_sent) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sub->unsubscribe_sent) { 0 0 0
1886 0 sds key = (sds)dictGetEntryKey(he);
1887 0 track_unsubscribe(e, pattern, key, sdslen(key), sub, 0);
1888 }
1889 }
1890 0 return REDIS_OK;
1891 }
1892  
1893 0 ▶ for (i = 1; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < argc; i++) { 0 0 0
1894 0 he = sub_entry(d, argv[i], argvlen[i]);
1895 0 ▶ if (NULL == he) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == he) { 0 0 0
1896 ev_redis_cb_t* cbt;
1897 redisCallback stub;
1898 0 sds key = sdsnewlen(argv[i], argvlen[i]);
1899 0 ▶ if (NULL == key) goto oom;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == key) goto oom; 0 0 0
1900 0 Newxz(cbt, 1, ev_redis_cb_t);
1901 0 cbt->ac = ac;
1902 0 cbt->persist = 1;
1903 0 cbt->sub_count = 1;
1904 0 cbt->seq = ++self->cb_seq;
1905 0 memset(&stub, 0, sizeof(stub));
1906 0 stub.fn = EV__redis_reply_cb;
1907 0 stub.privdata = (void*)cbt;
1908 0 ▶ if (DICT_OK != dictAdd(d, key, &stub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DICT_OK != dictAdd(d, key, &stub)) { 0 0 0
1909 0 sdsfree(key);
1910 0 free_cb_entry(cbt);
1911 0 goto oom;
1912 }
1913 0 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
1914 0 he = sub_entry(d, argv[i], argvlen[i]);
1915 }
1916 0 track_unsubscribe(e, pattern, argv[i], argvlen[i],
1917 0 (redisCallback*)dictGetEntryVal(he), 1);
1918 }
1919 0 return REDIS_OK;
1920  
1921 0 oom:
1922 0 __redisSetError(&ac->c, REDIS_ERR_OOM, "Out of memory");
1923 0 ac->err = ac->c.err;
1924 0 ac->errstr = ac->c.errstr;
1925 0 return REDIS_ERR;
1926 }
1927  
1928 /* cbt must already be queued; on failure it is reported and freed. Sets
1929 * monitor and sub_count. */
1930 73 static int submit_to_redis(EV__Redis self, ev_redis_cb_t* cbt,
1931 int argc, const char** argv, const size_t* argvlen)
1932 {
1933 73 redisCallbackFn* fn = EV__redis_reply_cb;
1934 73 void* privdata = (void*)cbt;
1935 73 const char* cmd = argv[0];
1936 73 ev_redis_cb_t** replaced = NULL;
1937 73 int nreplaced = 0, nsub = 0, i, r;
1938 73 int pattern = 0, fix_subs = 0;
1939 73 int named_unsubs = 0, pending_unsubs = 0;
1940 73 HV* seen = NULL;
1941 73 const char* refused = NULL;
1942  
1943 73 cbt->ac = self->ac;
1944 73 ▶ cbt->monitor = cbt->persist && is_monitor_command(cmd);
Branch: 2 branches
BranchTrueFalseTotal
cbt->monitor = cbt->persist && is_monitor_command(cmd); 15 58 73
cbt->monitor = cbt->persist && is_monitor_command(cmd); 0 15 15
1945 73 cbt->watch = 0 == ascii_strcasecmp(cmd, "watch");
1946 73 cbt->sub_count = cbt->monitor ? 1 : 0;
1947 73 ▶ cbt->proto_cmd = 0 == ascii_strcasecmp(cmd, "hello") || 0 == ascii_strcasecmp(cmd, "reset");
Branch: 2 branches
BranchTrueFalseTotal
cbt->proto_cmd = 0 == ascii_strcasecmp(cmd, "hello") || 0 == ascii_strcasecmp(cmd, "reset"); 72 1 73
cbt->proto_cmd = 0 == ascii_strcasecmp(cmd, "hello") || 0 == ascii_strcasecmp(cmd, "reset"); 0 72 72
1948 73 cbt->multi = transaction_command(cmd);
1949  
1950 /* queued, the server answers QUEUED, which hiredis would hand to the next
1951 * command's callback */
1952 73 ▶ if (cbt->persist && self->in_multi) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->persist && self->in_multi) { 15 58 73
if (cbt->persist && self->in_multi) { 0 15 15
1953 0 refused = "pub/sub and MONITOR are not supported inside MULTI";
1954 }
1955 /* its protocol switch would come inside EXEC's reply, unseen */
1956 73 ▶ else if (self->in_multi && 0 == ascii_strcasecmp(cmd, "hello")) {
Branch: 2 branches
BranchTrueFalseTotal
else if (self->in_multi && 0 == ascii_strcasecmp(cmd, "hello")) { 0 73 73
else if (self->in_multi && 0 == ascii_strcasecmp(cmd, "hello")) { 0 0 0
1957 0 refused = "HELLO is not supported inside MULTI";
1958 }
1959 /* a RESET that waited may meet a subscription on_connect made since */
1960 73 ▶ else if (!cbt->persist && 0 == ascii_strcasecmp(cmd, "reset")
Branch: 2 branches
BranchTrueFalseTotal
else if (!cbt->persist && 0 == ascii_strcasecmp(cmd, "reset") 58 15 73
else if (!cbt->persist && 0 == ascii_strcasecmp(cmd, "reset") 0 58 58
1961 0 ▶ && (self->ac->c.flags & REDIS_SUBSCRIBED)) {
Branch: 1 branch
BranchTrueFalseTotal
&& (self->ac->c.flags & REDIS_SUBSCRIBED)) { 0 0 0
1962 0 refused = "RESET is not supported on a subscribed connection";
1963 }
1964 /* like RESET: the server refuses it, which would kill the connection */
1965 73 ▶ else if (!cbt->persist && 0 == ascii_strcasecmp(cmd, "hello")
Branch: 2 branches
BranchTrueFalseTotal
else if (!cbt->persist && 0 == ascii_strcasecmp(cmd, "hello") 58 15 73
else if (!cbt->persist && 0 == ascii_strcasecmp(cmd, "hello") 1 57 58
1966 1 ▶ && (self->ac->c.flags & REDIS_SUBSCRIBED)) {
Branch: 1 branch
BranchTrueFalseTotal
&& (self->ac->c.flags & REDIS_SUBSCRIBED)) { 0 1 1
1967 0 refused = "HELLO is not supported on a subscribed connection";
1968 }
1969 /* hiredis keeps no callback for unsubscribe (replies go to the subscribe
1970 * callback): pass NULL so it holds no dangling cbt. */
1971 73 ▶ else if (cbt->persist && is_unsubscribe_command(cmd)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (cbt->persist && is_unsubscribe_command(cmd)) { 15 58 73
else if (cbt->persist && is_unsubscribe_command(cmd)) { 0 15 15
1972 0 fn = NULL;
1973 0 privdata = NULL;
1974 0 ▶ if (self->ac->c.flags & REDIS_SUBSCRIBED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ac->c.flags & REDIS_SUBSCRIBED) { 0 0 0
1975 0 ▶ pattern = cmd[0] == 'p' || cmd[0] == 'P';
Branch: 2 branches
BranchTrueFalseTotal
pattern = cmd[0] == 'p' || cmd[0] == 'P'; 0 0 0
pattern = cmd[0] == 'p' || cmd[0] == 'P'; 0 0 0
1976 0 pending_unsubs = self->ac->sub.pending_unsubs;
1977 0 named_unsubs = argc > 1;
1978 0 ▶ if (REDIS_OK != prepare_unsubscribe(self, pattern, argc, argv, argvlen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != prepare_unsubscribe(self, pattern, argc, argv, argvlen)) { 0 0 0
1979 0 refused = "Out of memory";
1980 }
1981 }
1982 else {
1983 /* hiredis refuses these without an error string */
1984 0 refused = "not subscribed";
1985 }
1986 }
1987 73 ▶ else if (cbt->persist && !cbt->monitor) {
Branch: 2 branches
BranchTrueFalseTotal
else if (cbt->persist && !cbt->monitor) { 15 58 73
else if (cbt->persist && !cbt->monitor) { 15 0 15
1988 /* each distinct name gets one entry, taken over from any older cbt */
1989 15 ▶ pattern = (cmd[0] == 'p' || cmd[0] == 'P');
Branch: 2 branches
BranchTrueFalseTotal
pattern = (cmd[0] == 'p' || cmd[0] == 'P'); 15 0 15
pattern = (cmd[0] == 'p' || cmd[0] == 'P'); 0 15 15
1990 15 ▶ seen = argc > 2 ? newHV() : NULL;
Branch: 1 branch
BranchTrueFalseTotal
seen = argc > 2 ? newHV() : NULL; 0 15 15
1991 ev_redis_cb_t* owner;
1992 redisCallback* sub;
1993  
1994 15 Newx(replaced, argc, ev_redis_cb_t*);
1995 30 ▶ for (i = 1; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < argc; i++) { 15 15 30
1996 15 ▶ if (NULL != seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != seen) { 0 15 15
1997 0 SV** count = hv_fetch(seen, argv[i], (I32)argvlen[i], 0);
1998 0 ▶ if (NULL != count) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != count) { 0 0 0
1999 0 IV n = SvIV(*count);
2000 0 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 0 0 0
2001 0 (void)hv_store(seen, argv[i], (I32)argvlen[i], newSViv(n + 1), 0);
2002 0 fix_subs = 1;
2003 }
2004 0 continue;
2005 }
2006 }
2007 15 nsub++;
2008 15 owner = sub_owner(self->ac, pattern, argv[i], argvlen[i], &sub);
2009 15 ▶ if (NULL != sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != sub) { 0 15 15
2010 0 retain_unsubscribe((redisLibevEvents*)self->ac->ev.data,
2011 0 pattern, argv[i], argvlen[i], sub);
2012 }
2013 15 ▶ if (NULL != seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != seen) { 0 15 15
2014 0 ▶ int n = NULL != sub ? sub->pending_subs + 1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
int n = NULL != sub ? sub->pending_subs + 1 : 1; 0 0 0
2015 0 (void)hv_store(seen, argv[i], (I32)argvlen[i], newSViv(n), 0);
2016 0 ▶ if (n > 1) fix_subs = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) fix_subs = 1; 0 0 0
2017 }
2018 15 ▶ if (NULL != owner) replaced[nreplaced++] = owner;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != owner) replaced[nreplaced++] = owner; 0 15 15
2019 }
2020 }
2021 73 ▶ if (NULL != refused) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != refused) { 0 73 73
2022 0 r = REDIS_ERR;
2023 }
2024 else {
2025 73 ▶ if (!cbt->persist || cbt->monitor) {
Branch: 2 branches
BranchTrueFalseTotal
if (!cbt->persist || cbt->monitor) { 15 58 73
if (!cbt->persist || cbt->monitor) { 0 15 15
2026 58 redisLibevExpectNewReply(self->ac);
2027 }
2028 /* hiredis classifies these with locale-sensitive libc comparisons. */
2029 73 ▶ if (cbt->persist) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->persist) { 15 58 73
2030 15 ▶ argv[0] = cbt->monitor ? "monitor" : fn == NULL
Branch: 4 branches
BranchTrueFalseTotal
argv[0] = cbt->monitor ? "monitor" : fn == NULL 15 0 15
argv[0] = cbt->monitor ? "monitor" : fn == NULL 0 15 15
argv[0] = cbt->monitor ? "monitor" : fn == NULL 0 0 0
argv[0] = cbt->monitor ? "monitor" : fn == NULL 0 15 15
2031 ? (pattern ? "punsubscribe" : "unsubscribe")
2032 : (pattern ? "psubscribe" : "subscribe");
2033 }
2034 73 r = redisAsyncCommandArgv(self->ac, fn, privdata, argc, argv, argvlen);
2035 73 argv[0] = cmd;
2036 }
2037  
2038 73 ▶ if (REDIS_OK == r && cbt->multi) self->pending_multi++;
Branch: 2 branches
BranchTrueFalseTotal
if (REDIS_OK == r && cbt->multi) self->pending_multi++; 73 0 73
if (REDIS_OK == r && cbt->multi) self->pending_multi++; 0 73 73
2039 73 ▶ if (REDIS_OK == r && NULL != cbt->txn) {
Branch: 2 branches
BranchTrueFalseTotal
if (REDIS_OK == r && NULL != cbt->txn) { 73 0 73
if (REDIS_OK == r && NULL != cbt->txn) { 0 73 73
2040 0 cbt->txn->started = 1;
2041 0 ▶ if (cbt->multi == EV_REDIS_MULTI) cbt->txn->multi_started = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->multi == EV_REDIS_MULTI) cbt->txn->multi_started = 1; 0 0 0
2042 }
2043 /* Every named reply now has an entry; pending_unsubs counts only nil replies. */
2044 73 ▶ if (REDIS_OK == r && named_unsubs) self->ac->sub.pending_unsubs = pending_unsubs;
Branch: 2 branches
BranchTrueFalseTotal
if (REDIS_OK == r && named_unsubs) self->ac->sub.pending_unsubs = pending_unsubs; 73 0 73
if (REDIS_OK == r && named_unsubs) self->ac->sub.pending_unsubs = pending_unsubs; 0 73 73
2045  
2046 /* hiredis carries a repeated name's pending_subs into later new names.
2047 * Restore each name's own count; ordinary new subscriptions need no pass. */
2048 73 ▶ if (REDIS_OK == r && fix_subs) {
Branch: 2 branches
BranchTrueFalseTotal
if (REDIS_OK == r && fix_subs) { 73 0 73
if (REDIS_OK == r && fix_subs) { 0 73 73
2049 0 ▶ for (i = 1; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < argc; i++) { 0 0 0
2050 redisCallback* sub;
2051 0 SV** count = hv_fetch(seen, argv[i], (I32)argvlen[i], 0);
2052 0 (void)sub_owner(self->ac, pattern, argv[i], argvlen[i], &sub);
2053 0 ▶ if (NULL != sub && NULL != count && SvIV(*count) > 0) sub->pending_subs = (int)SvIV(*count);
Branch: 3 branches
BranchTrueFalseTotal
if (NULL != sub && NULL != count && SvIV(*count) > 0) sub->pending_subs = (int)SvIV(*count); 0 0 0
if (NULL != sub && NULL != count && SvIV(*count) > 0) sub->pending_subs = (int)SvIV(*count); 0 0 0
if (NULL != sub && NULL != count && SvIV(*count) > 0) sub->pending_subs = (int)SvIV(*count); 0 0 0
2054 }
2055 }
2056 73 ▶ if (NULL != seen) SvREFCNT_dec((SV*)seen);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != seen) SvREFCNT_dec((SV*)seen); 0 73 73
2057  
2058 73 ▶ if (REDIS_OK == r && NULL != replaced) {
Branch: 2 branches
BranchTrueFalseTotal
if (REDIS_OK == r && NULL != replaced) { 73 0 73
if (REDIS_OK == r && NULL != replaced) { 15 58 73
2059 15 int ndone = 0;
2060 15 cbt->sub_count = nsub;
2061 /* counts are exact, so each old cbt reaches 0 once */
2062 15 ▶ for (i = 0; i < nreplaced; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nreplaced; i++) { 0 15 15
2063 0 ▶ if (--replaced[i]->sub_count == 0) replaced[ndone++] = replaced[i];
Branch: 1 branch
BranchTrueFalseTotal
if (--replaced[i]->sub_count == 0) replaced[ndone++] = replaced[i]; 0 0 0
2064 }
2065 15 ▶ for (i = 0; i < ndone; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ndone; i++) { 0 15 15
2066 0 ev_redis_cb_t* old = replaced[i];
2067 0 ▶ if (old->running) continue; /* reply_cb frees it once the callback returns */
Branch: 1 branch
BranchTrueFalseTotal
if (old->running) continue; /* reply_cb frees it once the callback returns */ 0 0 0
2068 0 ngx_queue_remove(&old->queue);
2069 /* mortal: a DESTROY this triggers runs after the current statement */
2070 0 ▶ if (NULL != old->cb) sv_2mortal(old->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != old->cb) sv_2mortal(old->cb); 0 0 0
2071 0 free_cb_entry(old);
2072 }
2073 }
2074 73 Safefree(replaced);
2075  
2076 73 ▶ if (REDIS_OK != r) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != r) { 0 73 73
2077 0 ngx_queue_remove(&cbt->queue);
2078 0 ▶ if (!cbt->persist) drop_pending(self, cbt);
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) drop_pending(self, cbt); 0 0 0
2079  
2080 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
2081 0 ▶ invoke_callback_error(cbt->cb, sv_2mortal(newSVpv(
Branch: 3 branches
BranchTrueFalseTotal
invoke_callback_error(cbt->cb, sv_2mortal(newSVpv( 0 0 0
invoke_callback_error(cbt->cb, sv_2mortal(newSVpv( 0 0 0
invoke_callback_error(cbt->cb, sv_2mortal(newSVpv( 0 0 0
2082 NULL != refused ? refused
2083 : (self->ac && self->ac->errstr[0]) ? self->ac->errstr : "command failed", 0)));
2084 0 SvREFCNT_dec(cbt->cb);
2085 }
2086 0 free_cb_entry(cbt);
2087 73 ▶ } else if (fn == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (fn == NULL) { 0 73 73
2088 0 ngx_queue_remove(&cbt->queue);
2089 0 ▶ if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb); 0 0 0
2090 0 free_cb_entry(cbt);
2091 }
2092  
2093 73 return r;
2094 }
2095  
2096 68 static void send_next_waiting(EV__Redis self) {
2097 ngx_queue_t* q;
2098 ev_redis_wait_t* wt;
2099 ev_redis_cb_t* cbt;
2100  
2101 /* setters and local cancellation must not send backlog before setup finishes */
2102 68 ▶ if (self->in_connect_handler) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_connect_handler) return; 68 0 68
2103  
2104 while (1) {
2105 /* a failed submit's callback may change any of these */
2106 74 ▶ if (NULL == self->ac || self->intentional_disconnect) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self->ac || self->intentional_disconnect) return; 73 1 74
if (NULL == self->ac || self->intentional_disconnect) return; 0 73 73
2107 /* dying (a timeout sets err before the flags): its disconnect_cb
2108 * decides which to keep */
2109 73 ▶ if ((self->ac->c.flags & (REDIS_DISCONNECTING | REDIS_FREEING))
Branch: 1 branch
BranchTrueFalseTotal
if ((self->ac->c.flags & (REDIS_DISCONNECTING | REDIS_FREEING)) 73 0 73
2110 73 ▶ || self->ac->c.err) return;
Branch: 1 branch
BranchTrueFalseTotal
|| self->ac->c.err) return; 0 73 73
2111 /* a failed attempt must keep them */
2112 73 ▶ if (!(self->ac->c.flags & REDIS_CONNECTED)
Branch: 1 branch
BranchTrueFalseTotal
if (!(self->ac->c.flags & REDIS_CONNECTED) 0 73 73
2113 0 ▶ && self->reconnect && self->resume_waiting_on_reconnect) return;
Branch: 2 branches
BranchTrueFalseTotal
&& self->reconnect && self->resume_waiting_on_reconnect) return; 0 0 0
&& self->reconnect && self->resume_waiting_on_reconnect) return; 0 0 0
2114 73 ▶ if (!self->waiting_count) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->waiting_count) return; 67 6 73
2115 6 ▶ if (!ngx_queue_empty(&self->setup_queue)) q = ngx_queue_head(&self->setup_queue);
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->setup_queue)) q = ngx_queue_head(&self->setup_queue); 0 6 6
2116 6 ▶ else if (!ngx_queue_empty(&self->resume_setup_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!ngx_queue_empty(&self->resume_setup_queue)) { 0 6 6
2117 0 q = ngx_queue_head(&self->resume_setup_queue);
2118 }
2119 else {
2120 6 ▶ if (at_max_pending(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (at_max_pending(self)) return; 0 6 6
2121 6 q = ngx_queue_head(&self->wait_queue);
2122 }
2123 6 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
2124 6 ▶ if (self->pending_multi && needs_transaction_reply(wt->persist, wt->argv[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->pending_multi && needs_transaction_reply(wt->persist, wt->argv[0])) { 0 6 6
if (self->pending_multi && needs_transaction_reply(wt->persist, wt->argv[0])) { 0 0 0
2125 0 (void)sync_barrier(self);
2126 0 return;
2127 }
2128 6 (void)sync_barrier(self);
2129 6 ngx_queue_remove(q);
2130 6 self->waiting_count--;
2131  
2132 6 ▶ if (self->waiting_timeout_ms > 0 && NULL != self->loop) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->waiting_timeout_ms > 0 && NULL != self->loop) { 0 6 6
if (self->waiting_timeout_ms > 0 && NULL != self->loop) { 0 0 0
2133 0 ev_now_update(self->loop);
2134 0 ▶ if (ev_now(self->loop) - wt->queued_at >= self->waiting_timeout_ms / 1000.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ev_now(self->loop) - wt->queued_at >= self->waiting_timeout_ms / 1000.0) { 0 0 0
2135 0 ▶ if (NULL != wt->txn) rederive_span(self);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != wt->txn) rederive_span(self); 0 0 0
2136 0 ▶ if (NULL != wt->cb) invoke_callback_error(wt->cb, err_waiting_timeout);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != wt->cb) invoke_callback_error(wt->cb, err_waiting_timeout); 0 0 0
2137 0 free_wait_entry(wt);
2138 0 continue;
2139 }
2140 }
2141  
2142 6 Newx(cbt, 1, ev_redis_cb_t);
2143 6 cbt->cb = wt->cb;
2144 6 wt->cb = NULL;
2145 6 cbt->skipped = 0;
2146 6 cbt->running = 0;
2147 6 cbt->detached = 0;
2148 6 cbt->seq = ++self->cb_seq;
2149 6 cbt->persist = wt->persist;
2150 6 cbt->txn = txn_ref(wt->txn);
2151 6 ngx_queue_init(&cbt->queue);
2152 6 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
2153 6 ▶ if (!cbt->persist) self->pending_count++;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) self->pending_count++; 4 2 6
2154  
2155 6 (void)submit_to_redis(self, cbt, wt->argc,
2156 6 (const char**)wt->argv, wt->argvlen);
2157 6 free_wait_entry(wt);
2158 }
2159 }
2160  
2161 MODULE = EV::Redis PACKAGE = EV::Redis
2162  
2163 BOOT:
2164 {
2165 54 ▶ I_EV_API("EV::Redis");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::Redis"); 0 54 54
I_EV_API("EV::Redis"); 54 0 54
I_EV_API("EV::Redis"); 0 54 54
2166 54 ev_redis_stash = gv_stashpvs("EV::Redis", GV_ADD);
2167 {
2168 static int atfork_registered = 0;
2169 54 ▶ if (!atfork_registered) {
Branch: 1 branch
BranchTrueFalseTotal
if (!atfork_registered) { 54 0 54
2170 54 pthread_atfork(NULL, NULL, redisLibevAtforkChild);
2171 54 atfork_registered = 1;
2172 }
2173 }
2174  
2175 54 err_skipped = newSVpvs_share("skipped");
2176 54 SvREADONLY_on(err_skipped);
2177  
2178 54 err_waiting_timeout = newSVpvs_share("waiting timeout");
2179 54 SvREADONLY_on(err_waiting_timeout);
2180  
2181 54 err_disconnected = newSVpvs_share("disconnected");
2182 54 SvREADONLY_on(err_disconnected);
2183 #ifdef EV_REDIS_SSL
2184 54 redisInitOpenSSL();
2185 #endif
2186 }
2187  
2188 EV::Redis
2189 _new(char* class, EV::Loop loop);
2190 CODE:
2191 {
2192 PERL_UNUSED_VAR(class);
2193 72 Newxz(RETVAL, 1, ev_redis_t);
2194 72 RETVAL->magic = EV_REDIS_MAGIC;
2195 72 ngx_queue_init(&RETVAL->cb_queue);
2196 72 ngx_queue_init(&RETVAL->wait_queue);
2197 72 ngx_queue_init(&RETVAL->setup_queue);
2198 72 ngx_queue_init(&RETVAL->resume_setup_queue);
2199 72 ngx_queue_init(&RETVAL->drain_queue);
2200 72 RETVAL->loop = loop;
2201 /* pin: a dropped non-default loop would free the C loop under us */
2202 72 RETVAL->loop_sv = SvREFCNT_inc(SvRV(ST(1)));
2203 72 RETVAL->cloexec = 1;
2204 72 RETVAL->reconnect_delay_ms = 1000;
2205 }
2206 OUTPUT:
2207 RETVAL
2208  
2209 void
2210 DESTROY(SV* self_sv);
2211 CODE:
2212 {
2213 EV__Redis self;
2214 SV* inner;
2215 redisAsyncContext* ac_to_free;
2216 ngx_queue_t* dq;
2217 ev_redis_drain_t* dn;
2218 78 int skip_cb_cleanup = 0;
2219  
2220 /* quietly: a hash-based or already destroyed object has nothing to free */
2221 78 ▶ if (!SvROK(self_sv)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self_sv)) return; 0 78 78
2222 78 inner = SvRV(self_sv);
2223 78 ▶ if (SvTYPE(inner) >= SVt_PVAV || !SvOK(inner) || 0 == SvIV(inner)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (SvTYPE(inner) >= SVt_PVAV || !SvOK(inner) || 0 == SvIV(inner)) return; 77 1 78
if (SvTYPE(inner) >= SVt_PVAV || !SvOK(inner) || 0 == SvIV(inner)) return; 77 0 77
if (SvTYPE(inner) >= SVt_PVAV || !SvOK(inner) || 0 == SvIV(inner)) return; 1 76 77
2224 /* quietly, like a destroyed object: a foreign copy has nothing of ours */
2225 76 ▶ if (!ev_redis_owns_sv(inner)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ev_redis_owns_sv(inner)) return; 4 72 76
2226 72 self = INT2PTR(EV__Redis, SvIV(inner));
2227 72 ▶ if (self->magic != EV_REDIS_MAGIC) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) return; 0 72 72
2228  
2229 /* first: blocks re-entrant DESTROY */
2230 72 self->magic = EV_REDIS_FREED;
2231 /* an explicit $obj->DESTROY does not pin the referent its callbacks may free */
2232 72 sv_2mortal(SvREFCNT_inc_simple_NN(inner));
2233 72 detach_dead_loop(self);
2234  
2235 72 stop_reconnect_timer(self);
2236 72 stop_waiting_timer(self);
2237  
2238 /* Global destruction: run no Perl handlers, but still free every hiredis
2239 * context, or its watchers stay registered with dangling data. */
2240 72 ▶ if (PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) { 8 64 72
2241 /* cbs may already be freed SVs: null them before any redisAsyncFree */
2242 {
2243 ngx_queue_t* q;
2244 8 for (q = ngx_queue_head(&self->cb_queue);
2245 8 ▶ q != ngx_queue_sentinel(&self->cb_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->cb_queue); 0 8 8
2246 0 q = ngx_queue_next(q)) {
2247 0 ev_redis_cb_t* cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
2248 0 cbt->cb = NULL;
2249 }
2250 /* the contexts free them without unlinking: a callback of ours on
2251 * the stack must not walk them afterwards */
2252 8 ngx_queue_init(&self->cb_queue);
2253 }
2254 /* detach draining contexts first, as in the non-dirty path below */
2255 8 for (dq = ngx_queue_head(&self->drain_queue);
2256 8 ▶ dq != ngx_queue_sentinel(&self->drain_queue);
Branch: 1 branch
BranchTrueFalseTotal
dq != ngx_queue_sentinel(&self->drain_queue); 0 8 8
2257 0 dq = ngx_queue_next(dq)) {
2258 0 dn = ngx_queue_data(dq, ev_redis_drain_t, queue);
2259 0 dn->ac->data = NULL;
2260 }
2261 8 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 8 8
2262 0 self->ac->data = NULL;
2263 0 redisAsyncFree(self->ac);
2264 0 self->ac = NULL;
2265 }
2266 8 ▶ while (!ngx_queue_empty(&self->drain_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->drain_queue)) { 0 8 8
2267 redisAsyncContext* dc;
2268 0 dq = ngx_queue_head(&self->drain_queue);
2269 0 dc = ngx_queue_data(dq, ev_redis_drain_t, queue)->ac;
2270 0 untrack_draining(dc);
2271 0 dc->data = NULL;
2272 0 redisAsyncFree(dc);
2273 }
2274 8 free_c_fields(self);
2275 8 ▶ while (self->waiting_count) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->waiting_count) { 0 8 8
2276 0 ngx_queue_t* q = oldest_waiting(self);
2277 0 ev_redis_wait_t* wt = ngx_queue_data(q, ev_redis_wait_t, queue);
2278 0 ngx_queue_remove(q);
2279 0 self->waiting_count--;
2280 0 wt->cb = NULL;
2281 0 free_wait_entry(wt);
2282 }
2283 8 sv_setiv(inner, 0);
2284 /* a callback of ours may be on the stack */
2285 8 self->loop = NULL;
2286 8 check_destroyed(self);
2287 8 return;
2288 }
2289  
2290 64 self->reconnect = 0;
2291 /* callbacks run below may reach self through a weak ref */
2292 64 self->callback_depth++;
2293  
2294 /* detach draining contexts before freeing self->ac runs any callback */
2295 64 for (dq = ngx_queue_head(&self->drain_queue);
2296 65 ▶ dq != ngx_queue_sentinel(&self->drain_queue);
Branch: 1 branch
BranchTrueFalseTotal
dq != ngx_queue_sentinel(&self->drain_queue); 1 64 65
2297 1 dq = ngx_queue_next(dq)) {
2298 1 dn = ngx_queue_data(dq, ev_redis_drain_t, queue);
2299 1 dn->ac->data = NULL;
2300 1 unlink_cbts_of(self, dn->ac);
2301 }
2302  
2303 /* Null self->ac before redisAsyncFree: its reply callbacks run
2304 * send_next_waiting, which would issue commands into the dying context. */
2305 64 self->loop = NULL;
2306 64 ac_to_free = self->ac;
2307 64 self->ac = NULL;
2308 64 ▶ if (NULL != ac_to_free) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != ac_to_free) { 0 64 64
2309 /* Inside a hiredis callback the free is deferred and pending replies
2310 * fire later; null data so they take the self == NULL path. */
2311 0 ▶ if (ac_to_free->c.flags & REDIS_IN_CALLBACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (ac_to_free->c.flags & REDIS_IN_CALLBACK) { 0 0 0
2312 0 ac_to_free->data = NULL;
2313 0 skip_cb_cleanup = 1;
2314 }
2315 0 redisAsyncFree(ac_to_free);
2316 }
2317 65 ▶ while (!ngx_queue_empty(&self->drain_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->drain_queue)) { 1 64 65
2318 redisAsyncContext* dc;
2319 1 dq = ngx_queue_head(&self->drain_queue);
2320 1 dc = ngx_queue_data(dq, ev_redis_drain_t, queue)->ac;
2321 1 untrack_draining(dc);
2322 1 dc->data = NULL;
2323 1 redisAsyncFree(dc);
2324 1 skip_cb_cleanup = 1;
2325 }
2326 /* Inside a C callback, hiredis may still fire trailing replies for a
2327 * mid-teardown context (failed connect); reply_cb frees those cbts. */
2328 64 ▶ if (self->callback_depth > 1 || self->current_cb != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->callback_depth > 1 || self->current_cb != NULL) { 63 1 64
if (self->callback_depth > 1 || self->current_cb != NULL) { 0 63 63
2329 1 skip_cb_cleanup = 1;
2330 }
2331 64 ▶ CLEAR_HANDLER(self->error_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->error_handler); 62 2 64
2332 64 ▶ CLEAR_HANDLER(self->connect_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->connect_handler); 14 50 64
2333 64 ▶ CLEAR_HANDLER(self->disconnect_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->disconnect_handler); 10 54 64
2334 64 ▶ CLEAR_HANDLER(self->push_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->push_handler); 1 63 64
2335 /* all loop usage (timers, watcher teardown) is done */
2336 64 ▶ CLEAR_HANDLER(self->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->loop_sv); 64 0 64
2337 64 free_c_fields(self);
2338  
2339 /* even mid-batch: a batch owns only the commands it took */
2340 64 clear_wait_queue_sv(self, err_disconnected);
2341 64 ▶ if (!skip_cb_cleanup && !self->in_cb_cleanup) {
Branch: 2 branches
BranchTrueFalseTotal
if (!skip_cb_cleanup && !self->in_cb_cleanup) { 63 1 64
if (!skip_cb_cleanup && !self->in_cb_cleanup) { 63 0 63
2342 63 remove_cb_queue_sv(self, NULL, NULL);
2343 }
2344  
2345 /* later method calls croak; a second DESTROY returns early */
2346 64 sv_setiv(inner, 0);
2347 /* again: those callbacks may have set handlers or options */
2348 64 ▶ CLEAR_HANDLER(self->error_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->error_handler); 0 64 64
2349 64 ▶ CLEAR_HANDLER(self->connect_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->connect_handler); 0 64 64
2350 64 ▶ CLEAR_HANDLER(self->disconnect_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->disconnect_handler); 0 64 64
2351 64 ▶ CLEAR_HANDLER(self->push_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->push_handler); 0 64 64
2352 64 free_c_fields(self);
2353  
2354 64 self->callback_depth--;
2355 64 check_destroyed(self);
2356 }
2357  
2358 void
2359 connect(EV::Redis self, SV* host_sv, SV* port_sv = NULL);
2360 CODE:
2361 {
2362 redisOptions opts;
2363 const char* hostname;
2364 IV port;
2365  
2366 44 ▶ if (self->magic != EV_REDIS_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) { 0 44 44
2367 0 croak_caller("cannot connect: object is being destroyed");
2368 }
2369 44 ▶ if (NULL == self->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) { 0 44 44
2370 0 croak_caller("cannot connect: the event loop is gone");
2371 }
2372 44 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 44 44
2373 0 croak_caller("already connected");
2374 }
2375 /* omitted or undef means the default, like new()'s port */
2376 44 ▶ port = (NULL == port_sv || !SvOK(port_sv)) ? 6379 : sv_to_sat_iv(port_sv);
Branch: 2 branches
BranchTrueFalseTotal
port = (NULL == port_sv || !SvOK(port_sv)) ? 6379 : sv_to_sat_iv(port_sv); 44 0 44
port = (NULL == port_sv || !SvOK(port_sv)) ? 6379 : sv_to_sat_iv(port_sv); 44 0 44
2377 44 ▶ if (port < 1 || port > 65535) {
Branch: 2 branches
BranchTrueFalseTotal
if (port < 1 || port > 65535) { 44 0 44
if (port < 1 || port > 65535) { 0 44 44
2378 0 croak_caller("invalid port %" IVdf, port);
2379 }
2380 44 hostname = c_string(host_sv, "host name");
2381  
2382 42 self->intentional_disconnect = 0;
2383 42 self->reconnect_attempts = 0;
2384 42 stop_reconnect_timer(self);
2385 42 self->monitoring = 0;
2386 42 clear_connection_params(self);
2387 42 self->host = savepv(hostname);
2388 42 self->port = (int)port;
2389  
2390 42 memset(&opts, 0, sizeof(opts));
2391 42 pre_connect_common(self, &opts);
2392 42 REDIS_OPTIONS_SET_TCP(&opts, hostname, self->port);
2393 42 self->ac = redisAsyncConnectWithOptions(&opts);
2394 42 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 0 42 42
2395 0 croak_caller("connect error: cannot allocate memory");
2396 }
2397  
2398 42 connect_setup_or_retry(self);
2399 }
2400  
2401 void
2402 connect_unix(EV::Redis self, SV* path_sv);
2403 CODE:
2404 {
2405 redisOptions opts;
2406 const char* path;
2407  
2408 17 ▶ if (self->magic != EV_REDIS_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) { 0 17 17
2409 0 croak_caller("cannot connect: object is being destroyed");
2410 }
2411 17 ▶ if (NULL == self->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) { 0 17 17
2412 0 croak_caller("cannot connect: the event loop is gone");
2413 }
2414 17 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 17 17
2415 0 croak_caller("already connected");
2416 }
2417 #ifdef EV_REDIS_SSL
2418 17 ▶ if (NULL != self->ssl_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ssl_ctx) { 1 16 17
2419 1 croak_caller("TLS is not supported over unix sockets");
2420 }
2421 #endif
2422 16 path = c_string(path_sv, "unix socket path");
2423 /* hiredis would cut it to fit sun_path and leave it unterminated */
2424 14 ▶ if (strlen(path) >= sizeof(((struct sockaddr_un*)0)->sun_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(path) >= sizeof(((struct sockaddr_un*)0)->sun_path)) { 1 13 14
2425 1 croak_caller("unix socket path too long (max %d bytes)",
2426 (int)sizeof(((struct sockaddr_un*)0)->sun_path) - 1);
2427 }
2428  
2429 13 self->intentional_disconnect = 0;
2430 13 self->reconnect_attempts = 0;
2431 13 stop_reconnect_timer(self);
2432 13 self->monitoring = 0;
2433 13 clear_connection_params(self);
2434 13 self->path = savepv(path);
2435  
2436 13 memset(&opts, 0, sizeof(opts));
2437 13 pre_connect_common(self, &opts);
2438 13 REDIS_OPTIONS_SET_UNIX(&opts, path);
2439 13 self->ac = redisAsyncConnectWithOptions(&opts);
2440 13 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 0 13 13
2441 0 croak_caller("connect error: cannot allocate memory");
2442 }
2443  
2444 13 connect_setup_or_retry(self);
2445 }
2446  
2447 void
2448 disconnect(EV::Redis self);
2449 CODE:
2450 {
2451 23 self->intentional_disconnect = 1;
2452 23 stop_reconnect_timer(self);
2453 23 self->reconnect_attempts = 0;
2454  
2455 /* redisAsyncDisconnect defers unless outside a callback with no pending
2456 * replies; track c so DESTROY can free it (disconnect_cb untracks). */
2457 23 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 17 6 23
2458 17 redisAsyncContext* c = self->ac;
2459 17 track_draining(self, c);
2460 17 self->callback_depth++;
2461 17 ▶ if (self->monitoring) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->monitoring) { 0 17 17
2462 /* a monitor stream never drains: hiredis re-queues its record per line */
2463 0 redisAsyncFree(c);
2464 }
2465 else {
2466 17 redisAsyncDisconnect(c);
2467 }
2468 /* on_disconnect may have connected again, even into MONITOR */
2469 17 ▶ if (self->ac == c) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ac == c) { 2 15 17
2470 2 self->ac = NULL;
2471 2 self->monitoring = 0;
2472 2 detach_pending_of(self, c);
2473 }
2474 17 self->callback_depth--;
2475 17 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 17 17
2476 }
2477  
2478 /* no disconnect_cb clears these for a deferred or still-connecting c */
2479 23 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 23 0 23
2480 23 self->callback_depth++;
2481 23 clear_wait_queue_sv(self, err_disconnected);
2482 23 self->callback_depth--;
2483 23 check_destroyed(self);
2484 }
2485 }
2486  
2487 int
2488 is_connected(EV::Redis self);
2489 CODE:
2490 {
2491 69 ▶ RETVAL = (NULL != self->ac) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (NULL != self->ac) ? 1 : 0; 40 29 69
2492 }
2493 OUTPUT:
2494 RETVAL
2495  
2496 SV*
2497 connect_timeout(EV::Redis self, SV* timeout_ms = NULL);
2498 CODE:
2499 {
2500 5 RETVAL = timeout_accessor(&self->connect_timeout, timeout_ms, "connect_timeout");
2501 }
2502 OUTPUT:
2503 RETVAL
2504  
2505 SV*
2506 command_timeout(EV::Redis self, SV* timeout_ms = NULL);
2507 CODE:
2508 {
2509 15 RETVAL = timeout_accessor(&self->command_timeout, timeout_ms, "command_timeout");
2510 15 ▶ if (NULL != timeout_ms && SvOK(timeout_ms) && NULL != self->ac && NULL != self->command_timeout) {
Branch: 4 branches
BranchTrueFalseTotal
if (NULL != timeout_ms && SvOK(timeout_ms) && NULL != self->ac && NULL != self->command_timeout) { 15 0 15
if (NULL != timeout_ms && SvOK(timeout_ms) && NULL != self->ac && NULL != self->command_timeout) { 15 0 15
if (NULL != timeout_ms && SvOK(timeout_ms) && NULL != self->ac && NULL != self->command_timeout) { 1 14 15
if (NULL != timeout_ms && SvOK(timeout_ms) && NULL != self->ac && NULL != self->command_timeout) { 1 0 1
2511 1 redisAsyncSetTimeout(self->ac, *self->command_timeout);
2512 /* while connecting, the armed timer is the connect timeout */
2513 1 ▶ if (self->ac->c.flags & REDIS_CONNECTED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ac->c.flags & REDIS_CONNECTED) { 1 0 1
2514 1 redisLibevRefreshTimeout(self->ac, *self->command_timeout);
2515 }
2516 }
2517 }
2518 OUTPUT:
2519 RETVAL
2520  
2521 SV*
2522 on_error(EV::Redis self, SV* handler = NULL);
2523 PREINIT:
2524 SV* old;
2525 CODE:
2526 {
2527 76 RETVAL = handler_accessor(&self->error_handler, handler, items > 1, &old);
2528 75 ▶ if (NULL != old) SvREFCNT_dec(old);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != old) SvREFCNT_dec(old); 3 72 75
2529 }
2530 OUTPUT:
2531 RETVAL
2532  
2533 SV*
2534 on_connect(EV::Redis self, SV* handler = NULL);
2535 PREINIT:
2536 SV* old;
2537 CODE:
2538 {
2539 21 RETVAL = handler_accessor(&self->connect_handler, handler, items > 1, &old);
2540 21 ▶ if (NULL != old) SvREFCNT_dec(old);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != old) SvREFCNT_dec(old); 0 21 21
2541 }
2542 OUTPUT:
2543 RETVAL
2544  
2545 SV*
2546 on_disconnect(EV::Redis self, SV* handler = NULL);
2547 PREINIT:
2548 SV* old;
2549 CODE:
2550 {
2551 10 RETVAL = handler_accessor(&self->disconnect_handler, handler, items > 1, &old);
2552 10 ▶ if (NULL != old) SvREFCNT_dec(old);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != old) SvREFCNT_dec(old); 0 10 10
2553 }
2554 OUTPUT:
2555 RETVAL
2556  
2557 SV*
2558 on_push(EV::Redis self, SV* handler = NULL);
2559 PREINIT:
2560 SV* old;
2561 CODE:
2562 {
2563 1 RETVAL = handler_accessor(&self->push_handler, handler, items > 1, &old);
2564 1 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 1 0 1
2565 1 ▶ if (NULL != self->push_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->push_handler) { 1 0 1
2566 1 redisAsyncSetPushCallback(self->ac, EV__redis_push_cb);
2567 } else {
2568 0 redisAsyncSetPushCallback(self->ac, NULL);
2569 }
2570 }
2571 1 ▶ if (NULL != old) SvREFCNT_dec(old);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != old) SvREFCNT_dec(old); 0 1 1
2572 }
2573 OUTPUT:
2574 RETVAL
2575  
2576 int
2577 command(EV::Redis self, ...);
2578 PREINIT:
2579 SV* cb;
2580 char** argv;
2581 size_t* argvlen;
2582 STRLEN len;
2583 int argc, i, persist;
2584 ev_redis_txn_t* txn_now;
2585 ev_redis_cb_t* cbt;
2586 ev_redis_wait_t* wt;
2587 char* p;
2588 CODE:
2589 {
2590 92 ▶ if (items < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) { 0 92 92
2591 0 croak_caller("Usage: command(\"command\", ..., [$callback])");
2592 }
2593  
2594 92 cb = ST(items - 1);
2595 92 ▶ if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 84 8 92
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 84 0 84
2596 /* a copy: cb may be the caller's variable, reassigned before the reply */
2597 84 cb = sv_2mortal(newSVsv(cb));
2598 84 argc = items - 2;
2599 }
2600 else {
2601 8 cb = NULL;
2602 8 argc = items - 1;
2603 }
2604  
2605 92 ▶ if (argc < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (argc < 1) { 0 92 92
2606 0 croak_caller("Usage: command(\"command\", ..., [$callback])");
2607 }
2608 92 ▶ if (self->magic != EV_REDIS_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) { 0 92 92
2609 0 croak_caller("cannot send commands: object is being destroyed");
2610 }
2611 92 ▶ if (NULL == self->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) { 0 92 92
2612 0 croak_caller("cannot send commands: the event loop is gone");
2613 }
2614  
2615 /* hiredis monitor mode re-queues each just-run callback record; a freed
2616 * one-shot cbt would be re-fired as dangling privdata. */
2617 92 ▶ if (self->monitoring) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->monitoring) { 0 92 92
2618 0 croak_caller("cannot send commands while MONITOR is active on this connection");
2619 }
2620  
2621 92 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 17 75 92
2622 17 ▶ if (!self->reconnect_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->reconnect_timer_active) { 7 10 17
2623 7 croak_caller("connection required before calling command");
2624 }
2625 }
2626 85 Newx(argv, argc, char*);
2627 85 SAVEFREEPV(argv);
2628 85 Newx(argvlen, argc, size_t);
2629 85 SAVEFREEPV(argvlen);
2630  
2631 {
2632 /* warnings from converting the arguments (undef) name the caller's
2633 * line and follow its lexical warnings */
2634 85 COP* saved = PL_curcop;
2635 85 COP* cop = caller_cop();
2636 85 ▶ if (NULL != cop) PL_curcop = cop;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cop) PL_curcop = cop; 38 47 85
2637 223 ▶ for (i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) { 139 84 223
2638 139 SV* arg = ST(i + 1);
2639 /* SvPVbyte's own croak would name a line of this module */
2640 139 ▶ if (SvPOK(arg) && SvUTF8(arg) && !SvGMAGICAL(arg) && has_wide_char(arg)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvPOK(arg) && SvUTF8(arg) && !SvGMAGICAL(arg) && has_wide_char(arg)) { 136 3 139
if (SvPOK(arg) && SvUTF8(arg) && !SvGMAGICAL(arg) && has_wide_char(arg)) { 2 134 136
if (SvPOK(arg) && SvUTF8(arg) && !SvGMAGICAL(arg) && has_wide_char(arg)) { 2 0 2
if (SvPOK(arg) && SvUTF8(arg) && !SvGMAGICAL(arg) && has_wide_char(arg)) { 1 1 2
2641 1 croak_caller("Wide character in subroutine entry");
2642 }
2643 /* wire bytes must not depend on the UTF8 flag */
2644 138 argv[i] = SvPVbyte(arg, len);
2645 138 argvlen[i] = len;
2646 }
2647 84 PL_curcop = saved;
2648 }
2649 /* the command checks below stop at a NUL; hiredis and the server may not */
2650 84 ▶ if (NULL != memchr(argv[0], '\0', argvlen[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != memchr(argv[0], '\0', argvlen[0])) { 0 84 84
2651 0 croak_caller("command name contains a NUL byte");
2652 }
2653  
2654 84 ▶ if (is_shard_pubsub_command(argv[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_shard_pubsub_command(argv[0])) { 0 84 84
2655 0 croak_caller("%s is not supported: bundled hiredis has no sharded pub/sub "
2656 "support (use spublish for publishing; subscribe via a plain "
2657 "subscribe on a non-cluster channel)", argv[0]);
2658 }
2659 /* hiredis drops a leading 'p' and would run it as MONITOR */
2660 84 ▶ if (0 == ascii_strcasecmp(argv[0], "pmonitor")) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == ascii_strcasecmp(argv[0], "pmonitor")) { 0 84 84
2661 0 croak_caller("%s is not supported", argv[0]);
2662 }
2663 84 ▶ if (is_reply_suppressing(argc, argv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_reply_suppressing(argc, argv)) { 0 84 84
2664 0 ▶ if (0 == ascii_strcasecmp(argv[0], "client")) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == ascii_strcasecmp(argv[0], "client")) { 0 0 0
2665 0 croak_caller("CLIENT REPLY %s is not supported: replies are matched to "
2666 0 "callbacks by their order", argv[2]);
2667 }
2668 0 croak_caller("REPLCONF ACK and GETACK are not supported: they get no reply");
2669 }
2670 /* the replication stream that follows is not one reply per command: the
2671 * first unexpected one trips hiredis's assert */
2672 84 ▶ if (0 == ascii_strcasecmp(argv[0], "sync") || 0 == ascii_strcasecmp(argv[0], "psync")) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == ascii_strcasecmp(argv[0], "sync") || 0 == ascii_strcasecmp(argv[0], "psync")) { 84 0 84
if (0 == ascii_strcasecmp(argv[0], "sync") || 0 == ascii_strcasecmp(argv[0], "psync")) { 0 84 84
2673 0 croak_caller("%s is not supported: a replication stream follows", argv[0]);
2674 }
2675 84 ▶ if (0 == ascii_strcasecmp(argv[0], "reset") && may_be_subscribed(self)) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == ascii_strcasecmp(argv[0], "reset") && may_be_subscribed(self)) { 0 84 84
if (0 == ascii_strcasecmp(argv[0], "reset") && may_be_subscribed(self)) { 0 0 0
2676 0 croak_caller("RESET is not supported on a subscribed connection");
2677 }
2678  
2679 84 persist = is_persistent_command(argv[0]);
2680  
2681 /* A channel-less subscribe is one-shot to hiredis but persistent to us:
2682 * the cbt would strand and double-fire at disconnect. */
2683 84 ▶ if (persist && argc < 2 &&
Branch: 1 branch
BranchTrueFalseTotal
if (persist && argc < 2 && 15 69 84
2684 0 ▶ !is_monitor_command(argv[0]) && !is_unsubscribe_command(argv[0])) {
Branch: 1 branch
BranchTrueFalseTotal
!is_monitor_command(argv[0]) && !is_unsubscribe_command(argv[0])) { 0 0 0
2685 0 croak_caller("%s requires at least one channel", argv[0]);
2686 }
2687  
2688 84 ▶ if (is_monitor_command(argv[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_monitor_command(argv[0])) { 0 84 84
2689 0 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 0 0 0
2690 0 croak_caller("MONITOR requires an active connection");
2691 }
2692 /* pending commands would hit the same re-queue hazard; hiredis's own records
2693 * count too, as skip_pending unlinks commands still outstanding there.
2694 * Inside MULTI the submit fails through the callback instead, so the
2695 * idle check (which a reply callback of its own never passes) is skipped. */
2696 0 int busy = 0;
2697 ngx_queue_t* q;
2698 0 for (q = ngx_queue_head(&self->cb_queue);
2699 0 ▶ q != ngx_queue_sentinel(&self->cb_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->cb_queue); 0 0 0
2700 0 q = ngx_queue_next(q)) {
2701 0 ev_redis_cb_t* cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
2702 /* detached drained replies belong to a replaced connection */
2703 0 ▶ if (!cbt->detached && cbt->ac == self->ac) { busy = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (!cbt->detached && cbt->ac == self->ac) { busy = 1; break; } 0 0 0
if (!cbt->detached && cbt->ac == self->ac) { busy = 1; break; } 0 0 0
2704 }
2705 0 ▶ if (!self->in_multi
Branch: 1 branch
BranchTrueFalseTotal
if (!self->in_multi 0 0 0
2706 0 ▶ && (busy || self->waiting_count
Branch: 2 branches
BranchTrueFalseTotal
&& (busy || self->waiting_count 0 0 0
&& (busy || self->waiting_count 0 0 0
2707 0 ▶ || NULL != self->ac->replies.head || NULL != self->ac->sub.replies.head
Branch: 2 branches
BranchTrueFalseTotal
|| NULL != self->ac->replies.head || NULL != self->ac->sub.replies.head 0 0 0
|| NULL != self->ac->replies.head || NULL != self->ac->sub.replies.head 0 0 0
2708 0 ▶ || (self->ac->c.flags & REDIS_SUBSCRIBED))) {
Branch: 1 branch
BranchTrueFalseTotal
|| (self->ac->c.flags & REDIS_SUBSCRIBED))) { 0 0 0
2709 0 croak_caller("MONITOR requires an idle connection "
2710 "(no pending, waiting, or subscribed commands)");
2711 }
2712 }
2713  
2714 /* Track a transaction across both waiting queues and submitted callbacks. */
2715 {
2716 84 int boundary = transaction_command(argv[0]);
2717 84 int opens = EV_REDIS_MULTI == boundary
2718 84 ▶ || 0 == ascii_strcasecmp(argv[0], "watch");
Branch: 2 branches
BranchTrueFalseTotal
|| 0 == ascii_strcasecmp(argv[0], "watch"); 84 0 84
|| 0 == ascii_strcasecmp(argv[0], "watch"); 0 84 84
2719 84 ▶ if (opens && NULL == self->issued_txn) {
Branch: 2 branches
BranchTrueFalseTotal
if (opens && NULL == self->issued_txn) { 0 84 84
if (opens && NULL == self->issued_txn) { 0 0 0
2720 0 Newxz(self->issued_txn, 1, ev_redis_txn_t);
2721 0 self->issued_txn->refs = 1;
2722 }
2723 84 txn_now = txn_ref(self->issued_txn);
2724 84 ▶ if (NULL != txn_now) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != txn_now) { 0 84 84
2725 0 SAVEDESTRUCTOR_X(txn_scope_free, txn_now);
2726 0 ▶ if (EV_REDIS_MULTI == boundary) txn_now->multi = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (EV_REDIS_MULTI == boundary) txn_now->multi = 1; 0 0 0
2727 0 ▶ if ((!opens && boundary) || (!txn_now->multi
Branch: 3 branches
BranchTrueFalseTotal
if ((!opens && boundary) || (!txn_now->multi 0 0 0
if ((!opens && boundary) || (!txn_now->multi 0 0 0
if ((!opens && boundary) || (!txn_now->multi 0 0 0
2728 0 ▶ && 0 == ascii_strcasecmp(argv[0], "unwatch"))) {
Branch: 1 branch
BranchTrueFalseTotal
&& 0 == ascii_strcasecmp(argv[0], "unwatch"))) { 0 0 0
2729 0 txn_now->closed = 1;
2730 0 txn_unref(self->issued_txn);
2731 0 self->issued_txn = NULL;
2732 }
2733 }
2734 }
2735  
2736 /* Queued behind waiting commands, to keep order (on_connect's setup goes
2737 * first, past max_pending too); while connecting under resume_waiting_on_reconnect,
2738 * so a failed attempt keeps them; on a dying connection, where a retry-on-error
2739 * callback would recurse. MONITOR must mark the connection at once. */
2740 84 ▶ if (!is_monitor_command(argv[0]) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!is_monitor_command(argv[0]) && 84 0 84
2741 84 ▶ (NULL == self->ac || ac_dying(self->ac) ||
Branch: 2 branches
BranchTrueFalseTotal
(NULL == self->ac || ac_dying(self->ac) || 75 9 84
(NULL == self->ac || ac_dying(self->ac) || 70 5 75
2742 70 ▶ (self->pending_multi && needs_transaction_reply(persist, argv[0])) ||
Branch: 2 branches
BranchTrueFalseTotal
(self->pending_multi && needs_transaction_reply(persist, argv[0])) || 0 70 70
(self->pending_multi && needs_transaction_reply(persist, argv[0])) || 0 0 0
2743 70 ▶ (!(self->ac->c.flags & REDIS_CONNECTED) &&
Branch: 1 branch
BranchTrueFalseTotal
(!(self->ac->c.flags & REDIS_CONNECTED) && 25 45 70
2744 77 ▶ self->reconnect && self->resume_waiting_on_reconnect) ||
Branch: 3 branches
BranchTrueFalseTotal
self->reconnect && self->resume_waiting_on_reconnect) || 3 22 25
self->reconnect && self->resume_waiting_on_reconnect) || 0 3 3
self->reconnect && self->resume_waiting_on_reconnect) || 0 52 52
2745 67 ▶ (self->in_connect_handler ? !ngx_queue_empty(&self->setup_queue)
Branch: 2 branches
BranchTrueFalseTotal
(self->in_connect_handler ? !ngx_queue_empty(&self->setup_queue) 15 52 67
(self->in_connect_handler ? !ngx_queue_empty(&self->setup_queue) 0 15 15
2746 52 ▶ : (self->waiting_count || at_max_pending(self))))) {
Branch: 2 branches
BranchTrueFalseTotal
: (self->waiting_count || at_max_pending(self))))) { 52 0 52
: (self->waiting_count || at_max_pending(self))))) { 0 52 52
2747 17 Newx(wt, 1, ev_redis_wait_t);
2748 17 Newx(wt->argv, argc, char*);
2749 17 Newx(wt->argvlen, argc, size_t);
2750 48 ▶ for (i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) { 31 17 48
2751 31 Newx(p, argvlen[i] + 1, char);
2752 31 Copy(argv[i], p, argvlen[i], char);
2753 31 p[argvlen[i]] = '\0';
2754 31 wt->argv[i] = p;
2755 31 wt->argvlen[i] = argvlen[i];
2756 }
2757 17 wt->argc = argc;
2758 17 wt->cb = SvREFCNT_inc(cb);
2759 17 wt->persist = persist;
2760 17 wt->txn = txn_ref(txn_now);
2761 /* ev_now is stale outside ev_run and would expire the command early */
2762 17 ev_now_update(self->loop);
2763 17 wt->queued_at = ev_now(self->loop);
2764 17 ngx_queue_init(&wt->queue);
2765 17 ▶ ngx_queue_insert_tail(self->in_connect_handler ? &self->setup_queue : &self->wait_queue,
Branch: 3 branches
BranchTrueFalseTotal
ngx_queue_insert_tail(self->in_connect_handler ? &self->setup_queue : &self->wait_queue, 0 17 17
ngx_queue_insert_tail(self->in_connect_handler ? &self->setup_queue : &self->wait_queue, 0 17 17
ngx_queue_insert_tail(self->in_connect_handler ? &self->setup_queue : &self->wait_queue, 0 17 17
2766 &wt->queue);
2767 17 self->waiting_count++;
2768 17 schedule_waiting_timer(self);
2769 17 RETVAL = REDIS_OK;
2770 }
2771 else {
2772 67 Newx(cbt, 1, ev_redis_cb_t);
2773 67 cbt->cb = SvREFCNT_inc(cb);
2774 67 cbt->skipped = 0;
2775 67 cbt->running = 0;
2776 67 cbt->detached = 0;
2777 67 cbt->seq = ++self->cb_seq;
2778 67 cbt->persist = persist;
2779 67 cbt->txn = txn_ref(txn_now);
2780 67 ngx_queue_init(&cbt->queue);
2781 67 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
2782 67 ▶ if (!persist) self->pending_count++;
Branch: 1 branch
BranchTrueFalseTotal
if (!persist) self->pending_count++; 54 13 67
2783  
2784 /* a failed submit runs the callback, which may destroy self */
2785 67 self->callback_depth++;
2786 67 RETVAL = submit_to_redis(self, cbt,
2787 argc, (const char**)argv, argvlen);
2788 67 self->callback_depth--;
2789 67 ▶ if (self->magic == EV_REDIS_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED) { 0 67 67
2790 0 check_destroyed(self);
2791 }
2792 67 ▶ else if (REDIS_OK == RETVAL && is_monitor_command(argv[0])) {
Branch: 2 branches
BranchTrueFalseTotal
else if (REDIS_OK == RETVAL && is_monitor_command(argv[0])) { 67 0 67
else if (REDIS_OK == RETVAL && is_monitor_command(argv[0])) { 0 67 67
2793 0 self->monitoring = 1;
2794 }
2795 }
2796 }
2797 OUTPUT:
2798 RETVAL
2799  
2800 void
2801 reconnect(EV::Redis self, bool enable, SV* delay_ms = NULL, SV* max_attempts = NULL);
2802 CODE:
2803 {
2804 /* omitted restores the default; explicit undef keeps the current value */
2805 21 ▶ if (NULL == delay_ms) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == delay_ms) { 4 17 21
2806 4 self->reconnect_delay_ms = 1000;
2807 }
2808 17 ▶ else if (SvOK(delay_ms)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvOK(delay_ms)) { 17 0 17
2809 17 IV delay = sv_to_sat_iv(delay_ms);
2810 17 validate_timeout_ms(delay, "reconnect_delay");
2811 17 self->reconnect_delay_ms = (int)delay;
2812 }
2813 21 ▶ if (NULL == max_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == max_attempts) { 4 17 21
2814 4 self->max_reconnect_attempts = 0;
2815 }
2816 17 ▶ else if (SvOK(max_attempts)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvOK(max_attempts)) { 17 0 17
2817 17 IV attempts = sv_to_sat_iv(max_attempts);
2818 17 self->max_reconnect_attempts = attempts < 0 ? 0
2819 17 ▶ : attempts > INT_MAX ? INT_MAX : (int)attempts;
Branch: 2 branches
BranchTrueFalseTotal
: attempts > INT_MAX ? INT_MAX : (int)attempts; 17 0 17
: attempts > INT_MAX ? INT_MAX : (int)attempts; 17 0 17
2820 }
2821 21 self->reconnect = enable ? 1 : 0;
2822 21 self->reconnect_attempts = 0;
2823  
2824 21 ▶ if (!enable) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enable) { 4 17 21
2825 4 stop_reconnect_timer(self);
2826 /* commands queued for the cancelled reconnect would hang forever */
2827 4 ▶ if (NULL == self->ac && self->waiting_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self->ac && self->waiting_count) { 1 3 4
if (NULL == self->ac && self->waiting_count) { 0 1 1
2828 0 self->callback_depth++;
2829 0 clear_wait_queue_sv(self,
2830 sv_2mortal(newSVpv("reconnect disabled", 0)));
2831 0 self->callback_depth--;
2832 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
2833 }
2834 }
2835 }
2836  
2837 int
2838 reconnect_enabled(EV::Redis self);
2839 CODE:
2840 {
2841 0 ▶ RETVAL = self->reconnect;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->reconnect; 0 0 0
2842 }
2843 OUTPUT:
2844 RETVAL
2845  
2846 int
2847 pending_count(EV::Redis self);
2848 CODE:
2849 {
2850 2 ▶ RETVAL = self->pending_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->pending_count; 1 1 2
2851 }
2852 OUTPUT:
2853 RETVAL
2854  
2855 int
2856 waiting_count(EV::Redis self);
2857 CODE:
2858 {
2859 4 ▶ RETVAL = self->waiting_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->waiting_count; 3 1 4
2860 }
2861 OUTPUT:
2862 RETVAL
2863  
2864 int
2865 max_pending(EV::Redis self, SV* limit = NULL);
2866 CODE:
2867 {
2868 0 ▶ if (NULL != limit && SvOK(limit)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != limit && SvOK(limit)) { 0 0 0
if (NULL != limit && SvOK(limit)) { 0 0 0
2869 0 IV val = sv_to_sat_iv(limit);
2870 0 ▶ if (val < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val < 0) { 0 0 0
2871 0 croak_caller("max_pending must be non-negative");
2872 }
2873 0 ▶ self->max_pending = val > INT_MAX ? INT_MAX : (int)val;
Branch: 1 branch
BranchTrueFalseTotal
self->max_pending = val > INT_MAX ? INT_MAX : (int)val; 0 0 0
2874  
2875 0 self->callback_depth++;
2876 0 send_next_waiting(self);
2877 0 self->callback_depth--;
2878 0 ▶ if (check_destroyed(self)) XSRETURN_IV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) XSRETURN_IV(0); 0 0 0
2879 }
2880 0 ▶ RETVAL = self->max_pending;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->max_pending; 0 0 0
2881 }
2882 OUTPUT:
2883 RETVAL
2884  
2885 SV*
2886 waiting_timeout(EV::Redis self, SV* timeout_ms = NULL);
2887 CODE:
2888 {
2889 0 ▶ if (NULL != timeout_ms && SvOK(timeout_ms)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != timeout_ms && SvOK(timeout_ms)) { 0 0 0
if (NULL != timeout_ms && SvOK(timeout_ms)) { 0 0 0
2890 0 IV ms = sv_to_sat_iv(timeout_ms);
2891 0 validate_timeout_ms(ms, "waiting_timeout");
2892 0 self->waiting_timeout_ms = (int)ms;
2893 0 schedule_waiting_timer(self);
2894 }
2895  
2896 0 RETVAL = newSViv((IV)self->waiting_timeout_ms);
2897 }
2898 OUTPUT:
2899 RETVAL
2900  
2901 int
2902 resume_waiting_on_reconnect(EV::Redis self, SV* value = NULL);
2903 CODE:
2904 {
2905 5 ▶ if (NULL != value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != value && SvOK(value)) { 5 0 5
if (NULL != value && SvOK(value)) { 5 0 5
2906 5 self->resume_waiting_on_reconnect = SvTRUE(value) ? 1 : 0;
2907 }
2908 5 ▶ RETVAL = self->resume_waiting_on_reconnect;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->resume_waiting_on_reconnect; 3 2 5
2909 }
2910 OUTPUT:
2911 RETVAL
2912  
2913 int
2914 priority(EV::Redis self, SV* value = NULL);
2915 CODE:
2916 {
2917 2 ▶ if (NULL != value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != value && SvOK(value)) { 2 0 2
if (NULL != value && SvOK(value)) { 2 0 2
2918 2 IV prio = sv_to_sat_iv(value);
2919 2 ▶ if (prio < EV_MINPRI) prio = EV_MINPRI;
Branch: 1 branch
BranchTrueFalseTotal
if (prio < EV_MINPRI) prio = EV_MINPRI; 0 2 2
2920 2 ▶ if (prio > EV_MAXPRI) prio = EV_MAXPRI;
Branch: 1 branch
BranchTrueFalseTotal
if (prio > EV_MAXPRI) prio = EV_MAXPRI; 0 2 2
2921 2 self->priority = (int)prio;
2922 2 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 1 1 2
2923 1 redisLibevSetPriority(self->ac, self->priority);
2924 }
2925 }
2926 2 ▶ RETVAL = self->priority;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->priority; 0 2 2
2927 }
2928 OUTPUT:
2929 RETVAL
2930  
2931 int
2932 keepalive(EV::Redis self, SV* value = NULL);
2933 CODE:
2934 {
2935 0 ▶ if (NULL != value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != value && SvOK(value)) { 0 0 0
if (NULL != value && SvOK(value)) { 0 0 0
2936 0 IV interval = sv_to_sat_iv(value);
2937 0 ▶ if (interval < 0) croak_caller("keepalive interval must be non-negative");
Branch: 1 branch
BranchTrueFalseTotal
if (interval < 0) croak_caller("keepalive interval must be non-negative"); 0 0 0
2938 0 ▶ if (interval > MAX_KEEPALIVE) croak_caller("keepalive interval too large (max %d)", MAX_KEEPALIVE);
Branch: 1 branch
BranchTrueFalseTotal
if (interval > MAX_KEEPALIVE) croak_caller("keepalive interval too large (max %d)", MAX_KEEPALIVE); 0 0 0
2939 0 ▶ if (NULL != self->ac && interval > 0
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != self->ac && interval > 0 0 0 0
if (NULL != self->ac && interval > 0 0 0 0
2940 0 ▶ && self->ac->c.connection_type == REDIS_CONN_TCP) {
Branch: 1 branch
BranchTrueFalseTotal
&& self->ac->c.connection_type == REDIS_CONN_TCP) { 0 0 0
2941 0 redisContext* rc = &self->ac->c;
2942 0 int saved_err = rc->err;
2943 char saved_errstr[sizeof(rc->errstr)];
2944 0 Copy(rc->errstr, saved_errstr, sizeof(saved_errstr), char);
2945 0 ▶ if (REDIS_OK != redisEnableKeepAliveWithInterval(rc, (int)interval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != redisEnableKeepAliveWithInterval(rc, (int)interval)) { 0 0 0
2946 0 SV* msg = sv_2mortal(newSVpv(rc->errstr, 0));
2947 /* the socket still works: hiredis must not treat it as failed */
2948 0 rc->err = saved_err;
2949 0 Copy(saved_errstr, rc->errstr, sizeof(saved_errstr), char);
2950 0 croak_caller("keepalive: %" SVf, SVfARG(msg));
2951 }
2952 }
2953 0 self->keepalive = (int)interval;
2954 }
2955 0 ▶ RETVAL = self->keepalive;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->keepalive; 0 0 0
2956 }
2957 OUTPUT:
2958 RETVAL
2959  
2960 int
2961 prefer_ipv4(EV::Redis self, SV* value = NULL);
2962 CODE:
2963 {
2964 1 ▶ if (NULL != value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != value && SvOK(value)) { 1 0 1
if (NULL != value && SvOK(value)) { 1 0 1
2965 1 self->prefer_ipv4 = SvTRUE(value) ? 1 : 0;
2966 1 ▶ if (self->prefer_ipv4) self->prefer_ipv6 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->prefer_ipv4) self->prefer_ipv6 = 0; 1 0 1
2967 }
2968 1 ▶ RETVAL = self->prefer_ipv4;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->prefer_ipv4; 0 1 1
2969 }
2970 OUTPUT:
2971 RETVAL
2972  
2973 int
2974 prefer_ipv6(EV::Redis self, SV* value = NULL);
2975 CODE:
2976 {
2977 1 ▶ if (NULL != value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != value && SvOK(value)) { 1 0 1
if (NULL != value && SvOK(value)) { 1 0 1
2978 1 self->prefer_ipv6 = SvTRUE(value) ? 1 : 0;
2979 1 ▶ if (self->prefer_ipv6) self->prefer_ipv4 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->prefer_ipv6) self->prefer_ipv4 = 0; 1 0 1
2980 }
2981 1 ▶ RETVAL = self->prefer_ipv6;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->prefer_ipv6; 0 1 1
2982 }
2983 OUTPUT:
2984 RETVAL
2985  
2986 SV*
2987 source_addr(EV::Redis self, SV* value = NULL);
2988 CODE:
2989 {
2990 2 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 2 0 2
2991 2 ▶ if (NULL != self->source_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->source_addr) { 0 2 2
2992 0 Safefree(self->source_addr);
2993 0 self->source_addr = NULL;
2994 }
2995 2 ▶ if (NULL != value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != value && SvOK(value)) { 2 0 2
if (NULL != value && SvOK(value)) { 2 0 2
2996 2 self->source_addr = savepv(c_string(value, "source_addr"));
2997 }
2998 }
2999 1 ▶ if (NULL != self->source_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->source_addr) { 1 0 1
3000 1 RETVAL = newSVpv(self->source_addr, 0);
3001 } else {
3002 0 RETVAL = &PL_sv_undef;
3003 }
3004 }
3005 OUTPUT:
3006 RETVAL
3007  
3008 unsigned int
3009 tcp_user_timeout(EV::Redis self, SV* value = NULL);
3010 CODE:
3011 {
3012 2 ▶ if (NULL != value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != value && SvOK(value)) { 2 0 2
if (NULL != value && SvOK(value)) { 2 0 2
3013 2 IV ms = sv_to_sat_iv(value);
3014 2 validate_timeout_ms(ms, "tcp_user_timeout");
3015 2 self->tcp_user_timeout = (unsigned int)ms;
3016 }
3017 2 ▶ RETVAL = self->tcp_user_timeout;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->tcp_user_timeout; 1 1 2
3018 }
3019 OUTPUT:
3020 RETVAL
3021  
3022 int
3023 cloexec(EV::Redis self, SV* value = NULL);
3024 CODE:
3025 {
3026 0 ▶ if (NULL != value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != value && SvOK(value)) { 0 0 0
if (NULL != value && SvOK(value)) { 0 0 0
3027 0 self->cloexec = SvTRUE(value) ? 1 : 0;
3028 }
3029 0 ▶ RETVAL = self->cloexec;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->cloexec; 0 0 0
3030 }
3031 OUTPUT:
3032 RETVAL
3033  
3034 int
3035 reuseaddr(EV::Redis self, SV* value = NULL);
3036 CODE:
3037 {
3038 0 ▶ if (NULL != value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != value && SvOK(value)) { 0 0 0
if (NULL != value && SvOK(value)) { 0 0 0
3039 0 self->reuseaddr = SvTRUE(value) ? 1 : 0;
3040 }
3041 0 ▶ RETVAL = self->reuseaddr;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->reuseaddr; 0 0 0
3042 }
3043 OUTPUT:
3044 RETVAL
3045  
3046 void
3047 skip_waiting(EV::Redis self);
3048 CODE:
3049 {
3050 0 self->callback_depth++;
3051  
3052 0 ▶ if (self->in_wait_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_wait_cleanup) { 0 0 0
3053 0 self->callback_depth--;
3054 0 check_destroyed(self);
3055 0 return;
3056 }
3057  
3058 0 clear_wait_queue_sv(self, err_skipped);
3059  
3060 0 self->callback_depth--;
3061 0 check_destroyed(self);
3062 }
3063  
3064 void
3065 skip_pending(EV::Redis self);
3066 CODE:
3067 {
3068 ngx_queue_t local_queue;
3069 ngx_queue_t* q;
3070 ngx_queue_t* next;
3071 ev_redis_cb_t* cbt;
3072 /* commands the skipped callbacks issue are not skipped */
3073 0 UV issued_before = self->cb_seq;
3074  
3075 0 self->callback_depth++;
3076  
3077 0 ▶ if (!self->in_wait_cleanup) clear_wait_queue_sv(self, err_skipped);
Branch: 1 branch
BranchTrueFalseTotal
if (!self->in_wait_cleanup) clear_wait_queue_sv(self, err_skipped); 0 0 0
3078  
3079 0 ▶ if (self->in_cb_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_cb_cleanup) { 0 0 0
3080 0 self->callback_depth--;
3081 0 check_destroyed(self);
3082 0 return;
3083 }
3084  
3085 0 self->in_cb_cleanup = 1;
3086  
3087 0 ngx_queue_init(&local_queue);
3088 0 for (q = ngx_queue_head(&self->cb_queue);
3089 0 ▶ q != ngx_queue_sentinel(&self->cb_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->cb_queue); 0 0 0
3090 0 q = next) {
3091 0 next = ngx_queue_next(q);
3092 0 cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
3093 /* the monitor stream is not a pending command; the server has no off switch */
3094 0 ▶ if (cbt->running || cbt->monitor || cbt->seq > issued_before) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (cbt->running || cbt->monitor || cbt->seq > issued_before) continue; 0 0 0
if (cbt->running || cbt->monitor || cbt->seq > issued_before) continue; 0 0 0
if (cbt->running || cbt->monitor || cbt->seq > issued_before) continue; 0 0 0
3095 0 ngx_queue_remove(q);
3096 0 ngx_queue_insert_tail(&local_queue, q);
3097 }
3098  
3099 0 ▶ while (!ngx_queue_empty(&local_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&local_queue)) { 0 0 0
3100 0 ▶ if (self->magic == EV_REDIS_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED) { 0 0 0
3101 0 break;
3102 }
3103  
3104 0 q = ngx_queue_head(&local_queue);
3105 0 cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
3106 0 ngx_queue_remove(q);
3107  
3108 /* before invoking: a reply in a re-entered loop must find it skipped */
3109 0 cbt->skipped = 1;
3110  
3111 /* reply_cb's skipped path may unlink it again */
3112 0 ngx_queue_init(q);
3113 0 ▶ if (!cbt->persist) drop_pending(self, cbt);
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) drop_pending(self, cbt); 0 0 0
3114  
3115 /* take cb first: reply_cb may free cbt while the callback runs */
3116 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
3117 0 SV* cb_to_invoke = cbt->cb;
3118 0 cbt->cb = NULL;
3119  
3120 0 invoke_callback_error(cb_to_invoke, err_skipped);
3121 0 SvREFCNT_dec(cb_to_invoke);
3122 }
3123 }
3124  
3125 0 self->in_cb_cleanup = 0;
3126  
3127 /* skipping releases slots for commands the callbacks issued */
3128 0 ▶ if (self->magic == EV_REDIS_MAGIC) send_next_waiting(self);
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_MAGIC) send_next_waiting(self); 0 0 0
3129  
3130 0 self->callback_depth--;
3131 0 check_destroyed(self);
3132 }
3133  
3134 void
3135 _warn(const char* what, SV* err, SV* msg);
3136 CODE:
3137 2 sv_setpvf(msg, "EV::Redis: exception in %s: %" SVf, what, SVfARG(err));
3138 2 warn("%" SVf, SVfARG(msg));
3139  
3140 int
3141 has_ssl(char* class);
3142 CODE:
3143 {
3144 PERL_UNUSED_VAR(class);
3145 #ifdef EV_REDIS_SSL
3146 8 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 2 6 8
3147 #else
3148 RETVAL = 0;
3149 #endif
3150 }
3151 OUTPUT:
3152 RETVAL
3153  
3154 #ifdef EV_REDIS_SSL
3155  
3156 void
3157 _setup_ssl_context(EV::Redis self, SV* cacert, SV* capath, SV* cert, SV* key, SV* server_name, int verify = 1);
3158 CODE:
3159 {
3160 7 redisSSLContextError ssl_error = REDIS_SSL_CTX_NONE;
3161 redisSSLContext* new_ctx;
3162 redisSSLOptions ssl_opts;
3163  
3164 7 memset(&ssl_opts, 0, sizeof(ssl_opts));
3165 7 ▶ ssl_opts.cacert_filename = (SvOK(cacert)) ? c_string(cacert, "tls_ca") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.cacert_filename = (SvOK(cacert)) ? c_string(cacert, "tls_ca") : NULL; 2 5 7
3166 6 ▶ ssl_opts.capath = (SvOK(capath)) ? c_string(capath, "tls_capath") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.capath = (SvOK(capath)) ? c_string(capath, "tls_capath") : NULL; 1 5 6
3167 6 ▶ ssl_opts.cert_filename = (SvOK(cert)) ? c_string(cert, "tls_cert") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.cert_filename = (SvOK(cert)) ? c_string(cert, "tls_cert") : NULL; 1 5 6
3168 6 ▶ ssl_opts.private_key_filename = (SvOK(key)) ? c_string(key, "tls_key") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.private_key_filename = (SvOK(key)) ? c_string(key, "tls_key") : NULL; 0 6 6
3169 6 ▶ ssl_opts.server_name = (SvOK(server_name)) ? c_string(server_name, "tls_server_name") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.server_name = (SvOK(server_name)) ? c_string(server_name, "tls_server_name") : NULL; 0 6 6
3170 6 ssl_opts.verify_mode = verify ? REDIS_SSL_VERIFY_PEER : REDIS_SSL_VERIFY_NONE;
3171  
3172 /* create first: a failed (eval-trapped) reconfigure must not leave
3173 * ssl_ctx NULL, or the next reconnect goes plaintext */
3174 6 new_ctx = redisCreateSSLContextWithOptions(&ssl_opts, &ssl_error);
3175 6 ▶ if (NULL == new_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == new_ctx) { 2 4 6
3176 2 croak_caller("SSL context creation failed: %s", redisSSLContextGetError(ssl_error));
3177 }
3178  
3179 4 ▶ if (NULL != self->ssl_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ssl_ctx) { 0 4 4
3180 0 redisFreeSSLContext(self->ssl_ctx);
3181 }
3182 4 self->ssl_ctx = new_ctx;
3183 }
3184  
3185 #endif
« deps/hiredis/dict.c ↑ Summary deps/hiredis/sds.h »