Redis.xs

filter: Statementstmt 10.0% 102 / 1013 Branchbran 7.5% 60 / 798 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 8.9% 162 / 1811 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/libev_adapter.h ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5  
6 #include "EVAPI.h"
7  
8 #include "hiredis.h"
9 #include "async.h"
10 #include "libev_adapter.h"
11 #include "ngx-queue.h"
12  
13 #ifdef EV_REDIS_SSL
14 #include "hiredis_ssl.h"
15 #endif
16  
17 typedef struct ev_redis_s ev_redis_t;
18 typedef struct ev_redis_cb_s ev_redis_cb_t;
19 typedef struct ev_redis_wait_s ev_redis_wait_t;
20 typedef struct ev_redis_drain_s ev_redis_drain_t;
21  
22 typedef ev_redis_t* EV__Redis;
23 typedef struct ev_loop* EV__Loop;
24  
25 #define EV_REDIS_MAGIC 0xDEADBEEF
26 #define EV_REDIS_FREED 0xFEEDFACE
27  
28 #define CLEAR_HANDLER(field) \
29 do { if (NULL != (field)) { SvREFCNT_dec(field); (field) = NULL; } } while(0)
30  
31 struct ev_redis_s {
32 unsigned int magic; /* Set to EV_REDIS_MAGIC when alive */
33 struct ev_loop* loop;
34 SV* loop_sv; /* pins the EV::Loop object for the lifetime of self */
35 redisAsyncContext* ac;
36 SV* error_handler;
37 SV* connect_handler;
38 SV* disconnect_handler;
39 SV* push_handler;
40 struct timeval* connect_timeout;
41 struct timeval* command_timeout;
42 ngx_queue_t cb_queue;
43 ngx_queue_t wait_queue;
44 int pending_count;
45 int waiting_count;
46 int max_pending; /* 0 = unlimited */
47 ev_redis_cb_t* current_cb; /* callback currently executing */
48 int resume_waiting_on_reconnect; /* keep waiting queue on disconnect */
49 int waiting_timeout_ms; /* max ms in waiting queue, 0 = unlimited */
50 ev_timer waiting_timer;
51 int waiting_timer_active;
52  
53 /* Reconnect settings */
54 char* host;
55 int port;
56 char* path;
57 int reconnect; /* 0 = disabled, 1 = enabled */
58 int reconnect_delay_ms; /* delay between reconnect attempts */
59 int max_reconnect_attempts; /* 0 = unlimited */
60 int reconnect_attempts; /* current attempt count */
61 ev_timer reconnect_timer;
62 int reconnect_timer_active;
63 int intentional_disconnect; /* set before explicit disconnect() */
64 int priority; /* libev watcher priority, default 0 */
65 int in_cb_cleanup; /* prevent re-entrant cb_queue modification */
66 int in_wait_cleanup; /* prevent re-entrant wait_queue modification */
67 int callback_depth; /* nesting depth of C-level callbacks invoking Perl code */
68 int monitoring; /* MONITOR active on current connection */
69 int keepalive; /* TCP keepalive interval in seconds, 0 = disabled */
70 int prefer_ipv4; /* prefer IPv4 DNS resolution */
71 int prefer_ipv6; /* prefer IPv6 DNS resolution */
72 char* source_addr; /* local address to bind to */
73 unsigned int tcp_user_timeout; /* TCP_USER_TIMEOUT in ms, 0 = OS default */
74 int cloexec; /* set SOCK_CLOEXEC on socket */
75 int reuseaddr; /* set SO_REUSEADDR on socket */
76 ngx_queue_t drain_queue; /* contexts detached from self->ac whose teardown
77 hiredis may complete later; they still hold
78 data == self and privdata pointers to cbts */
79 #ifdef EV_REDIS_SSL
80 redisSSLContext* ssl_ctx;
81 #endif
82 };
83  
84 struct ev_redis_cb_s {
85 SV* cb;
86 redisAsyncContext* ac; /* owning context — cb_queue is shared across
87 connections, and a draining old context still
88 references its cbts as privdata */
89 ngx_queue_t queue;
90 int persist;
91 int skipped;
92 int sub_count; /* subscription channels remaining (for persistent commands) */
93 };
94  
95 struct ev_redis_wait_s {
96 char** argv;
97 size_t* argvlen;
98 int argc;
99 SV* cb;
100 int persist;
101 ngx_queue_t queue;
102 ev_tstamp queued_at;
103 };
104  
105 struct ev_redis_drain_s {
106 redisAsyncContext* ac;
107 ngx_queue_t queue;
108 };
109  
110 /* Shared error strings (initialized in BOOT) */
111 static SV* err_skipped = NULL;
112 static SV* err_waiting_timeout = NULL;
113 static SV* err_disconnected = NULL;
114  
115 /* Check for unsubscribe-family commands. These are persistent (stay in cb_queue)
116 * but hiredis ignores their callbacks — replies go through the subscribe callback. */
117 0 static int is_unsubscribe_command(const char* cmd) {
118 0 char c = cmd[0];
119 0 ▶ if (c == 'u' || c == 'U') return (0 == strcasecmp(cmd, "unsubscribe"));
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'u' || c == 'U') return (0 == strcasecmp(cmd, "unsubscribe")); 0 0 0
if (c == 'u' || c == 'U') return (0 == strcasecmp(cmd, "unsubscribe")); 0 0 0
120 0 ▶ if (c == 'p' || c == 'P') return (0 == strcasecmp(cmd, "punsubscribe"));
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'p' || c == 'P') return (0 == strcasecmp(cmd, "punsubscribe")); 0 0 0
if (c == 'p' || c == 'P') return (0 == strcasecmp(cmd, "punsubscribe")); 0 0 0
121 0 return 0;
122 }
123  
124 /* Refused in command(): hiredis treats SSUBSCRIBE as a plain one-shot
125 * command, so an incoming smessage aborts on assert(REDIS_SUBSCRIBED)
126 * under RESP2 or is misrouted to on_push under RESP3. */
127 0 static int is_shard_pubsub_command(const char* cmd) {
128 0 char c = cmd[0];
129 0 ▶ if (c != 's' && c != 'S') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c != 's' && c != 'S') return 0; 0 0 0
if (c != 's' && c != 'S') return 0; 0 0 0
130 0 ▶ return (0 == strcasecmp(cmd, "ssubscribe")) ||
Branch: 1 branch
BranchTrueFalseTotal
return (0 == strcasecmp(cmd, "ssubscribe")) || 0 0 0
131 0 ▶ (0 == strcasecmp(cmd, "sunsubscribe"));
Branch: 1 branch
BranchTrueFalseTotal
(0 == strcasecmp(cmd, "sunsubscribe")); 0 0 0
132 }
133  
134 0 static int is_persistent_command(const char* cmd) {
135 0 char c = cmd[0];
136  
137 0 ▶ if (c == 's' || c == 'S') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 's' || c == 'S') { 0 0 0
if (c == 's' || c == 'S') { 0 0 0
138 0 return (0 == strcasecmp(cmd, "subscribe"));
139 }
140 0 ▶ if (c == 'u' || c == 'U') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'u' || c == 'U') { 0 0 0
if (c == 'u' || c == 'U') { 0 0 0
141 0 return (0 == strcasecmp(cmd, "unsubscribe"));
142 }
143 0 ▶ if (c == 'p' || c == 'P') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'p' || c == 'P') { 0 0 0
if (c == 'p' || c == 'P') { 0 0 0
144 0 ▶ if (0 == strcasecmp(cmd, "psubscribe")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (0 == strcasecmp(cmd, "psubscribe")) return 1; 0 0 0
145 0 ▶ if (0 == strcasecmp(cmd, "punsubscribe")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (0 == strcasecmp(cmd, "punsubscribe")) return 1; 0 0 0
146 0 return 0;
147 }
148 0 ▶ if (c == 'm' || c == 'M') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 'm' || c == 'M') { 0 0 0
if (c == 'm' || c == 'M') { 0 0 0
149 0 return (0 == strcasecmp(cmd, "monitor"));
150 }
151  
152 0 return 0;
153 }
154  
155 0 static int is_monitor_command(const char* cmd) {
156 0 ▶ 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"); 0 0 0
return (cmd[0] == 'm' || cmd[0] == 'M') && 0 == strcasecmp(cmd, "monitor"); 0 0 0
return (cmd[0] == 'm' || cmd[0] == 'M') && 0 == strcasecmp(cmd, "monitor"); 0 0 0
157 }
158  
159 /* Detect unsubscribe-type replies that indicate end of a subscription channel.
160 * Format: [type_string, channel, remaining_count]
161 * Returns 1 if the reply is an unsubscribe/punsubscribe/sunsubscribe message. */
162 0 static int is_unsub_reply(redisReply* reply) {
163 const char* s;
164  
165 0 ▶ 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 0 0
if (reply->type != REDIS_REPLY_ARRAY && reply->type != REDIS_REPLY_PUSH) return 0; 0 0 0
166 0 ▶ if (reply->elements < 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (reply->elements < 3) return 0; 0 0 0
167 0 ▶ if (NULL == reply->element[0]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == reply->element[0]) return 0; 0 0 0
168 0 ▶ if (reply->element[0]->type != REDIS_REPLY_STRING &&
Branch: 1 branch
BranchTrueFalseTotal
if (reply->element[0]->type != REDIS_REPLY_STRING && 0 0 0
169 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
170  
171 0 s = reply->element[0]->str;
172 0 ▶ 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")); 0 0 0
if (s[0] == 'u' || s[0] == 'U') return (0 == strcasecmp(s, "unsubscribe")); 0 0 0
173 0 ▶ 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")); 0 0 0
if (s[0] == 'p' || s[0] == 'P') return (0 == strcasecmp(s, "punsubscribe")); 0 0 0
174 0 ▶ 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")); 0 0 0
if (s[0] == 's' || s[0] == 'S') return (0 == strcasecmp(s, "sunsubscribe")); 0 0 0
175 0 return 0;
176 }
177  
178 0 static void emit_error(EV__Redis self, SV* error) {
179 0 SV* handler = self->error_handler;
180 0 ▶ if (NULL == handler) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == handler) return; 0 0 0
181 /* pin: the callback may clear its own handler ($r->on_error(undef)),
182 which would otherwise free the CV while call_sv is still running it */
183 0 SvREFCNT_inc_simple_void_NN(handler);
184  
185 0 dSP;
186  
187 0 ENTER;
188 0 SAVETMPS;
189  
190 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
191 0 ▶ XPUSHs(error);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(error); 0 0 0
192 0 PUTBACK;
193  
194 0 call_sv(handler, G_DISCARD | G_EVAL);
195 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
196 0 ▶ warn("EV::Redis: exception in error handler: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Redis: exception in error handler: %s", SvPV_nolen(ERRSV)); 0 0 0
197 }
198  
199 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
200 0 LEAVE;
201 0 SvREFCNT_dec(handler);
202 }
203  
204 0 static void emit_error_str(EV__Redis self, const char* error) {
205 0 ▶ if (NULL == self->error_handler) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->error_handler) return; 0 0 0
206 0 emit_error(self, sv_2mortal(newSVpv(error, 0)));
207 }
208  
209 0 static void invoke_callback_error(SV* cb, SV* error_sv) {
210 0 dSP;
211 0 ENTER;
212 0 SAVETMPS;
213 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
214 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
215 0 PUSHs(&PL_sv_undef);
216 /* copy: error_sv may be a shared READONLY SV, and @_ aliases it — an
217 * in-place modification in the callback would die otherwise */
218 0 PUSHs(sv_mortalcopy(error_sv));
219 0 PUTBACK;
220 0 call_sv(cb, G_DISCARD | G_EVAL);
221 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
222 0 ▶ warn("EV::Redis: exception in command callback: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Redis: exception in command callback: %s", SvPV_nolen(ERRSV)); 0 0 0
223 }
224 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
225 0 LEAVE;
226 0 }
227  
228 /* Check if DESTROY was called during a callback and deferred Safefree.
229 * Call after decrementing callback_depth. Returns 1 if self was freed
230 * (caller MUST NOT access self afterward). */
231 0 static int check_destroyed(EV__Redis self) {
232 0 ▶ if (self->magic == EV_REDIS_FREED &&
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED && 0 0 0
233 0 ▶ self->callback_depth == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
self->callback_depth == 0 && 0 0 0
234 0 ▶ self->current_cb == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
self->current_cb == NULL) { 0 0 0
235 0 Safefree(self);
236 0 return 1;
237 }
238 0 return 0;
239 }
240  
241 /* Free C-allocated fields (used by both PL_dirty and normal DESTROY paths) */
242 6 static void free_c_fields(EV__Redis self) {
243 6 ▶ if (NULL != self->host) { Safefree(self->host); self->host = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->host) { Safefree(self->host); self->host = NULL; } 0 6 6
244 6 ▶ 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 6 6
245 6 ▶ 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; } 0 6 6
246 6 ▶ 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; } 0 6 6
247 6 ▶ 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; } 0 6 6
248 #ifdef EV_REDIS_SSL
249 6 ▶ 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; } 2 4 6
250 #endif
251 6 }
252  
253 6 static void stop_waiting_timer(EV__Redis self) {
254 6 ▶ if (self->waiting_timer_active && NULL != self->loop && !PL_dirty) {
Branch: 3 branches
BranchTrueFalseTotal
if (self->waiting_timer_active && NULL != self->loop && !PL_dirty) { 0 6 6
if (self->waiting_timer_active && NULL != self->loop && !PL_dirty) { 0 0 0
if (self->waiting_timer_active && NULL != self->loop && !PL_dirty) { 0 0 0
255 0 ev_timer_stop(self->loop, &self->waiting_timer);
256 0 self->waiting_timer_active = 0;
257 }
258 6 }
259  
260 6 static void stop_reconnect_timer(EV__Redis self) {
261 6 ▶ if (self->reconnect_timer_active && NULL != self->loop && !PL_dirty) {
Branch: 3 branches
BranchTrueFalseTotal
if (self->reconnect_timer_active && NULL != self->loop && !PL_dirty) { 0 6 6
if (self->reconnect_timer_active && NULL != self->loop && !PL_dirty) { 0 0 0
if (self->reconnect_timer_active && NULL != self->loop && !PL_dirty) { 0 0 0
262 0 ev_timer_stop(self->loop, &self->reconnect_timer);
263 0 self->reconnect_timer_active = 0;
264 }
265 6 }
266  
267 /* Maximum timeout: ~23 days (fits safely in 32-bit calculations) */
268 #define MAX_TIMEOUT_MS 2000000000
269  
270 0 static void validate_timeout_ms(IV ms, const char* name) {
271 0 ▶ if (ms < 0) croak("%s must be non-negative", name);
Branch: 1 branch
BranchTrueFalseTotal
if (ms < 0) croak("%s must be non-negative", name); 0 0 0
272 0 ▶ if (ms > MAX_TIMEOUT_MS) croak("%s too large (max %d ms)", name, MAX_TIMEOUT_MS);
Branch: 1 branch
BranchTrueFalseTotal
if (ms > MAX_TIMEOUT_MS) croak("%s too large (max %d ms)", name, MAX_TIMEOUT_MS); 0 0 0
273 0 }
274  
275 0 static SV* timeout_accessor(struct timeval** tv_ptr, SV* timeout_ms, const char* name) {
276 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
277 0 IV ms = SvIV(timeout_ms);
278 0 validate_timeout_ms(ms, name);
279 0 ▶ if (NULL == *tv_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == *tv_ptr) { 0 0 0
280 0 Newx(*tv_ptr, 1, struct timeval);
281 }
282 0 (*tv_ptr)->tv_sec = (long)(ms / 1000);
283 0 (*tv_ptr)->tv_usec = (long)((ms % 1000) * 1000);
284 }
285  
286 0 ▶ if (NULL != *tv_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != *tv_ptr) { 0 0 0
287 0 return newSViv((IV)(*tv_ptr)->tv_sec * 1000 + (*tv_ptr)->tv_usec / 1000);
288 }
289 0 return &PL_sv_undef;
290 }
291  
292 /* Helper to set/clear a callback handler field.
293 * If called without handler (items == 1), clears the handler.
294 * If called with handler, sets it (or clears if handler is undef/not CODE).
295 * Returns the current handler (with refcount incremented) or undef. */
296 6 static SV* handler_accessor(SV** handler_ptr, SV* handler, int has_handler_arg) {
297 /* Clear existing handler first - both no-arg calls and set calls clear first */
298 6 ▶ CLEAR_HANDLER(*handler_ptr);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(*handler_ptr); 0 6 6
299  
300 /* If a handler argument was provided and it's a valid CODE ref, set it */
301 6 ▶ if (has_handler_arg && NULL != handler && SvOK(handler) && SvROK(handler) &&
Branch: 4 branches
BranchTrueFalseTotal
if (has_handler_arg && NULL != handler && SvOK(handler) && SvROK(handler) && 6 0 6
if (has_handler_arg && NULL != handler && SvOK(handler) && SvROK(handler) && 6 0 6
if (has_handler_arg && NULL != handler && SvOK(handler) && SvROK(handler) && 6 0 6
if (has_handler_arg && NULL != handler && SvOK(handler) && SvROK(handler) && 6 0 6
302 6 ▶ SvTYPE(SvRV(handler)) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(handler)) == SVt_PVCV) { 6 0 6
303 6 *handler_ptr = SvREFCNT_inc(handler);
304 }
305  
306 6 return (NULL != *handler_ptr)
307 6 ? SvREFCNT_inc(*handler_ptr)
308 12 ▶ : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
: &PL_sv_undef; 6 0 6
309 }
310  
311 /* Invoke a no-argument void handler (connect/disconnect), trapping and
312 * warning on any exception. No-op when the handler is unset. The handler SV
313 * is pinned across the call: the callback may clear its own handler (e.g.
314 * $r->on_connect(undef) for one-shot use), which would otherwise drop the last
315 * ref and free the CV while call_sv is still running it. */
316 0 static void call_void_handler(SV* handler, const char* what) {
317 0 dSP;
318 0 ▶ if (NULL == handler) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == handler) return; 0 0 0
319 0 SvREFCNT_inc_simple_void_NN(handler);
320 0 ENTER;
321 0 SAVETMPS;
322 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
323 0 PUTBACK;
324 0 call_sv(handler, G_DISCARD | G_EVAL);
325 0 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 0 0 0
if (SvTRUE(ERRSV)) 0 0 0
326 0 ▶ warn("EV::Redis: exception in %s: %s", what, SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Redis: exception in %s: %s", what, SvPV_nolen(ERRSV)); 0 0 0
327 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
328 0 LEAVE;
329 0 SvREFCNT_dec(handler);
330 }
331  
332 /* Remove and free cb_queue entries, invoking callbacks with error_sv when
333 * given. only_ac limits the sweep to one context's entries (NULL = all) —
334 * the queue is shared, and a draining old context still references its
335 * cbts. Collect-then-drain so user callbacks cannot corrupt the walk. */
336 6 static void remove_cb_queue_sv(EV__Redis self, SV* error_sv,
337 const redisAsyncContext* only_ac) {
338 ngx_queue_t local_queue;
339 ngx_queue_t* q;
340 ngx_queue_t* next;
341 ev_redis_cb_t* cbt;
342  
343 6 ▶ if (self->in_cb_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_cb_cleanup) { 0 6 6
344 0 return;
345 }
346  
347 6 self->in_cb_cleanup = 1;
348  
349 6 ngx_queue_init(&local_queue);
350 6 for (q = ngx_queue_head(&self->cb_queue);
351 6 ▶ q != ngx_queue_sentinel(&self->cb_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->cb_queue); 0 6 6
352 0 q = next) {
353 0 next = ngx_queue_next(q);
354 0 cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
355  
356 0 ▶ if (cbt == self->current_cb) continue; /* owned by in-flight reply_cb */
Branch: 1 branch
BranchTrueFalseTotal
if (cbt == self->current_cb) continue; /* owned by in-flight reply_cb */ 0 0 0
357 0 ▶ if (NULL != only_ac && cbt->ac != only_ac) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != only_ac && cbt->ac != only_ac) continue; 0 0 0
if (NULL != only_ac && cbt->ac != only_ac) continue; 0 0 0
358  
359 0 ngx_queue_remove(q);
360 0 ngx_queue_insert_tail(&local_queue, q);
361 0 ▶ if (!cbt->persist) self->pending_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) self->pending_count--; 0 0 0
362 }
363  
364 6 ▶ while (!ngx_queue_empty(&local_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&local_queue)) { 0 6 6
365 0 q = ngx_queue_head(&local_queue);
366 0 cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
367 0 ngx_queue_remove(q);
368  
369 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
370 0 ▶ if (NULL != error_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != error_sv) { 0 0 0
371 0 invoke_callback_error(cbt->cb, error_sv);
372 }
373 0 SvREFCNT_dec(cbt->cb);
374 }
375 0 Safefree(cbt);
376 }
377  
378 6 self->in_cb_cleanup = 0;
379 }
380  
381 0 static void free_wait_entry(ev_redis_wait_t* wt) {
382 int i;
383 0 ▶ for (i = 0; i < wt->argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < wt->argc; i++) { 0 0 0
384 0 Safefree(wt->argv[i]);
385 }
386 0 Safefree(wt->argv);
387 0 Safefree(wt->argvlen);
388 0 ▶ if (NULL != wt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != wt->cb) { 0 0 0
389 0 SvREFCNT_dec(wt->cb);
390 }
391 0 Safefree(wt);
392 0 }
393  
394 /* Uses in_wait_cleanup flag to prevent re-entrant queue modification. */
395 6 static void clear_wait_queue_sv(EV__Redis self, SV* error_sv) {
396 ngx_queue_t* q;
397 ev_redis_wait_t* wt;
398  
399 6 ▶ if (self->in_wait_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_wait_cleanup) { 0 6 6
400 0 return;
401 }
402  
403 /* Protect against re-entrancy: if a callback invokes skip_waiting() or
404 * skip_pending(), they should no-op since we're already clearing. */
405 6 self->in_wait_cleanup = 1;
406  
407 6 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 0 6 6
408 0 q = ngx_queue_head(&self->wait_queue);
409 0 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
410 0 ngx_queue_remove(q);
411 0 self->waiting_count--;
412  
413 0 ▶ if (NULL != error_sv && NULL != wt->cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != error_sv && NULL != wt->cb) { 0 0 0
if (NULL != error_sv && NULL != wt->cb) { 0 0 0
414 0 invoke_callback_error(wt->cb, error_sv);
415 }
416  
417 0 free_wait_entry(wt);
418 }
419  
420 6 self->in_wait_cleanup = 0;
421 }
422  
423 /* Track a context whose teardown hiredis may complete after self->ac is
424 * nulled (deferred disconnect, failed connect). A tracked context still
425 * holds data == self and privdata pointers into cb_queue. */
426 0 static void track_draining(EV__Redis self, redisAsyncContext* ac) {
427 ev_redis_drain_t* dn;
428 0 Newx(dn, 1, ev_redis_drain_t);
429 0 dn->ac = ac;
430 0 ngx_queue_init(&dn->queue);
431 0 ngx_queue_insert_tail(&self->drain_queue, &dn->queue);
432 0 }
433  
434 /* Stop tracking ac. No-op when not found (e.g. DESTROY already consumed
435 * the list, or the context finished synchronously). */
436 0 static void untrack_draining(EV__Redis self, const redisAsyncContext* ac) {
437 ngx_queue_t* q;
438 ev_redis_drain_t* dn;
439 0 for (q = ngx_queue_head(&self->drain_queue);
440 0 ▶ q != ngx_queue_sentinel(&self->drain_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->drain_queue); 0 0 0
441 0 q = ngx_queue_next(q)) {
442 0 dn = ngx_queue_data(q, ev_redis_drain_t, queue);
443 0 ▶ if (dn->ac == ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (dn->ac == ac) { 0 0 0
444 0 ngx_queue_remove(q);
445 0 Safefree(dn);
446 0 return;
447 }
448 }
449 }
450  
451 /* Runs at the end of every __redisAsyncFree — including teardowns with no
452 * disconnect callback (failed connect, disconnect while connecting).
453 * Untrack the context being freed: it is the tracked node with a detached
454 * adapter (ev.data == NULL; _EL_CLEANUP ran earlier in this teardown,
455 * while draining-but-alive contexts keep their watchers). */
456 0 static void EV__redis_data_cleanup(void* data) {
457 0 EV__Redis self = (EV__Redis)data;
458 ngx_queue_t* q;
459 ev_redis_drain_t* dn;
460  
461 0 ▶ if (NULL == self) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self) return; 0 0 0
462 0 ▶ if (self->magic != EV_REDIS_MAGIC && self->magic != EV_REDIS_FREED) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC && self->magic != EV_REDIS_FREED) return; 0 0 0
if (self->magic != EV_REDIS_MAGIC && self->magic != EV_REDIS_FREED) return; 0 0 0
463  
464 0 for (q = ngx_queue_head(&self->drain_queue);
465 0 ▶ q != ngx_queue_sentinel(&self->drain_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->drain_queue); 0 0 0
466 0 q = ngx_queue_next(q)) {
467 0 dn = ngx_queue_data(q, ev_redis_drain_t, queue);
468 0 ▶ if (NULL == dn->ac->ev.data) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == dn->ac->ev.data) { 0 0 0
469 0 ngx_queue_remove(q);
470 0 Safefree(dn);
471 0 return;
472 }
473 }
474 }
475  
476 /* Forward declarations */
477 static void pre_connect_common(EV__Redis self, redisOptions* opts);
478 static int post_connect_setup(EV__Redis self, const char* err_prefix);
479 static void do_reconnect(EV__Redis self);
480 static void send_next_waiting(EV__Redis self);
481 static void schedule_waiting_timer(EV__Redis self);
482 static void expire_waiting_commands(EV__Redis self);
483 static void schedule_reconnect(EV__Redis self);
484 static void EV__redis_connect_cb(redisAsyncContext* c, int status);
485 static void EV__redis_disconnect_cb(const redisAsyncContext* c, int status);
486 static void EV__redis_push_cb(redisAsyncContext* ac, void* reply_ptr);
487 static SV* EV__redis_decode_reply(redisReply* reply);
488 /* Recursion limit for nested array/map/set replies. Bounds C-stack growth
489 * when decoding maliciously deep replies from an untrusted server. */
490 #define EV_REDIS_MAX_REPLY_DEPTH 512
491 static SV* decode_reply_depth(redisReply* reply, int depth);
492  
493 0 static void clear_connection_params(EV__Redis self) {
494 0 ▶ if (NULL != self->host) { Safefree(self->host); self->host = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->host) { Safefree(self->host); self->host = NULL; } 0 0 0
495 0 ▶ 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 0 0
496 0 }
497  
498 /* C-entry callbacks run with no Perl scope above them — without their own
499 * ENTER/SAVETMPS, error-path mortals would pile up until EV::run returns. */
500  
501 0 static void reconnect_timer_cb(EV_P_ ev_timer* w, int revents) {
502 0 EV__Redis self = (EV__Redis)w->data;
503  
504 (void)loop;
505 (void)revents;
506  
507 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
508  
509 0 ENTER;
510 0 SAVETMPS;
511 0 self->reconnect_timer_active = 0;
512 0 self->callback_depth++;
513 0 do_reconnect(self);
514 0 self->callback_depth--;
515 0 check_destroyed(self);
516 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
517 0 LEAVE;
518 }
519  
520 0 static void schedule_reconnect(EV__Redis self) {
521 ev_tstamp delay;
522  
523 0 ▶ if (!self->reconnect) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->reconnect) return; 0 0 0
524 0 ▶ if (self->intentional_disconnect) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->intentional_disconnect) return; 0 0 0
525 0 ▶ if (NULL == self->loop) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) return; 0 0 0
526 0 stop_reconnect_timer(self);
527 0 ▶ if (self->max_reconnect_attempts > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (self->max_reconnect_attempts > 0 && 0 0 0
528 0 ▶ self->reconnect_attempts >= self->max_reconnect_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
self->reconnect_attempts >= self->max_reconnect_attempts) { 0 0 0
529 /* Clear waiting queue that was preserved for reconnect - reconnect has
530 * permanently failed, so these commands will never be sent. */
531 0 clear_wait_queue_sv(self, sv_2mortal(newSVpv("reconnect error: max attempts reached", 0)));
532 0 stop_waiting_timer(self);
533 0 emit_error_str(self, "reconnect error: max attempts reached");
534 0 return;
535 }
536  
537 0 self->reconnect_attempts++;
538 0 delay = self->reconnect_delay_ms / 1000.0;
539  
540 0 ev_timer_init(&self->reconnect_timer, reconnect_timer_cb, delay, 0);
541 0 self->reconnect_timer.data = (void*)self;
542 0 ev_timer_start(self->loop, &self->reconnect_timer);
543 0 self->reconnect_timer_active = 1;
544 }
545  
546 /* Expire waiting commands that have exceeded waiting_timeout.
547 * Uses head-refetch iteration pattern which is safe against re-entrant
548 * queue modification (e.g., if a callback calls skip_waiting). */
549 0 static void expire_waiting_commands(EV__Redis self) {
550 ngx_queue_t* q;
551 ev_redis_wait_t* wt;
552 ev_tstamp now;
553 ev_tstamp timeout;
554  
555 0 now = ev_now(self->loop);
556 /* Capture timeout at start - callbacks may modify self->waiting_timeout_ms
557 * and we need consistent behavior for the entire batch. */
558 0 timeout = self->waiting_timeout_ms / 1000.0;
559  
560 /* Use while loop with re-fetch of head each iteration.
561 * This is safe against re-entrant modifications. */
562 0 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 0 0 0
563 0 q = ngx_queue_head(&self->wait_queue);
564 0 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
565  
566 0 ▶ if (now - wt->queued_at >= timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (now - wt->queued_at >= timeout) { 0 0 0
567 0 ngx_queue_remove(q);
568 0 self->waiting_count--;
569  
570 0 ▶ if (NULL != wt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != wt->cb) { 0 0 0
571 0 invoke_callback_error(wt->cb, err_waiting_timeout);
572 }
573  
574 0 free_wait_entry(wt);
575 }
576 else {
577 /* Queue is FIFO with monotonically increasing queued_at times.
578 * If this entry hasn't expired, neither have any following entries. */
579 0 break;
580 }
581 }
582 0 }
583  
584 0 static void waiting_timer_cb(EV_P_ ev_timer* w, int revents) {
585 0 EV__Redis self = (EV__Redis)w->data;
586  
587 (void)loop;
588 (void)revents;
589  
590 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
591  
592 0 ENTER;
593 0 SAVETMPS;
594 0 self->waiting_timer_active = 0;
595 0 self->callback_depth++;
596 0 expire_waiting_commands(self);
597 0 schedule_waiting_timer(self);
598 0 self->callback_depth--;
599 0 check_destroyed(self);
600 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
601 0 LEAVE;
602 }
603  
604 0 static void schedule_waiting_timer(EV__Redis self) {
605 ngx_queue_t* q;
606 ev_redis_wait_t* wt;
607 ev_tstamp now, expires_at, delay;
608  
609 /* Use helper which includes NULL loop check */
610 0 stop_waiting_timer(self);
611  
612 0 ▶ if (NULL == self->loop) return;
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) return; 0 0 0
613 0 ▶ if (self->waiting_timeout_ms <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timeout_ms <= 0) return; 0 0 0
614 0 ▶ if (ngx_queue_empty(&self->wait_queue)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->wait_queue)) return; 0 0 0
615  
616 0 q = ngx_queue_head(&self->wait_queue);
617 0 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
618  
619 0 now = ev_now(self->loop);
620 0 expires_at = wt->queued_at + self->waiting_timeout_ms / 1000.0;
621 0 delay = expires_at - now;
622 0 ▶ if (delay < 0) delay = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay < 0) delay = 0; 0 0 0
623  
624 0 ev_timer_init(&self->waiting_timer, waiting_timer_cb, delay, 0);
625 0 self->waiting_timer.data = (void*)self;
626 0 ev_timer_start(self->loop, &self->waiting_timer);
627 0 self->waiting_timer_active = 1;
628 }
629  
630 0 static void do_reconnect(EV__Redis self) {
631 redisOptions opts;
632 0 memset(&opts, 0, sizeof(opts));
633  
634 0 ▶ if (NULL == self->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->loop) { 0 0 0
635 /* Object is being destroyed */
636 0 return;
637 }
638  
639 0 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 0 0
640 /* Already connected or connecting */
641 0 return;
642 }
643  
644 0 self->intentional_disconnect = 0;
645 0 self->monitoring = 0;
646 0 pre_connect_common(self, &opts);
647  
648 0 ▶ if (NULL != self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->path) { 0 0 0
649 0 REDIS_OPTIONS_SET_UNIX(&opts, self->path);
650 }
651 0 ▶ else if (NULL != self->host) {
Branch: 1 branch
BranchTrueFalseTotal
else if (NULL != self->host) { 0 0 0
652 0 REDIS_OPTIONS_SET_TCP(&opts, self->host, self->port);
653 }
654 else {
655 0 emit_error_str(self, "reconnect error: no connection parameters");
656 0 return;
657 }
658  
659 0 self->ac = redisAsyncConnectWithOptions(&opts);
660 0 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 0 0 0
661 0 emit_error_str(self, "reconnect error: cannot allocate memory");
662 0 schedule_reconnect(self);
663 0 return;
664 }
665  
666 0 ▶ if (REDIS_OK != post_connect_setup(self, "reconnect error")) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != post_connect_setup(self, "reconnect error")) { 0 0 0
667 0 schedule_reconnect(self);
668 0 return;
669 }
670 }
671  
672 0 static void EV__redis_connect_cb(redisAsyncContext* c, int status) {
673 0 EV__Redis self = (EV__Redis)c->data;
674 int owned;
675  
676 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
677  
678 /* Not owned: disconnect() already detached and tracked c. Tracking it
679 * again would leave a duplicate node dataCleanup can't reap; touching
680 * self state would clobber any replacement connection. */
681 0 owned = (self->ac == c);
682  
683 0 ENTER;
684 0 SAVETMPS;
685 0 self->callback_depth++;
686  
687 0 ▶ if (REDIS_OK != status) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != status) { 0 0 0
688 0 ▶ if (owned) {
Branch: 1 branch
BranchTrueFalseTotal
if (owned) { 0 0 0
689 /* hiredis fires pending reply callbacks and frees c only after
690 * this callback returns; keep c tracked for that whole window
691 * (untracked by dataCleanup, or consumed by DESTROY). */
692 0 track_draining(self, c);
693 0 self->ac = NULL;
694 0 self->monitoring = 0;
695 0 ▶ emit_error_str(self, c->errstr[0] ? c->errstr : "connect failed");
Branch: 1 branch
BranchTrueFalseTotal
emit_error_str(self, c->errstr[0] ? c->errstr : "connect failed"); 0 0 0
696 0 ▶ if (!self->reconnect || !self->resume_waiting_on_reconnect
Branch: 2 branches
BranchTrueFalseTotal
if (!self->reconnect || !self->resume_waiting_on_reconnect 0 0 0
if (!self->reconnect || !self->resume_waiting_on_reconnect 0 0 0
697 0 ▶ || self->intentional_disconnect) {
Branch: 1 branch
BranchTrueFalseTotal
|| self->intentional_disconnect) { 0 0 0
698 0 ▶ clear_wait_queue_sv(self, sv_2mortal(newSVpv(
Branch: 1 branch
BranchTrueFalseTotal
clear_wait_queue_sv(self, sv_2mortal(newSVpv( 0 0 0
699 c->errstr[0] ? c->errstr : "connect failed", 0)));
700 0 stop_waiting_timer(self);
701 }
702 0 schedule_reconnect(self);
703 }
704 }
705 0 ▶ else if (owned) {
Branch: 1 branch
BranchTrueFalseTotal
else if (owned) { 0 0 0
706 0 self->reconnect_attempts = 0;
707  
708 0 call_void_handler(self->connect_handler, "connect handler");
709  
710 0 send_next_waiting(self);
711 }
712  
713 0 self->callback_depth--;
714 0 check_destroyed(self);
715 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
716 0 LEAVE;
717 }
718  
719 0 static void EV__redis_disconnect_cb(const redisAsyncContext* c, int status) {
720 0 EV__Redis self = (EV__Redis)c->data;
721 SV* error_sv;
722 0 int should_reconnect = 0;
723 int was_intentional;
724 int will_reconnect;
725  
726 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
727  
728 /* This context's teardown is completing — stop tracking it (no-op if it
729 * was never deferred or DESTROY already consumed the list). */
730 0 untrack_draining(self, c);
731  
732 /* Stale disconnect callback: user already established a new connection
733 * (e.g., called disconnect() then connect() before the old deferred
734 * disconnect fired). Old pending callbacks were already processed by
735 * reply_cb. Skip all cleanup to avoid clobbering the new connection. */
736 0 ▶ if (self->ac != NULL && self->ac != c) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->ac != NULL && self->ac != c) { 0 0 0
if (self->ac != NULL && self->ac != c) { 0 0 0
737 0 return;
738 }
739  
740 0 was_intentional = self->intentional_disconnect;
741 0 self->intentional_disconnect = 0;
742  
743 0 self->ac = NULL;
744 0 self->monitoring = 0; /* MONITOR does not survive the connection */
745 0 ENTER;
746 0 SAVETMPS;
747 0 self->callback_depth++;
748  
749 0 ▶ if (REDIS_OK == status) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK == status) { 0 0 0
750 0 error_sv = err_disconnected;
751 }
752 else {
753 0 ▶ error_sv = sv_2mortal(newSVpv(
Branch: 1 branch
BranchTrueFalseTotal
error_sv = sv_2mortal(newSVpv( 0 0 0
754 c->errstr[0] ? c->errstr : "disconnected", 0));
755 0 ▶ emit_error_str(self, c->errstr[0] ? c->errstr : "disconnected");
Branch: 1 branch
BranchTrueFalseTotal
emit_error_str(self, c->errstr[0] ? c->errstr : "disconnected"); 0 0 0
756 0 ▶ if (!was_intentional) {
Branch: 1 branch
BranchTrueFalseTotal
if (!was_intentional) { 0 0 0
757 0 should_reconnect = 1;
758 }
759 }
760  
761 0 ▶ if (NULL != self->disconnect_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->disconnect_handler) { 0 0 0
762 0 call_void_handler(self->disconnect_handler, "disconnect handler");
763  
764 /* Re-check: user's handler might have called connect() or reconnect()
765 * establishing a new ac. If so, skip clearing cb_queue to avoid
766 * freeing new commands; still honour resume_waiting_on_reconnect=0
767 * by clearing the old wait queue (its entries belong to the prior
768 * connection, not the new one). */
769 0 ▶ if (self->ac != NULL && self->ac != c) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->ac != NULL && self->ac != c) { 0 0 0
if (self->ac != NULL && self->ac != c) { 0 0 0
770 0 ▶ if (!self->resume_waiting_on_reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->resume_waiting_on_reconnect) { 0 0 0
771 0 clear_wait_queue_sv(self, error_sv);
772 0 stop_waiting_timer(self);
773 }
774 0 self->callback_depth--;
775 0 check_destroyed(self);
776 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
777 0 LEAVE;
778 0 return;
779 }
780 }
781  
782 0 remove_cb_queue_sv(self, error_sv, c);
783  
784 0 ▶ will_reconnect = should_reconnect && !self->intentional_disconnect && self->reconnect;
Branch: 3 branches
BranchTrueFalseTotal
will_reconnect = should_reconnect && !self->intentional_disconnect && self->reconnect; 0 0 0
will_reconnect = should_reconnect && !self->intentional_disconnect && self->reconnect; 0 0 0
will_reconnect = should_reconnect && !self->intentional_disconnect && self->reconnect; 0 0 0
785 0 ▶ if (!self->resume_waiting_on_reconnect || was_intentional || !will_reconnect) {
Branch: 3 branches
BranchTrueFalseTotal
if (!self->resume_waiting_on_reconnect || was_intentional || !will_reconnect) { 0 0 0
if (!self->resume_waiting_on_reconnect || was_intentional || !will_reconnect) { 0 0 0
if (!self->resume_waiting_on_reconnect || was_intentional || !will_reconnect) { 0 0 0
786 0 clear_wait_queue_sv(self, error_sv);
787 0 stop_waiting_timer(self);
788 }
789  
790 0 ▶ if (will_reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (will_reconnect) { 0 0 0
791 0 schedule_reconnect(self);
792 }
793  
794 0 self->callback_depth--;
795 0 check_destroyed(self);
796 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
797 0 LEAVE;
798 }
799  
800 0 static void EV__redis_push_cb(redisAsyncContext* ac, void* reply_ptr) {
801 0 EV__Redis self = (EV__Redis)ac->data;
802 0 redisReply* reply = (redisReply*)reply_ptr;
803  
804 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
805 0 SV* handler = self->push_handler;
806 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
807 /* pin: the callback may clear its own handler ($r->on_push(undef)) */
808 0 SvREFCNT_inc_simple_void_NN(handler);
809  
810 0 self->callback_depth++;
811  
812 {
813 0 dSP;
814  
815 0 ENTER;
816 0 SAVETMPS;
817  
818 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
819 0 ▶ XPUSHs(sv_2mortal(EV__redis_decode_reply(reply)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(EV__redis_decode_reply(reply))); 0 0 0
820 0 PUTBACK;
821  
822 0 call_sv(handler, G_DISCARD | G_EVAL);
823 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
824 0 ▶ warn("EV::Redis: exception in push handler: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Redis: exception in push handler: %s", SvPV_nolen(ERRSV)); 0 0 0
825 }
826  
827 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
828 0 LEAVE;
829 }
830  
831 0 SvREFCNT_dec(handler);
832 0 self->callback_depth--;
833 0 check_destroyed(self);
834 }
835  
836 0 static void pre_connect_common(EV__Redis self, redisOptions* opts) {
837 0 ▶ if (NULL != self->connect_timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->connect_timeout) { 0 0 0
838 0 opts->connect_timeout = self->connect_timeout;
839 }
840 0 ▶ if (NULL != self->command_timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->command_timeout) { 0 0 0
841 0 opts->command_timeout = self->command_timeout;
842 }
843 0 ▶ if (self->prefer_ipv4) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->prefer_ipv4) { 0 0 0
844 0 opts->options |= REDIS_OPT_PREFER_IPV4;
845 }
846 0 ▶ else if (self->prefer_ipv6) {
Branch: 1 branch
BranchTrueFalseTotal
else if (self->prefer_ipv6) { 0 0 0
847 0 opts->options |= REDIS_OPT_PREFER_IPV6;
848 }
849 0 ▶ if (self->cloexec) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cloexec) { 0 0 0
850 0 opts->options |= REDIS_OPT_SET_SOCK_CLOEXEC;
851 }
852 0 ▶ if (self->reuseaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reuseaddr) { 0 0 0
853 0 opts->options |= REDIS_OPT_REUSEADDR;
854 }
855 0 ▶ if (NULL != self->source_addr && NULL == self->path) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL != self->source_addr && NULL == self->path) { 0 0 0
if (NULL != self->source_addr && NULL == self->path) { 0 0 0
856 0 opts->endpoint.tcp.source_addr = self->source_addr;
857 }
858 0 }
859  
860 /* Set up a newly allocated redisAsyncContext: SSL, keepalive, libev, callbacks.
861 * On failure: frees ac, nulls self->ac, emits error with err_prefix. */
862 0 static int post_connect_setup(EV__Redis self, const char* err_prefix) {
863 0 self->ac->data = (void*)self;
864 0 self->ac->dataCleanup = EV__redis_data_cleanup;
865  
866 #ifdef EV_REDIS_SSL
867 0 ▶ if (NULL != self->ssl_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ssl_ctx) { 0 0 0
868 0 ▶ if (REDIS_OK != redisInitiateSSLWithContext(&self->ac->c, self->ssl_ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != redisInitiateSSLWithContext(&self->ac->c, self->ssl_ctx)) { 0 0 0
869 0 ▶ SV* err = sv_2mortal(newSVpvf("%s: SSL initiation failed: %s",
Branch: 1 branch
BranchTrueFalseTotal
SV* err = sv_2mortal(newSVpvf("%s: SSL initiation failed: %s", 0 0 0
870 err_prefix, self->ac->errstr[0] ? self->ac->errstr : "unknown error"));
871 0 redisAsyncFree(self->ac);
872 0 self->ac = NULL;
873 0 emit_error(self, err);
874 0 return REDIS_ERR;
875 }
876 }
877 #endif
878  
879 0 ▶ if (self->keepalive > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->keepalive > 0) { 0 0 0
880 0 redisEnableKeepAliveWithInterval(&self->ac->c, self->keepalive);
881 }
882 0 ▶ if (self->tcp_user_timeout > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->tcp_user_timeout > 0) { 0 0 0
883 0 redisSetTcpUserTimeout(&self->ac->c, self->tcp_user_timeout);
884 }
885  
886 0 ▶ if (REDIS_OK != redisLibevAttach(self->loop, self->ac)) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != redisLibevAttach(self->loop, self->ac)) { 0 0 0
887 0 SV* err = sv_2mortal(newSVpvf("%s: cannot attach libev", err_prefix));
888 0 redisAsyncFree(self->ac);
889 0 self->ac = NULL;
890 0 emit_error(self, err);
891 0 return REDIS_ERR;
892 }
893  
894 0 ▶ if (self->priority != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->priority != 0) { 0 0 0
895 0 redisLibevSetPriority(self->ac, self->priority);
896 }
897  
898 0 redisAsyncSetConnectCallbackNC(self->ac, EV__redis_connect_cb);
899 0 redisAsyncSetDisconnectCallback(self->ac, EV__redis_disconnect_cb);
900 0 ▶ if (NULL != self->push_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->push_handler) { 0 0 0
901 0 redisAsyncSetPushCallback(self->ac, EV__redis_push_cb);
902 }
903  
904 0 ▶ if (self->ac->err) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ac->err) { 0 0 0
905 0 SV* err = sv_2mortal(newSVpvf("%s: %s", err_prefix, self->ac->errstr));
906 0 redisAsyncFree(self->ac);
907 0 self->ac = NULL;
908 0 emit_error(self, err);
909 0 return REDIS_ERR;
910 }
911  
912 0 return REDIS_OK;
913 }
914  
915 0 static SV* decode_reply_depth(redisReply* reply, int depth) {
916 SV* res;
917  
918 0 switch (reply->type) {
919 0 case REDIS_REPLY_STRING:
920 case REDIS_REPLY_ERROR:
921 case REDIS_REPLY_STATUS:
922 case REDIS_REPLY_BIGNUM:
923 case REDIS_REPLY_VERB:
924 0 res = newSVpvn(reply->str, reply->len);
925 0 break;
926  
927 0 case REDIS_REPLY_INTEGER:
928 0 res = newSViv(reply->integer);
929 0 break;
930  
931 0 case REDIS_REPLY_DOUBLE:
932 0 res = newSVnv(reply->dval);
933 0 break;
934  
935 0 case REDIS_REPLY_BOOL:
936 0 res = newSViv(reply->integer ? 1 : 0);
937 0 break;
938  
939 0 case REDIS_REPLY_NIL:
940 0 res = newSV(0);
941 0 break;
942  
943 0 case REDIS_REPLY_ARRAY:
944 case REDIS_REPLY_MAP:
945 case REDIS_REPLY_SET:
946 case REDIS_REPLY_ATTR:
947 case REDIS_REPLY_PUSH: {
948 0 AV* av = newAV();
949 size_t i;
950 0 ▶ if (depth >= EV_REDIS_MAX_REPLY_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= EV_REDIS_MAX_REPLY_DEPTH) { 0 0 0
951 /* Stop recursing: an empty array placeholder bounds C-stack
952 * usage against a hostile server replying with deep nesting. */
953 0 res = newRV_noinc((SV*)av);
954 0 break;
955 }
956 0 ▶ if (reply->elements > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (reply->elements > 0) { 0 0 0
957 0 av_extend(av, (SSize_t)(reply->elements - 1));
958 0 ▶ for (i = 0; i < reply->elements; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < reply->elements; i++) { 0 0 0
959 0 ▶ if (NULL != reply->element[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != reply->element[i]) { 0 0 0
960 0 av_push(av, decode_reply_depth(reply->element[i], depth + 1));
961 }
962 else {
963 0 av_push(av, newSV(0));
964 }
965 }
966 }
967 0 res = newRV_noinc((SV*)av);
968 0 break;
969 }
970  
971 0 default:
972 0 res = newSV(0);
973 0 break;
974 }
975  
976 0 return res;
977 }
978  
979 0 static SV* EV__redis_decode_reply(redisReply* reply) {
980 0 return decode_reply_depth(reply, 0);
981 }
982  
983 0 static void EV__redis_reply_cb_body(redisAsyncContext* c, void* reply, void* privdata) {
984 0 EV__Redis self = (EV__Redis)c->data;
985 ev_redis_cb_t* cbt;
986 SV* sv_reply;
987 SV* sv_err;
988  
989 0 cbt = (ev_redis_cb_t*)privdata;
990  
991 0 ▶ if (cbt->skipped) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->skipped) { 0 0 0
992 0 ▶ if (!cbt->persist || NULL == reply) {
Branch: 2 branches
BranchTrueFalseTotal
if (!cbt->persist || NULL == reply) { 0 0 0
if (!cbt->persist || NULL == reply) { 0 0 0
993 /* Multi-channel persistent: hiredis fires once per channel with
994 * same cbt. Decrement sub_count, free only on last call. */
995 0 ▶ if (cbt->persist && NULL == reply && cbt->sub_count > 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (cbt->persist && NULL == reply && cbt->sub_count > 1) { 0 0 0
if (cbt->persist && NULL == reply && cbt->sub_count > 1) { 0 0 0
if (cbt->persist && NULL == reply && cbt->sub_count > 1) { 0 0 0
996 0 cbt->sub_count--;
997 0 return;
998 }
999 0 Safefree(cbt);
1000 }
1001 0 ▶ else if (cbt->persist && reply != NULL && is_unsub_reply((redisReply*)reply)) {
Branch: 3 branches
BranchTrueFalseTotal
else if (cbt->persist && reply != NULL && is_unsub_reply((redisReply*)reply)) { 0 0 0
else if (cbt->persist && reply != NULL && is_unsub_reply((redisReply*)reply)) { 0 0 0
else if (cbt->persist && reply != NULL && is_unsub_reply((redisReply*)reply)) { 0 0 0
1002 0 cbt->sub_count--;
1003 0 ▶ if (cbt->sub_count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->sub_count <= 0) { 0 0 0
1004 0 Safefree(cbt);
1005 }
1006 }
1007 0 return;
1008 }
1009  
1010 /* self is NULL when DESTROY nulled ac->data (deferred free inside
1011 * REDIS_IN_CALLBACK) or during PL_dirty. Still invoke the callback
1012 * with a disconnect error so users can clean up resources. The hiredis
1013 * context (c) is still alive here — safe to read c->errstr.
1014 * cb may be NULL during PL_dirty where we pre-null it.
1015 * For persistent commands (multi-channel subscribe), hiredis fires
1016 * reply_cb once per channel with the same cbt. Invoke the callback
1017 * only once (null cb after), use sub_count to track when to free. */
1018 0 ▶ if (self == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self == NULL) { 0 0 0
1019 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
1020 0 invoke_callback_error(cbt->cb,
1021 0 ▶ sv_2mortal(newSVpv(c->errstr[0] ? c->errstr : "disconnected", 0)));
Branch: 1 branch
BranchTrueFalseTotal
sv_2mortal(newSVpv(c->errstr[0] ? c->errstr : "disconnected", 0))); 0 0 0
1022 0 SvREFCNT_dec(cbt->cb);
1023 0 cbt->cb = NULL;
1024 }
1025 0 ▶ if (cbt->persist && reply == NULL && cbt->sub_count > 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (cbt->persist && reply == NULL && cbt->sub_count > 1) { 0 0 0
if (cbt->persist && reply == NULL && cbt->sub_count > 1) { 0 0 0
if (cbt->persist && reply == NULL && cbt->sub_count > 1) { 0 0 0
1026 0 cbt->sub_count--;
1027 0 return;
1028 }
1029 0 Safefree(cbt);
1030 0 return;
1031 }
1032  
1033 /* Freed during DESTROY: invoke with an error, no self->field access.
1034 * Persistent teardown fires once per channel with the same cbt — free
1035 * on the last one (the skipped cb_queue sweep won't). */
1036 0 ▶ if (self->magic == EV_REDIS_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED) { 0 0 0
1037 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
1038 0 self->callback_depth++;
1039 0 ▶ invoke_callback_error(cbt->cb, sv_2mortal(newSVpv(c->errstr[0] ? c->errstr : "disconnected", 0)));
Branch: 1 branch
BranchTrueFalseTotal
invoke_callback_error(cbt->cb, sv_2mortal(newSVpv(c->errstr[0] ? c->errstr : "disconnected", 0))); 0 0 0
1040 0 self->callback_depth--;
1041 0 SvREFCNT_dec(cbt->cb);
1042 0 cbt->cb = NULL;
1043 }
1044 0 ▶ if (!cbt->persist) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) { 0 0 0
1045 0 ngx_queue_remove(&cbt->queue);
1046 0 Safefree(cbt);
1047 }
1048 0 ▶ else if (NULL == reply) {
Branch: 1 branch
BranchTrueFalseTotal
else if (NULL == reply) { 0 0 0
1049 0 ▶ if (cbt->sub_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->sub_count > 1) { 0 0 0
1050 0 cbt->sub_count--;
1051 }
1052 else {
1053 0 ngx_queue_remove(&cbt->queue);
1054 0 Safefree(cbt);
1055 }
1056 }
1057 0 check_destroyed(self);
1058 0 return;
1059 }
1060  
1061 /* Unknown magic - memory corruption, skip.
1062 * Don't touch queue pointers (self's memory may be garbage).
1063 * Always decrement refcount since callback will never be invoked again. */
1064 0 ▶ if (self->magic != EV_REDIS_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) { 0 0 0
1065 0 ▶ if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb); 0 0 0
1066 0 Safefree(cbt);
1067 0 return;
1068 }
1069  
1070 0 self->current_cb = cbt;
1071 0 self->callback_depth++;
1072  
1073 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
1074 0 ▶ if (NULL == reply) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == reply) { 0 0 0
1075 0 ▶ sv_err = sv_2mortal(newSVpv(
Branch: 1 branch
BranchTrueFalseTotal
sv_err = sv_2mortal(newSVpv( 0 0 0
1076 c->errstr[0] ? c->errstr : "disconnected", 0));
1077 0 invoke_callback_error(cbt->cb, sv_err);
1078 }
1079 else {
1080 0 dSP;
1081  
1082 0 ENTER;
1083 0 SAVETMPS;
1084  
1085 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1086 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
1087 0 sv_reply = sv_2mortal(EV__redis_decode_reply((redisReply*)reply));
1088 0 ▶ if (((redisReply*)reply)->type == REDIS_REPLY_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (((redisReply*)reply)->type == REDIS_REPLY_ERROR) { 0 0 0
1089 0 PUSHs(&PL_sv_undef);
1090 0 PUSHs(sv_reply);
1091 }
1092 else {
1093 0 PUSHs(sv_reply);
1094 }
1095 0 PUTBACK;
1096  
1097 0 call_sv(cbt->cb, G_DISCARD | G_EVAL);
1098 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
1099 0 ▶ warn("EV::Redis: exception in command callback: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Redis: exception in command callback: %s", SvPV_nolen(ERRSV)); 0 0 0
1100 }
1101  
1102 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1103 0 LEAVE;
1104 }
1105 }
1106  
1107 0 self->callback_depth--;
1108 0 self->current_cb = NULL;
1109  
1110 /* If DESTROY was called during our callback (e.g., user undef'd $redis),
1111 * self->magic is EV_REDIS_FREED but self is still valid (DESTROY defers
1112 * Safefree when callback_depth > 0). Complete cleanup here.
1113 * For persistent commands (multi-channel subscribe), hiredis will fire
1114 * reply_cb again for remaining channels via __redisAsyncFree. Null the
1115 * callback to prevent double invocation, but leave cbt alive so those
1116 * later calls see it and can track sub_count for proper cleanup. */
1117 0 ▶ if (self->magic == EV_REDIS_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED) { 0 0 0
1118 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
1119 0 SvREFCNT_dec(cbt->cb);
1120 0 cbt->cb = NULL;
1121 }
1122 0 ▶ if (!cbt->persist) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) { 0 0 0
1123 0 Safefree(cbt);
1124 }
1125 0 check_destroyed(self);
1126 0 return;
1127 }
1128  
1129 0 ▶ if (cbt->skipped) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->skipped) { 0 0 0
1130 /* Defensive check: handles edge case where callback is marked skipped
1131 * during its own execution (e.g., via reentrant event loop where a
1132 * nested callback overwrites current_cb, allowing skip_pending to
1133 * process this callback). ngx_queue_remove is safe here due to
1134 * ngx_queue_init in skip_pending. Don't decrement pending_count since
1135 * skip_pending already did when it set skipped=1. */
1136 0 ngx_queue_remove(&cbt->queue);
1137 /* For persistent commands (e.g., SUBSCRIBE), hiredis fires reply_cb
1138 * once per subscribed channel during disconnect. Only free cbt on the
1139 * last channel to prevent use-after-free. */
1140 0 ▶ if (cbt->persist && cbt->sub_count > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->persist && cbt->sub_count > 1) { 0 0 0
if (cbt->persist && cbt->sub_count > 1) { 0 0 0
1141 0 cbt->sub_count--;
1142 0 return;
1143 }
1144 0 Safefree(cbt);
1145 0 self->callback_depth++;
1146 0 send_next_waiting(self);
1147 0 self->callback_depth--;
1148 0 check_destroyed(self);
1149 0 return;
1150 }
1151  
1152 /* Detect end of persistent subscription: when all channels from a
1153 * SUBSCRIBE command have been unsubscribed, hiredis removes its internal
1154 * callback entry. Clean up our cbt to prevent orphaned queue entries. */
1155 0 ▶ if (cbt->persist && reply != NULL && is_unsub_reply((redisReply*)reply)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cbt->persist && reply != NULL && is_unsub_reply((redisReply*)reply)) { 0 0 0
if (cbt->persist && reply != NULL && is_unsub_reply((redisReply*)reply)) { 0 0 0
if (cbt->persist && reply != NULL && is_unsub_reply((redisReply*)reply)) { 0 0 0
1156 0 cbt->sub_count--;
1157 0 ▶ if (cbt->sub_count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->sub_count <= 0) { 0 0 0
1158 /* All channels unsubscribed — persistent commands are not counted
1159 * in pending_count, so don't decrement it. */
1160 0 ngx_queue_remove(&cbt->queue);
1161 0 self->callback_depth++;
1162 0 ▶ if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb); 0 0 0
1163 0 Safefree(cbt);
1164 0 self->callback_depth--;
1165 0 check_destroyed(self);
1166 0 return;
1167 }
1168 }
1169  
1170 /* Connection teardown with active subscription: hiredis fires reply_cb
1171 * once per subscribed channel (from dict iteration in __redisAsyncFree).
1172 * Track sub_count and remove from queue on last channel to prevent
1173 * disconnect_cb's remove_cb_queue_sv from invoking the callback again. */
1174 0 ▶ if (cbt->persist && NULL == reply) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->persist && NULL == reply) { 0 0 0
if (cbt->persist && NULL == reply) { 0 0 0
1175 0 ▶ if (cbt->sub_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->sub_count > 1) { 0 0 0
1176 0 cbt->sub_count--;
1177 } else {
1178 0 ngx_queue_remove(&cbt->queue);
1179 0 self->callback_depth++;
1180 0 ▶ if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb); 0 0 0
1181 0 Safefree(cbt);
1182 0 self->callback_depth--;
1183 0 check_destroyed(self);
1184 }
1185 0 return;
1186 }
1187  
1188 0 ▶ if (0 == cbt->persist) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == cbt->persist) { 0 0 0
1189 /* Remove from queue BEFORE SvREFCNT_dec. The SvREFCNT_dec may free a
1190 * closure that holds the last reference to this object, triggering
1191 * DESTROY. If cbt is still in the queue, DESTROY's remove_cb_queue_sv
1192 * would double-free it. Wrapping in callback_depth defers DESTROY's
1193 * Safefree(self) so we can safely access self afterward. */
1194 0 ngx_queue_remove(&cbt->queue);
1195 0 self->pending_count--;
1196 0 self->callback_depth++;
1197 0 ▶ if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb); 0 0 0
1198 0 Safefree(cbt);
1199 /* Don't drain waiting queue when reply is NULL (connection dying) —
1200 * disconnect_cb will handle reconnect and wait queue preservation. */
1201 0 ▶ if (reply != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (reply != NULL) { 0 0 0
1202 0 send_next_waiting(self);
1203 }
1204 0 self->callback_depth--;
1205 0 check_destroyed(self);
1206 }
1207 }
1208  
1209 /* Scope wrapper — reply_cb_body has many exit paths. */
1210 0 static void EV__redis_reply_cb(redisAsyncContext* c, void* reply, void* privdata) {
1211 0 ENTER;
1212 0 SAVETMPS;
1213 0 EV__redis_reply_cb_body(c, reply, privdata);
1214 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1215 0 LEAVE;
1216 0 }
1217  
1218 /* Submit a cbt (already in cb_queue) to Redis. On failure, removes cbt from
1219 * queue, invokes error callback, and frees cbt. Returns REDIS_OK or REDIS_ERR. */
1220 0 static int submit_to_redis(EV__Redis self, ev_redis_cb_t* cbt,
1221 int argc, const char** argv, const size_t* argvlen)
1222 {
1223 0 redisCallbackFn* fn = EV__redis_reply_cb;
1224 0 void* privdata = (void*)cbt;
1225 0 const char* cmd = argv[0];
1226  
1227 0 cbt->ac = self->ac;
1228  
1229 /* Hiredis does not store callbacks for unsubscribe commands — replies are
1230 * routed through the original subscribe callback. Pass NULL so hiredis
1231 * doesn't hold a dangling reference; we clean up our cbt below. */
1232 0 ▶ if (cbt->persist && is_unsubscribe_command(cmd)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->persist && is_unsubscribe_command(cmd)) { 0 0 0
if (cbt->persist && is_unsubscribe_command(cmd)) { 0 0 0
1233 0 fn = NULL;
1234 0 privdata = NULL;
1235 }
1236  
1237 0 int r = redisAsyncCommandArgv(
1238 self->ac, fn, privdata,
1239 argc, argv, argvlen
1240 );
1241  
1242 0 ▶ if (REDIS_OK != r) {
Branch: 1 branch
BranchTrueFalseTotal
if (REDIS_OK != r) { 0 0 0
1243 0 ngx_queue_remove(&cbt->queue);
1244 0 ▶ if (!cbt->persist) self->pending_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) self->pending_count--; 0 0 0
1245  
1246 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
1247 0 ▶ invoke_callback_error(cbt->cb, sv_2mortal(newSVpv(
Branch: 2 branches
BranchTrueFalseTotal
invoke_callback_error(cbt->cb, sv_2mortal(newSVpv( 0 0 0
invoke_callback_error(cbt->cb, sv_2mortal(newSVpv( 0 0 0
1248 (self->ac && self->ac->errstr[0]) ? self->ac->errstr : "command failed", 0)));
1249 0 SvREFCNT_dec(cbt->cb);
1250 }
1251 0 Safefree(cbt);
1252 0 ▶ } else if (fn == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (fn == NULL) { 0 0 0
1253 /* Successfully sent an unsubscribe command. Since hiredis won't
1254 * call us back, we must clean up our tracking 'cbt' now. */
1255 0 ngx_queue_remove(&cbt->queue);
1256 0 ▶ if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) SvREFCNT_dec(cbt->cb); 0 0 0
1257 0 Safefree(cbt);
1258 }
1259  
1260 0 return r;
1261 }
1262  
1263 /* Send waiting commands to Redis. Uses iterative loop instead of recursion
1264 * to avoid stack overflow when many commands fail consecutively. */
1265 0 static void send_next_waiting(EV__Redis self) {
1266 ngx_queue_t* q;
1267 ev_redis_wait_t* wt;
1268 ev_redis_cb_t* cbt;
1269  
1270 while (1) {
1271 /* Check preconditions each iteration - they may change after callbacks */
1272 0 ▶ if (NULL == self->ac || self->intentional_disconnect) return;
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self->ac || self->intentional_disconnect) return; 0 0 0
if (NULL == self->ac || self->intentional_disconnect) return; 0 0 0
1273 0 ▶ if (ngx_queue_empty(&self->wait_queue)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->wait_queue)) return; 0 0 0
1274 0 ▶ if (self->max_pending > 0 && self->pending_count >= self->max_pending) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->max_pending > 0 && self->pending_count >= self->max_pending) return; 0 0 0
if (self->max_pending > 0 && self->pending_count >= self->max_pending) return; 0 0 0
1275  
1276 0 q = ngx_queue_head(&self->wait_queue);
1277 0 wt = ngx_queue_data(q, ev_redis_wait_t, queue);
1278 0 ngx_queue_remove(q);
1279 0 self->waiting_count--;
1280  
1281 0 Newx(cbt, 1, ev_redis_cb_t);
1282 0 cbt->cb = wt->cb;
1283 0 wt->cb = NULL;
1284 0 cbt->skipped = 0;
1285 0 cbt->persist = wt->persist;
1286 0 ▶ cbt->sub_count = wt->persist ? wt->argc - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
cbt->sub_count = wt->persist ? wt->argc - 1 : 0; 0 0 0
1287 0 ngx_queue_init(&cbt->queue);
1288 0 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
1289 0 ▶ if (!cbt->persist) self->pending_count++;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) self->pending_count++; 0 0 0
1290  
1291 /* Ignore submit_to_redis return: on failure it invokes cbt's error
1292 * callback and frees cbt, so the loop continues to try the next entry. */
1293 0 (void)submit_to_redis(self, cbt, wt->argc,
1294 0 (const char**)wt->argv, wt->argvlen);
1295 0 free_wait_entry(wt);
1296 }
1297 }
1298  
1299 MODULE = EV::Redis PACKAGE = EV::Redis
1300  
1301 BOOT:
1302 {
1303 30 ▶ I_EV_API("EV::Redis");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::Redis"); 0 30 30
I_EV_API("EV::Redis"); 30 0 30
I_EV_API("EV::Redis"); 0 30 30
1304  
1305 /* Initialize shared error strings */
1306 30 err_skipped = newSVpvs_share("skipped");
1307 30 SvREADONLY_on(err_skipped);
1308  
1309 30 err_waiting_timeout = newSVpvs_share("waiting timeout");
1310 30 SvREADONLY_on(err_waiting_timeout);
1311  
1312 30 err_disconnected = newSVpvs_share("disconnected");
1313 30 SvREADONLY_on(err_disconnected);
1314 #ifdef EV_REDIS_SSL
1315 30 redisInitOpenSSL();
1316 #endif
1317 }
1318  
1319 EV::Redis
1320 _new(char* class, EV::Loop loop);
1321 CODE:
1322 {
1323 PERL_UNUSED_VAR(class);
1324 6 Newxz(RETVAL, 1, ev_redis_t);
1325 6 RETVAL->magic = EV_REDIS_MAGIC;
1326 6 ngx_queue_init(&RETVAL->cb_queue);
1327 6 ngx_queue_init(&RETVAL->wait_queue);
1328 6 ngx_queue_init(&RETVAL->drain_queue);
1329 6 RETVAL->loop = loop;
1330 /* pin: a dropped non-default loop would free the C loop under us */
1331 6 RETVAL->loop_sv = SvREFCNT_inc(ST(1));
1332 6 RETVAL->cloexec = 1;
1333 }
1334 OUTPUT:
1335 RETVAL
1336  
1337 void
1338 DESTROY(EV::Redis self);
1339 CODE:
1340 {
1341 redisAsyncContext* ac_to_free;
1342 ngx_queue_t* dq;
1343 ev_redis_drain_t* dn;
1344 6 int skip_cb_cleanup = 0;
1345  
1346 /* Check for use-after-free: if magic number is wrong, this object
1347 * was already freed and memory is being reused. Skip cleanup. */
1348 6 ▶ if (self->magic != EV_REDIS_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) { 0 6 6
1349 0 ▶ if (self->magic == EV_REDIS_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED) { 0 0 0
1350 /* Already destroyed - this is a double-free at Perl level */
1351 0 return;
1352 }
1353 /* Unknown magic - memory corruption or uninitialized */
1354 0 return;
1355 }
1356  
1357 /* Mark as freed FIRST to prevent re-entrant DESTROY */
1358 6 self->magic = EV_REDIS_FREED;
1359  
1360 /* Stop our timers (their data pointer is self). The helpers no-op under
1361 * PL_dirty: the loop may already be destroyed then, and ev_timer_stop
1362 * on a freed loop would crash — a timer left registered at global
1363 * destruction is never dispatched again. */
1364 6 stop_reconnect_timer(self);
1365 6 stop_waiting_timer(self);
1366  
1367 /* During global destruction (PL_dirty), the EV loop and other Perl
1368 * objects may already be destroyed. Clean up hiredis and our own memory
1369 * but don't invoke Perl-level handlers.
1370 * CRITICAL: We must call redisAsyncFree to stop the libev adapter's
1371 * watchers and free the redisAsyncContext. Without this, the adapter's
1372 * ev_io/ev_timer watchers remain registered in the EV loop with dangling
1373 * data pointers, causing SEGV during process cleanup. */
1374 6 ▶ if (PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) { 0 6 6
1375 /* Null cb_queue callbacks before any redisAsyncFree to prevent
1376 * SvREFCNT_dec on potentially-freed SVs during global destruction.
1377 * Covers cbts of both self->ac and any draining contexts.
1378 * (Same pattern as wait_queue below.) */
1379 {
1380 ngx_queue_t* q;
1381 0 for (q = ngx_queue_head(&self->cb_queue);
1382 0 ▶ q != ngx_queue_sentinel(&self->cb_queue);
Branch: 1 branch
BranchTrueFalseTotal
q != ngx_queue_sentinel(&self->cb_queue); 0 0 0
1383 0 q = ngx_queue_next(q)) {
1384 0 ev_redis_cb_t* cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
1385 0 cbt->cb = NULL;
1386 }
1387 }
1388 /* Detach draining contexts before any free can trigger a nested
1389 * dataCleanup (see the non-dirty path below). */
1390 0 for (dq = ngx_queue_head(&self->drain_queue);
1391 0 ▶ dq != ngx_queue_sentinel(&self->drain_queue);
Branch: 1 branch
BranchTrueFalseTotal
dq != ngx_queue_sentinel(&self->drain_queue); 0 0 0
1392 0 dq = ngx_queue_next(dq)) {
1393 0 dn = ngx_queue_data(dq, ev_redis_drain_t, queue);
1394 0 dn->ac->data = NULL;
1395 }
1396 0 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 0 0
1397 0 self->ac->data = NULL; /* prevent callbacks from accessing self */
1398 0 redisAsyncFree(self->ac);
1399 0 self->ac = NULL;
1400 }
1401 /* Draining contexts (deferred disconnect) still have live watchers
1402 * registered in the loop; free them the same way. Their reply
1403 * callbacks see data == NULL and pre-nulled cbs — no Perl runs. */
1404 0 ▶ while (!ngx_queue_empty(&self->drain_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->drain_queue)) { 0 0 0
1405 0 dq = ngx_queue_head(&self->drain_queue);
1406 0 dn = ngx_queue_data(dq, ev_redis_drain_t, queue);
1407 0 ngx_queue_remove(dq);
1408 0 dn->ac->data = NULL;
1409 0 redisAsyncFree(dn->ac);
1410 0 Safefree(dn);
1411 }
1412 0 free_c_fields(self);
1413 /* Free wait_queue C memory (skip Perl callbacks during global destruction) */
1414 0 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 0 0 0
1415 0 ngx_queue_t* q = ngx_queue_head(&self->wait_queue);
1416 0 ev_redis_wait_t* wt = ngx_queue_data(q, ev_redis_wait_t, queue);
1417 0 ngx_queue_remove(q);
1418 0 wt->cb = NULL; /* skip SvREFCNT_dec during PL_dirty */
1419 0 free_wait_entry(wt);
1420 }
1421 0 Safefree(self);
1422 0 return;
1423 }
1424  
1425 6 self->reconnect = 0;
1426  
1427 /* Detach draining contexts before any teardown below: a nested
1428 * dataCleanup can misconsume a drain node (failed-connect contexts run
1429 * trailing callbacks with ev.data already NULL); pre-nulled data turns
1430 * that into a skipped redundant free instead of a dangling self. */
1431 6 for (dq = ngx_queue_head(&self->drain_queue);
1432 6 ▶ dq != ngx_queue_sentinel(&self->drain_queue);
Branch: 1 branch
BranchTrueFalseTotal
dq != ngx_queue_sentinel(&self->drain_queue); 0 6 6
1433 0 dq = ngx_queue_next(dq)) {
1434 0 dn = ngx_queue_data(dq, ev_redis_drain_t, queue);
1435 0 dn->ac->data = NULL;
1436 }
1437  
1438 /* CRITICAL: Set self->ac to NULL BEFORE calling redisAsyncFree.
1439 * redisAsyncFree triggers reply callbacks, which call send_next_waiting,
1440 * which checks self->ac != NULL before issuing commands. If we don't
1441 * clear self->ac first, send_next_waiting will try to call
1442 * redisAsyncCommandArgv during the teardown, causing heap corruption. */
1443 6 self->loop = NULL;
1444 6 ac_to_free = self->ac;
1445 6 self->ac = NULL;
1446 6 ▶ if (NULL != ac_to_free) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != ac_to_free) { 0 6 6
1447 /* If inside a hiredis callback (REDIS_IN_CALLBACK), redisAsyncFree
1448 * will be deferred. hiredis will fire pending reply callbacks later
1449 * via __redisAsyncFree. NULL ac->data so those callbacks see NULL self
1450 * and handle cleanup without accessing freed memory. */
1451 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
1452 0 ac_to_free->data = NULL;
1453 0 skip_cb_cleanup = 1;
1454 }
1455  
1456 /* Protect against premature free in disconnect_cb if triggered synchronously */
1457 0 self->callback_depth++;
1458 0 redisAsyncFree(ac_to_free);
1459 0 self->callback_depth--;
1460 }
1461 /* Detach and free draining contexts. Their pending callbacks fire via
1462 * the self==NULL reply path, which frees each cbt WITHOUT touching
1463 * cb_queue — hence skip_cb_cleanup. In-callback frees are deferred by
1464 * hiredis. */
1465 6 ▶ while (!ngx_queue_empty(&self->drain_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->drain_queue)) { 0 6 6
1466 0 dq = ngx_queue_head(&self->drain_queue);
1467 0 dn = ngx_queue_data(dq, ev_redis_drain_t, queue);
1468 0 ngx_queue_remove(dq);
1469 0 dn->ac->data = NULL;
1470 0 redisAsyncFree(dn->ac);
1471 0 Safefree(dn);
1472 0 skip_cb_cleanup = 1;
1473 }
1474 /* Inside any C-level callback, hiredis may still fire trailing reply
1475 * callbacks for a mid-teardown context we no longer point to (failed
1476 * connect). Preserve cb_queue; reply_cb frees each cbt as it fires. */
1477 6 ▶ if (self->callback_depth > 0 || self->current_cb != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->callback_depth > 0 || self->current_cb != NULL) { 6 0 6
if (self->callback_depth > 0 || self->current_cb != NULL) { 0 6 6
1478 0 skip_cb_cleanup = 1;
1479 }
1480 6 ▶ CLEAR_HANDLER(self->error_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->error_handler); 6 0 6
1481 6 ▶ CLEAR_HANDLER(self->connect_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->connect_handler); 0 6 6
1482 6 ▶ CLEAR_HANDLER(self->disconnect_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->disconnect_handler); 0 6 6
1483 6 ▶ CLEAR_HANDLER(self->push_handler);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->push_handler); 0 6 6
1484 /* all loop usage (timers, watcher teardown) is done */
1485 6 ▶ CLEAR_HANDLER(self->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->loop_sv); 6 0 6
1486 6 free_c_fields(self);
1487  
1488 6 ▶ if (!self->in_wait_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->in_wait_cleanup) { 6 0 6
1489 6 clear_wait_queue_sv(self, err_disconnected);
1490 }
1491 6 ▶ if (!skip_cb_cleanup && !self->in_cb_cleanup) {
Branch: 2 branches
BranchTrueFalseTotal
if (!skip_cb_cleanup && !self->in_cb_cleanup) { 6 0 6
if (!skip_cb_cleanup && !self->in_cb_cleanup) { 6 0 6
1492 /* Safe to free cbts ourselves — hiredis has no deferred references. */
1493 6 remove_cb_queue_sv(self, NULL, NULL);
1494 }
1495 /* else: hiredis still holds references to our cbts (deferred free/disconnect).
1496 * reply_cb will handle cbt cleanup when called with self == NULL. */
1497  
1498 /* Defer Safefree if inside a callback — check_destroyed() handles it */
1499 6 ▶ if (self->current_cb == NULL && self->callback_depth == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->current_cb == NULL && self->callback_depth == 0) { 6 0 6
if (self->current_cb == NULL && self->callback_depth == 0) { 6 0 6
1500 6 Safefree(self);
1501 }
1502 }
1503  
1504 void
1505 connect(EV::Redis self, char* hostname, int port = 6379);
1506 CODE:
1507 {
1508 redisOptions opts;
1509  
1510 0 ▶ if (self->magic != EV_REDIS_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) { 0 0 0
1511 0 croak("cannot connect: object is being destroyed");
1512 }
1513 0 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 0 0
1514 0 croak("already connected");
1515 }
1516  
1517 0 self->intentional_disconnect = 0;
1518 0 self->reconnect_attempts = 0;
1519 0 self->monitoring = 0;
1520 0 clear_connection_params(self);
1521 0 self->host = savepv(hostname);
1522 0 self->port = port;
1523  
1524 0 memset(&opts, 0, sizeof(opts));
1525 0 pre_connect_common(self, &opts);
1526 0 REDIS_OPTIONS_SET_TCP(&opts, hostname, port);
1527 0 self->ac = redisAsyncConnectWithOptions(&opts);
1528 0 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 0 0 0
1529 0 croak("connect error: cannot allocate memory");
1530 }
1531  
1532 0 (void)post_connect_setup(self, "connect error");
1533 }
1534  
1535 void
1536 connect_unix(EV::Redis self, const char* path);
1537 CODE:
1538 {
1539 redisOptions opts;
1540  
1541 0 ▶ if (self->magic != EV_REDIS_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_REDIS_MAGIC) { 0 0 0
1542 0 croak("cannot connect: object is being destroyed");
1543 }
1544 0 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 0 0
1545 0 croak("already connected");
1546 }
1547  
1548 0 self->intentional_disconnect = 0;
1549 0 self->reconnect_attempts = 0;
1550 0 self->monitoring = 0;
1551 0 clear_connection_params(self);
1552 0 self->path = savepv(path);
1553  
1554 0 memset(&opts, 0, sizeof(opts));
1555 0 pre_connect_common(self, &opts);
1556 0 REDIS_OPTIONS_SET_UNIX(&opts, path);
1557 0 self->ac = redisAsyncConnectWithOptions(&opts);
1558 0 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 0 0 0
1559 0 croak("connect error: cannot allocate memory");
1560 }
1561  
1562 0 (void)post_connect_setup(self, "connect error");
1563 }
1564  
1565 void
1566 disconnect(EV::Redis self);
1567 CODE:
1568 {
1569 /* Stop any pending reconnect timer on explicit disconnect */
1570 0 self->intentional_disconnect = 1;
1571 0 stop_reconnect_timer(self);
1572 0 self->reconnect_attempts = 0;
1573  
1574 0 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 0 0 0
1575 /* Already disconnected — still stop waiting timer and clear
1576 * wait queue (e.g., resume_waiting_on_reconnect kept them alive
1577 * after a connection drop, but user now explicitly disconnects). */
1578 0 stop_waiting_timer(self);
1579 0 ▶ if (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->wait_queue)) { 0 0 0
1580 0 self->callback_depth++;
1581 0 clear_wait_queue_sv(self, err_disconnected);
1582 0 self->callback_depth--;
1583 0 check_destroyed(self);
1584 }
1585 0 return;
1586 }
1587 /* redisAsyncDisconnect defers unless called outside a callback with no
1588 * pending replies — the context then keeps draining after we null
1589 * self->ac. Track it so DESTROY can detach/free it; the synchronous
1590 * case untracks via disconnect_cb before this call returns. */
1591 {
1592 0 redisAsyncContext* c = self->ac;
1593 0 track_draining(self, c);
1594 /* Protect against Safefree(self) if disconnect_cb fires synchronously
1595 * and user's on_disconnect handler drops the last Perl reference. */
1596 0 self->callback_depth++;
1597 0 ▶ if (self->monitoring) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->monitoring) { 0 0 0
1598 /* a monitor stream never drains (hiredis re-queues its record
1599 * after every line) — tear down immediately */
1600 0 redisAsyncFree(c);
1601 }
1602 else {
1603 0 redisAsyncDisconnect(c);
1604 }
1605 /* The handlers above may have re-established a new connection;
1606 * only clear our pointer if it still refers to c. */
1607 0 ▶ if (self->ac == c) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ac == c) { 0 0 0
1608 0 self->ac = NULL;
1609 }
1610 0 self->monitoring = 0;
1611 0 self->callback_depth--;
1612 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1613 }
1614 }
1615  
1616 int
1617 is_connected(EV::Redis self);
1618 CODE:
1619 {
1620 0 ▶ RETVAL = (NULL != self->ac) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (NULL != self->ac) ? 1 : 0; 0 0 0
1621 }
1622 OUTPUT:
1623 RETVAL
1624  
1625 SV*
1626 connect_timeout(EV::Redis self, SV* timeout_ms = NULL);
1627 CODE:
1628 {
1629 0 RETVAL = timeout_accessor(&self->connect_timeout, timeout_ms, "connect_timeout");
1630 }
1631 OUTPUT:
1632 RETVAL
1633  
1634 SV*
1635 command_timeout(EV::Redis self, SV* timeout_ms = NULL);
1636 CODE:
1637 {
1638 0 RETVAL = timeout_accessor(&self->command_timeout, timeout_ms, "command_timeout");
1639 /* Apply to active connection immediately */
1640 0 ▶ 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) { 0 0 0
if (NULL != timeout_ms && SvOK(timeout_ms) && NULL != self->ac && NULL != self->command_timeout) { 0 0 0
if (NULL != timeout_ms && SvOK(timeout_ms) && NULL != self->ac && NULL != self->command_timeout) { 0 0 0
if (NULL != timeout_ms && SvOK(timeout_ms) && NULL != self->ac && NULL != self->command_timeout) { 0 0 0
1641 0 redisAsyncSetTimeout(self->ac, *self->command_timeout);
1642 /* Re-arm the already-scheduled timer too — but only once connected;
1643 * while connecting the armed timer is the connect timeout. */
1644 0 ▶ if (self->ac->c.flags & REDIS_CONNECTED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ac->c.flags & REDIS_CONNECTED) { 0 0 0
1645 0 redisLibevRefreshTimeout(self->ac, *self->command_timeout);
1646 }
1647 }
1648 }
1649 OUTPUT:
1650 RETVAL
1651  
1652 SV*
1653 on_error(EV::Redis self, SV* handler = NULL);
1654 CODE:
1655 {
1656 6 RETVAL = handler_accessor(&self->error_handler, handler, items > 1);
1657 }
1658 OUTPUT:
1659 RETVAL
1660  
1661 SV*
1662 on_connect(EV::Redis self, SV* handler = NULL);
1663 CODE:
1664 {
1665 0 RETVAL = handler_accessor(&self->connect_handler, handler, items > 1);
1666 }
1667 OUTPUT:
1668 RETVAL
1669  
1670 SV*
1671 on_disconnect(EV::Redis self, SV* handler = NULL);
1672 CODE:
1673 {
1674 0 RETVAL = handler_accessor(&self->disconnect_handler, handler, items > 1);
1675 }
1676 OUTPUT:
1677 RETVAL
1678  
1679 SV*
1680 on_push(EV::Redis self, SV* handler = NULL);
1681 CODE:
1682 {
1683 0 RETVAL = handler_accessor(&self->push_handler, handler, items > 1);
1684 /* Sync push callback with hiredis if connected */
1685 0 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 0 0
1686 0 ▶ if (NULL != self->push_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->push_handler) { 0 0 0
1687 0 redisAsyncSetPushCallback(self->ac, EV__redis_push_cb);
1688 } else {
1689 0 redisAsyncSetPushCallback(self->ac, NULL);
1690 }
1691 }
1692 }
1693 OUTPUT:
1694 RETVAL
1695  
1696 int
1697 command(EV::Redis self, ...);
1698 PREINIT:
1699 SV* cb;
1700 char** argv;
1701 size_t* argvlen;
1702 STRLEN len;
1703 int argc, i, persist;
1704 ev_redis_cb_t* cbt;
1705 ev_redis_wait_t* wt;
1706 char* p;
1707 CODE:
1708 {
1709 1 ▶ if (items < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) { 0 1 1
1710 0 croak("Usage: command(\"command\", ..., [$callback])");
1711 }
1712  
1713 1 cb = ST(items - 1);
1714 1 ▶ if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 1 0 1
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 1 0 1
1715 1 argc = items - 2; /* last arg is callback */
1716 }
1717 else {
1718 0 cb = NULL; /* fire-and-forget: no callback */
1719 0 argc = items - 1;
1720 }
1721  
1722 1 ▶ if (argc < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (argc < 1) { 0 1 1
1723 0 croak("Usage: command(\"command\", ..., [$callback])");
1724 }
1725  
1726 /* hiredis's monitor mode repushes each just-run callback record; a
1727 * freed one-shot cbt would be re-fired with dangling privdata. */
1728 1 ▶ if (self->monitoring) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->monitoring) { 0 1 1
1729 0 croak("cannot send commands while MONITOR is active on this connection");
1730 }
1731  
1732 1 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 1 0 1
1733 1 ▶ if (!self->reconnect_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->reconnect_timer_active) { 1 0 1
1734 1 croak("connection required before calling command");
1735 }
1736 /* Reconnect in progress — fall through to queue in wait_queue */
1737 }
1738 0 Newx(argv, argc, char*);
1739 0 SAVEFREEPV(argv);
1740 0 Newx(argvlen, argc, size_t);
1741 0 SAVEFREEPV(argvlen);
1742  
1743 0 ▶ for (i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) { 0 0 0
1744 /* SvPVbyte: wire bytes must not depend on the SV's internal
1745 * representation; croaks on wide characters. */
1746 0 argv[i] = SvPVbyte(ST(i + 1), len);
1747 0 argvlen[i] = len;
1748 }
1749  
1750 0 ▶ if (is_shard_pubsub_command(argv[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_shard_pubsub_command(argv[0])) { 0 0 0
1751 0 croak("%s is not supported: bundled hiredis has no sharded pub/sub "
1752 "support (use spublish for publishing; subscribe via a plain "
1753 "subscribe on a non-cluster channel)", argv[0]);
1754 }
1755  
1756 0 persist = is_persistent_command(argv[0]);
1757  
1758 /* Channel-less subscribe: hiredis stores it one-shot while we track it
1759 * persistent — the cbt would strand and double-fire at disconnect.
1760 * (unsubscribe with no args is valid.) */
1761 0 ▶ if (persist && argc < 2 &&
Branch: 1 branch
BranchTrueFalseTotal
if (persist && argc < 2 && 0 0 0
1762 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
1763 0 croak("%s requires at least one channel", argv[0]);
1764 }
1765  
1766 0 ▶ if (is_monitor_command(argv[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_monitor_command(argv[0])) { 0 0 0
1767 /* Commands already pending when MONITOR activates hit the same
1768 * repush hazard — require a fully idle connection. */
1769 0 ▶ if (NULL == self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac) { 0 0 0
1770 0 croak("MONITOR requires an active connection");
1771 }
1772 0 ▶ if (!ngx_queue_empty(&self->cb_queue) ||
Branch: 1 branch
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->cb_queue) || 0 0 0
1773 0 ▶ !ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
!ngx_queue_empty(&self->wait_queue)) { 0 0 0
1774 0 croak("MONITOR requires an idle connection "
1775 "(no pending, waiting, or subscribed commands)");
1776 }
1777 }
1778  
1779 0 ▶ if (NULL == self->ac ||
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == self->ac || 0 0 0
1780 0 ▶ (self->max_pending > 0 && self->pending_count >= self->max_pending)) {
Branch: 2 branches
BranchTrueFalseTotal
(self->max_pending > 0 && self->pending_count >= self->max_pending)) { 0 0 0
(self->max_pending > 0 && self->pending_count >= self->max_pending)) { 0 0 0
1781 0 Newx(wt, 1, ev_redis_wait_t);
1782 0 Newx(wt->argv, argc, char*);
1783 0 Newx(wt->argvlen, argc, size_t);
1784 0 ▶ for (i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) { 0 0 0
1785 0 Newx(p, argvlen[i] + 1, char);
1786 0 Copy(argv[i], p, argvlen[i], char);
1787 0 p[argvlen[i]] = '\0';
1788 0 wt->argv[i] = p;
1789 0 wt->argvlen[i] = argvlen[i];
1790 }
1791 0 wt->argc = argc;
1792 0 wt->cb = SvREFCNT_inc(cb);
1793 0 wt->persist = persist;
1794 /* Refresh ev_now: command() may be called outside an ev_run iteration
1795 * (e.g. during initial setup), where the cached time is stale. Without
1796 * this, queued_at reflects an old time base and a later expire check
1797 * against the up-to-date ev_now would compute an inflated elapsed. */
1798 0 ev_now_update(self->loop);
1799 0 wt->queued_at = ev_now(self->loop);
1800 0 ngx_queue_init(&wt->queue);
1801 0 ngx_queue_insert_tail(&self->wait_queue, &wt->queue);
1802 0 self->waiting_count++;
1803 0 schedule_waiting_timer(self);
1804 0 RETVAL = REDIS_OK;
1805 }
1806 else {
1807 0 Newx(cbt, 1, ev_redis_cb_t);
1808 0 cbt->cb = SvREFCNT_inc(cb);
1809 0 cbt->skipped = 0;
1810 0 cbt->persist = persist;
1811 0 ▶ cbt->sub_count = persist ? argc - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
cbt->sub_count = persist ? argc - 1 : 0; 0 0 0
1812 0 ngx_queue_init(&cbt->queue);
1813 0 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
1814 0 ▶ if (!persist) self->pending_count++;
Branch: 1 branch
BranchTrueFalseTotal
if (!persist) self->pending_count++; 0 0 0
1815  
1816 0 RETVAL = submit_to_redis(self, cbt,
1817 argc, (const char**)argv, argvlen);
1818 0 ▶ if (REDIS_OK == RETVAL && is_monitor_command(argv[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (REDIS_OK == RETVAL && is_monitor_command(argv[0])) { 0 0 0
if (REDIS_OK == RETVAL && is_monitor_command(argv[0])) { 0 0 0
1819 0 self->monitoring = 1;
1820 }
1821 }
1822 }
1823 OUTPUT:
1824 RETVAL
1825  
1826 void
1827 reconnect(EV::Redis self, int enable, int delay_ms = 1000, int max_attempts = 0);
1828 CODE:
1829 {
1830 0 validate_timeout_ms(delay_ms, "reconnect_delay");
1831 0 self->reconnect = enable ? 1 : 0;
1832 0 self->reconnect_delay_ms = delay_ms;
1833 0 self->max_reconnect_attempts = max_attempts >= 0 ? max_attempts : 0;
1834 0 self->reconnect_attempts = 0;
1835  
1836 0 ▶ if (!enable) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enable) { 0 0 0
1837 0 stop_reconnect_timer(self);
1838 /* Disabling reconnect while disconnected: commands queued for the
1839 * (now cancelled) reconnect would otherwise hang forever. */
1840 0 ▶ if (NULL == self->ac && !ngx_queue_empty(&self->wait_queue)) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == self->ac && !ngx_queue_empty(&self->wait_queue)) { 0 0 0
if (NULL == self->ac && !ngx_queue_empty(&self->wait_queue)) { 0 0 0
1841 0 self->callback_depth++;
1842 0 clear_wait_queue_sv(self,
1843 sv_2mortal(newSVpv("reconnect disabled", 0)));
1844 0 stop_waiting_timer(self);
1845 0 self->callback_depth--;
1846 0 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 0 0
1847 }
1848 }
1849 }
1850  
1851 int
1852 reconnect_enabled(EV::Redis self);
1853 CODE:
1854 {
1855 0 ▶ RETVAL = self->reconnect;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->reconnect; 0 0 0
1856 }
1857 OUTPUT:
1858 RETVAL
1859  
1860 int
1861 pending_count(EV::Redis self);
1862 CODE:
1863 {
1864 0 ▶ RETVAL = self->pending_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->pending_count; 0 0 0
1865 }
1866 OUTPUT:
1867 RETVAL
1868  
1869 int
1870 waiting_count(EV::Redis self);
1871 CODE:
1872 {
1873 0 ▶ RETVAL = self->waiting_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->waiting_count; 0 0 0
1874 }
1875 OUTPUT:
1876 RETVAL
1877  
1878 int
1879 max_pending(EV::Redis self, SV* limit = NULL);
1880 CODE:
1881 {
1882 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
1883 0 int val = SvIV(limit);
1884 0 ▶ if (val < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val < 0) { 0 0 0
1885 0 croak("max_pending must be non-negative");
1886 }
1887 0 self->max_pending = val;
1888  
1889 /* When limit is increased or removed, send waiting commands.
1890 * callback_depth protects against DESTROY if a failed command's
1891 * error callback drops the last Perl reference to self. */
1892 0 self->callback_depth++;
1893 0 send_next_waiting(self);
1894 0 self->callback_depth--;
1895 0 ▶ if (check_destroyed(self)) XSRETURN_IV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) XSRETURN_IV(0); 0 0 0
1896 }
1897 0 ▶ RETVAL = self->max_pending;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->max_pending; 0 0 0
1898 }
1899 OUTPUT:
1900 RETVAL
1901  
1902 SV*
1903 waiting_timeout(EV::Redis self, SV* timeout_ms = NULL);
1904 CODE:
1905 {
1906 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
1907 0 IV ms = SvIV(timeout_ms);
1908 0 validate_timeout_ms(ms, "waiting_timeout");
1909 0 self->waiting_timeout_ms = (int)ms;
1910 0 schedule_waiting_timer(self);
1911 }
1912  
1913 0 RETVAL = newSViv((IV)self->waiting_timeout_ms);
1914 }
1915 OUTPUT:
1916 RETVAL
1917  
1918 int
1919 resume_waiting_on_reconnect(EV::Redis self, SV* value = NULL);
1920 CODE:
1921 {
1922 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
1923 0 self->resume_waiting_on_reconnect = SvTRUE(value) ? 1 : 0;
1924 }
1925 0 ▶ RETVAL = self->resume_waiting_on_reconnect;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->resume_waiting_on_reconnect; 0 0 0
1926 }
1927 OUTPUT:
1928 RETVAL
1929  
1930 int
1931 priority(EV::Redis self, SV* value = NULL);
1932 CODE:
1933 {
1934 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
1935 0 int prio = SvIV(value);
1936 0 ▶ if (prio < EV_MINPRI) prio = EV_MINPRI;
Branch: 1 branch
BranchTrueFalseTotal
if (prio < EV_MINPRI) prio = EV_MINPRI; 0 0 0
1937 0 ▶ if (prio > EV_MAXPRI) prio = EV_MAXPRI;
Branch: 1 branch
BranchTrueFalseTotal
if (prio > EV_MAXPRI) prio = EV_MAXPRI; 0 0 0
1938 0 self->priority = prio;
1939 0 ▶ if (NULL != self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ac) { 0 0 0
1940 0 redisLibevSetPriority(self->ac, prio);
1941 }
1942 }
1943 0 ▶ RETVAL = self->priority;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->priority; 0 0 0
1944 }
1945 OUTPUT:
1946 RETVAL
1947  
1948 int
1949 keepalive(EV::Redis self, SV* value = NULL);
1950 CODE:
1951 {
1952 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
1953 0 int interval = SvIV(value);
1954 0 ▶ if (interval < 0) croak("keepalive interval must be non-negative");
Branch: 1 branch
BranchTrueFalseTotal
if (interval < 0) croak("keepalive interval must be non-negative"); 0 0 0
1955 0 ▶ if (interval > MAX_TIMEOUT_MS / 1000) croak("keepalive interval too large");
Branch: 1 branch
BranchTrueFalseTotal
if (interval > MAX_TIMEOUT_MS / 1000) croak("keepalive interval too large"); 0 0 0
1956 0 self->keepalive = interval;
1957 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
1958 0 redisEnableKeepAliveWithInterval(&self->ac->c, interval);
1959 }
1960 }
1961 0 ▶ RETVAL = self->keepalive;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->keepalive; 0 0 0
1962 }
1963 OUTPUT:
1964 RETVAL
1965  
1966 int
1967 prefer_ipv4(EV::Redis self, SV* value = NULL);
1968 CODE:
1969 {
1970 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
1971 0 self->prefer_ipv4 = SvTRUE(value) ? 1 : 0;
1972 0 ▶ if (self->prefer_ipv4) self->prefer_ipv6 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->prefer_ipv4) self->prefer_ipv6 = 0; 0 0 0
1973 }
1974 0 ▶ RETVAL = self->prefer_ipv4;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->prefer_ipv4; 0 0 0
1975 }
1976 OUTPUT:
1977 RETVAL
1978  
1979 int
1980 prefer_ipv6(EV::Redis self, SV* value = NULL);
1981 CODE:
1982 {
1983 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
1984 0 self->prefer_ipv6 = SvTRUE(value) ? 1 : 0;
1985 0 ▶ if (self->prefer_ipv6) self->prefer_ipv4 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->prefer_ipv6) self->prefer_ipv4 = 0; 0 0 0
1986 }
1987 0 ▶ RETVAL = self->prefer_ipv6;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->prefer_ipv6; 0 0 0
1988 }
1989 OUTPUT:
1990 RETVAL
1991  
1992 SV*
1993 source_addr(EV::Redis self, SV* value = NULL);
1994 CODE:
1995 {
1996 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
1997 0 ▶ if (NULL != self->source_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->source_addr) { 0 0 0
1998 0 Safefree(self->source_addr);
1999 0 self->source_addr = NULL;
2000 }
2001 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
2002 0 self->source_addr = savepv(SvPV_nolen(value));
2003 }
2004 }
2005 0 ▶ if (NULL != self->source_addr) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->source_addr) { 0 0 0
2006 0 RETVAL = newSVpv(self->source_addr, 0);
2007 } else {
2008 0 RETVAL = &PL_sv_undef;
2009 }
2010 }
2011 OUTPUT:
2012 RETVAL
2013  
2014 unsigned int
2015 tcp_user_timeout(EV::Redis self, SV* value = NULL);
2016 CODE:
2017 {
2018 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
2019 0 IV ms = SvIV(value);
2020 0 validate_timeout_ms(ms, "tcp_user_timeout");
2021 0 self->tcp_user_timeout = (unsigned int)ms;
2022 }
2023 0 ▶ RETVAL = self->tcp_user_timeout;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->tcp_user_timeout; 0 0 0
2024 }
2025 OUTPUT:
2026 RETVAL
2027  
2028 int
2029 cloexec(EV::Redis self, SV* value = NULL);
2030 CODE:
2031 {
2032 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
2033 0 self->cloexec = SvTRUE(value) ? 1 : 0;
2034 }
2035 0 ▶ RETVAL = self->cloexec;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->cloexec; 0 0 0
2036 }
2037 OUTPUT:
2038 RETVAL
2039  
2040 int
2041 reuseaddr(EV::Redis self, SV* value = NULL);
2042 CODE:
2043 {
2044 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
2045 0 self->reuseaddr = SvTRUE(value) ? 1 : 0;
2046 }
2047 0 ▶ RETVAL = self->reuseaddr;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->reuseaddr; 0 0 0
2048 }
2049 OUTPUT:
2050 RETVAL
2051  
2052 void
2053 skip_waiting(EV::Redis self);
2054 CODE:
2055 {
2056 /* Protect self from destruction during queue iteration */
2057 0 self->callback_depth++;
2058  
2059 /* If cleanup is already in progress (e.g., during expire_waiting_commands
2060 * or disconnect callback), don't modify the wait_queue. */
2061 0 ▶ if (self->in_wait_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_wait_cleanup) { 0 0 0
2062 0 self->callback_depth--;
2063 0 check_destroyed(self);
2064 0 return;
2065 }
2066  
2067 0 clear_wait_queue_sv(self, err_skipped);
2068 0 stop_waiting_timer(self);
2069  
2070 0 self->callback_depth--;
2071 0 check_destroyed(self);
2072 }
2073  
2074 void
2075 skip_pending(EV::Redis self);
2076 CODE:
2077 {
2078 ngx_queue_t local_queue;
2079 ngx_queue_t* q;
2080 ev_redis_cb_t* cbt;
2081  
2082 /* Protect self from destruction during queue iteration */
2083 0 self->callback_depth++;
2084  
2085 /* Always attempt to clear waiting queue (handles its own re-entrancy) */
2086 0 clear_wait_queue_sv(self, err_skipped);
2087 0 stop_waiting_timer(self);
2088  
2089 /* If cb_queue cleanup is already in progress, stop here. */
2090 0 ▶ if (self->in_cb_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_cb_cleanup) { 0 0 0
2091 0 self->callback_depth--;
2092 0 check_destroyed(self);
2093 0 return;
2094 }
2095  
2096 /* Protect cb_queue iteration from re-entrancy. If a user callback
2097 * calls skip_pending() again, the in_cb_cleanup check above will return. */
2098 0 self->in_cb_cleanup = 1;
2099  
2100 0 ngx_queue_init(&local_queue);
2101 0 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 0 0 0
2102 0 q = ngx_queue_head(&self->cb_queue);
2103 0 cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
2104  
2105 0 ▶ if (cbt == self->current_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt == self->current_cb) { 0 0 0
2106 /* If current_cb is at head — if it's the only item, we're done */
2107 0 ▶ if (ngx_queue_next(q) == ngx_queue_sentinel(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_next(q) == ngx_queue_sentinel(&self->cb_queue)) { 0 0 0
2108 0 break;
2109 }
2110 0 q = ngx_queue_next(q);
2111 0 cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
2112 }
2113  
2114 0 ngx_queue_remove(q);
2115 0 ngx_queue_insert_tail(&local_queue, q);
2116 }
2117  
2118 0 ▶ while (!ngx_queue_empty(&local_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&local_queue)) { 0 0 0
2119 0 ▶ if (self->magic == EV_REDIS_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == EV_REDIS_FREED) { 0 0 0
2120 0 break;
2121 }
2122  
2123 0 q = ngx_queue_head(&local_queue);
2124 0 cbt = ngx_queue_data(q, ev_redis_cb_t, queue);
2125 0 ngx_queue_remove(q);
2126  
2127 /* Mark as skipped FIRST to prevent double callback invocation if
2128 * invoke_callback_error re-enters the event loop. */
2129 0 cbt->skipped = 1;
2130  
2131 /* Re-initialize queue node so any subsequent remove (from reply_cb's
2132 * skipped path on re-entry) is safe. */
2133 0 ngx_queue_init(q);
2134 0 ▶ if (!cbt->persist) self->pending_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->persist) self->pending_count--; 0 0 0
2135  
2136 /* Save and clear callback BEFORE invoking — if the user callback
2137 * re-enters and a Redis reply arrives, reply_cb sees skipped=1
2138 * and frees cbt. Clearing cb first avoids use-after-free. */
2139 0 ▶ if (NULL != cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != cbt->cb) { 0 0 0
2140 0 SV* cb_to_invoke = cbt->cb;
2141 0 cbt->cb = NULL;
2142  
2143 0 invoke_callback_error(cb_to_invoke, err_skipped);
2144 0 SvREFCNT_dec(cb_to_invoke);
2145 }
2146 }
2147  
2148 0 self->in_cb_cleanup = 0;
2149  
2150 0 self->callback_depth--;
2151 0 check_destroyed(self);
2152 }
2153  
2154 int
2155 has_ssl(char* class);
2156 CODE:
2157 {
2158 PERL_UNUSED_VAR(class);
2159 #ifdef EV_REDIS_SSL
2160 4 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 4 4
2161 #else
2162 RETVAL = 0;
2163 #endif
2164 }
2165 OUTPUT:
2166 RETVAL
2167  
2168 #ifdef EV_REDIS_SSL
2169  
2170 void
2171 _setup_ssl_context(EV::Redis self, SV* cacert, SV* capath, SV* cert, SV* key, SV* server_name, int verify = 1);
2172 CODE:
2173 {
2174 4 redisSSLContextError ssl_error = REDIS_SSL_CTX_NONE;
2175 redisSSLContext* new_ctx;
2176 redisSSLOptions ssl_opts;
2177  
2178 4 memset(&ssl_opts, 0, sizeof(ssl_opts));
2179 5 ▶ ssl_opts.cacert_filename = (SvOK(cacert)) ? SvPV_nolen(cacert) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.cacert_filename = (SvOK(cacert)) ? SvPV_nolen(cacert) : NULL; 1 3 4
2180 5 ▶ ssl_opts.capath = (SvOK(capath)) ? SvPV_nolen(capath) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.capath = (SvOK(capath)) ? SvPV_nolen(capath) : NULL; 1 3 4
2181 5 ▶ ssl_opts.cert_filename = (SvOK(cert)) ? SvPV_nolen(cert) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.cert_filename = (SvOK(cert)) ? SvPV_nolen(cert) : NULL; 1 3 4
2182 4 ▶ ssl_opts.private_key_filename = (SvOK(key)) ? SvPV_nolen(key) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.private_key_filename = (SvOK(key)) ? SvPV_nolen(key) : NULL; 0 4 4
2183 4 ▶ ssl_opts.server_name = (SvOK(server_name)) ? SvPV_nolen(server_name) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ssl_opts.server_name = (SvOK(server_name)) ? SvPV_nolen(server_name) : NULL; 0 4 4
2184 4 ssl_opts.verify_mode = verify ? REDIS_SSL_VERIFY_PEER : REDIS_SSL_VERIFY_NONE;
2185  
2186 /* Create before freeing the old context: a failed (eval-trapped)
2187 * reconfiguration must not leave ssl_ctx NULL, or the next reconnect
2188 * would silently proceed in plaintext. */
2189 4 new_ctx = redisCreateSSLContextWithOptions(&ssl_opts, &ssl_error);
2190 4 ▶ if (NULL == new_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == new_ctx) { 2 2 4
2191 2 croak("SSL context creation failed: %s", redisSSLContextGetError(ssl_error));
2192 }
2193  
2194 2 ▶ if (NULL != self->ssl_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != self->ssl_ctx) { 0 2 2
2195 0 redisFreeSSLContext(self->ssl_ctx);
2196 }
2197 2 self->ssl_ctx = new_ctx;
2198 }
2199  
2200 #endif
« src/libev_adapter.h ↑ Summary