Redis.xs

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