Memcached.xs

filter: Statementstmt 65.1% 891 / 1367 Branchbran 43.1% 424 / 982 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.9% 1315 / 2349 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/EV/Memcached.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5  
6 #include "EVAPI.h"
7 #include "ngx-queue.h"
8  
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
13 #include <netdb.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <arpa/inet.h>
19  
20 /* ================================================================
21 * Constants
22 * ================================================================ */
23  
24 #define MC_MAGIC_ALIVE 0xCAFEBEEF
25 #define MC_MAGIC_FREED 0xDEADCAFE
26  
27 #define MC_REQ_MAGIC 0x80
28 #define MC_RES_MAGIC 0x81
29 #define MC_HEADER_SIZE 24
30  
31 /* Opcodes */
32 #define MC_OP_GET 0x00
33 #define MC_OP_SET 0x01
34 #define MC_OP_ADD 0x02
35 #define MC_OP_REPLACE 0x03
36 #define MC_OP_DELETE 0x04
37 #define MC_OP_INCR 0x05
38 #define MC_OP_DECR 0x06
39 #define MC_OP_QUIT 0x07
40 #define MC_OP_FLUSH 0x08
41 #define MC_OP_NOOP 0x0A
42 #define MC_OP_VERSION 0x0B
43 #define MC_OP_GETKQ 0x0D
44 #define MC_OP_APPEND 0x0E
45 #define MC_OP_PREPEND 0x0F
46 #define MC_OP_STAT 0x10
47 #define MC_OP_SETQ 0x11
48 #define MC_OP_FLUSHQ 0x18
49 #define MC_OP_TOUCH 0x1C
50 #define MC_OP_GAT 0x1D
51 #define MC_OP_SASL_LIST_MECHS 0x20
52 #define MC_OP_SASL_AUTH 0x21
53 #define MC_OP_SASL_STEP 0x22
54  
55 /* Status codes */
56 #define MC_STATUS_OK 0x0000
57 #define MC_STATUS_KEY_NOT_FOUND 0x0001
58 #define MC_STATUS_KEY_EXISTS 0x0002
59 #define MC_STATUS_VALUE_TOO_LARGE 0x0003
60 #define MC_STATUS_INVALID_ARGS 0x0004
61 #define MC_STATUS_NOT_STORED 0x0005
62 #define MC_STATUS_NON_NUMERIC 0x0006
63 #define MC_STATUS_AUTH_ERROR 0x0020
64 #define MC_STATUS_AUTH_CONTINUE 0x0021
65 #define MC_STATUS_UNKNOWN_CMD 0x0081
66 #define MC_STATUS_OUT_OF_MEMORY 0x0082
67  
68 #define BUF_INIT_SIZE 16384
69 #define MC_MAX_KEY_LEN 250
70  
71 /* Callback command types */
72 #define CB_CMD_GET 0 /* get - return value only */
73 #define CB_CMD_GETS 1 /* gets - return {value, flags, cas} */
74 #define CB_CMD_STORE 2 /* set/add/replace/append/prepend - return 1 */
75 #define CB_CMD_DELETE 3 /* delete - return 1 */
76 #define CB_CMD_ARITH 4 /* incr/decr - return new value */
77 #define CB_CMD_TOUCH 5 /* touch - return 1 */
78 #define CB_CMD_GAT 6 /* gat - return value */
79 #define CB_CMD_GATS 7 /* gats - return {value, flags, cas} */
80 #define CB_CMD_VERSION 8 /* version - return string */
81 #define CB_CMD_NOOP 9 /* noop - return 1 */
82 #define CB_CMD_FLUSH 10 /* flush - return 1 */
83 #define CB_CMD_STATS 11 /* stats - accumulate, return hash */
84 #define CB_CMD_MGET_ENTRY 12 /* individual GETKQ in mget */
85 #define CB_CMD_MGET_FENCE 13 /* NOOP fence in mget */
86 #define CB_CMD_QUIT 14 /* quit */
87 #define CB_CMD_MGETS_ENTRY 15 /* individual GETKQ in mgets (full info) */
88 #define CB_CMD_MGETS_FENCE 16 /* NOOP fence in mgets */
89 #define CB_CMD_SASL_LIST 17 /* sasl_list_mechs - return string */
90 #define CB_CMD_SASL_AUTH 18 /* sasl_auth - return 1 on success */
91  
92 #define CLEAR_HANDLER(field) \
93 do { if (NULL != (field)) { SvREFCNT_dec(field); (field) = NULL; } } while(0)
94  
95 #define MC_CROAK_UNLESS_CONNECTED(self) \
96 do { \
97 if (!(self)->connected && !(self)->connecting && \
98 !((self)->reconnect && (self)->reconnect_timer_active)) \
99 croak("not connected"); \
100 } while(0)
101  
102 #define MC_MAX_VALUE_LEN 0x40000000 /* 1 GiB — wire length is u32 */
103  
104 /* ================================================================
105 * Type declarations
106 * ================================================================ */
107  
108 typedef struct ev_mc_s ev_mc_t;
109 typedef struct ev_mc_cb_s ev_mc_cb_t;
110 typedef struct ev_mc_wait_s ev_mc_wait_t;
111  
112 typedef ev_mc_t* EV__Memcached;
113 typedef struct ev_loop* EV__Loop;
114  
115 /* ================================================================
116 * Data structures
117 * ================================================================ */
118  
119 struct ev_mc_cb_s {
120 SV *cb; /* Perl callback (NULL for fire-and-forget / mget entries) */
121 ngx_queue_t queue;
122 uint32_t opaque;
123 int cmd; /* CB_CMD_* */
124 int quiet; /* 1 = quiet variant, may not get response */
125 int counted; /* 1 = contributes to pending_count */
126 int skipped;
127 HV *stats_hv; /* for CB_CMD_STATS: accumulated hash */
128 HV *mget_results; /* for CB_CMD_MGET_ENTRY: shared hash (borrowed) */
129 /* for CB_CMD_MGET_FENCE: owned hash */
130 };
131  
132 struct ev_mc_wait_s {
133 char *packet;
134 size_t packet_len;
135 SV *cb;
136 int cmd;
137 int quiet;
138 uint32_t opaque;
139 HV *stats_hv;
140 HV *mget_results; /* borrowed for MGET_ENTRY, owned for MGET_FENCE */
141 int counted;
142 int no_response; /* 1: fire-and-forget, drain to wbuf only, no cb_queue entry */
143 ngx_queue_t queue;
144 ev_tstamp queued_at;
145 };
146  
147 struct ev_mc_s {
148 unsigned int magic;
149 unsigned int conn_gen; /* bumped by cleanup_connection: stale-session guard */
150 struct ev_loop *loop;
151 SV *loop_sv; /* held ref on a user-supplied EV::Loop (NULL = default) */
152 int fd;
153 int connected;
154 int connecting;
155  
156 /* IO watchers */
157 ev_io rio, wio;
158 int reading, writing;
159  
160 /* Buffers */
161 char *rbuf;
162 size_t rbuf_len, rbuf_cap;
163 char *wbuf;
164 size_t wbuf_len, wbuf_off, wbuf_cap;
165  
166 /* Callbacks */
167 SV *on_error;
168 SV *on_connect;
169 SV *on_disconnect;
170  
171 /* Command queue */
172 ngx_queue_t cb_queue;
173 ngx_queue_t wait_queue;
174 int pending_count;
175 int waiting_count;
176 int max_pending; /* 0 = unlimited */
177 uint32_t next_opaque;
178  
179 /* Reconnection */
180 char *host;
181 int port;
182 char *path;
183 int reconnect;
184 int reconnect_delay_ms;
185 int max_reconnect_attempts;
186 int reconnect_attempts;
187 ev_timer reconnect_timer;
188 int reconnect_timer_active;
189 int intentional_disconnect;
190 int resume_waiting_on_reconnect;
191  
192 /* Timeouts */
193 int connect_timeout_ms;
194 ev_timer connect_timer;
195 int connect_timer_active;
196 int command_timeout_ms;
197 ev_timer cmd_timer;
198 int cmd_timer_active;
199  
200 /* Flow control */
201 int waiting_timeout_ms;
202 ev_timer waiting_timer;
203 int waiting_timer_active;
204  
205 /* Safety */
206 int callback_depth;
207 int in_cb_cleanup;
208 int in_wait_cleanup;
209  
210 /* Options */
211 int priority;
212 int keepalive;
213  
214 /* SASL auth */
215 char *username;
216 char *password;
217 };
218  
219 /* ================================================================
220 * Shared error strings (initialized in BOOT)
221 * ================================================================ */
222  
223 static SV *err_skipped = NULL;
224 static SV *err_disconnected = NULL;
225 static SV *err_waiting_timeout = NULL;
226  
227 /* ================================================================
228 * Forward declarations
229 * ================================================================ */
230  
231 static void io_cb(EV_P_ ev_io *w, int revents);
232 static void reconnect_timer_cb(EV_P_ ev_timer *w, int revents);
233 static void waiting_timer_cb(EV_P_ ev_timer *w, int revents);
234 static void connect_timeout_cb(EV_P_ ev_timer *w, int revents);
235 static void cmd_timeout_cb(EV_P_ ev_timer *w, int revents);
236 static void arm_cmd_timer(ev_mc_t *self);
237 static void disarm_cmd_timer(ev_mc_t *self);
238 static uint32_t mc_enqueue_cmd(pTHX_ ev_mc_t *self,
239 uint8_t opcode, const char *key, STRLEN key_len,
240 const char *value, STRLEN value_len,
241 const char *extras, uint8_t extras_len,
242 uint64_t cas, int cmd, int quiet, SV *cb);
243 static void start_reading(ev_mc_t *self);
244 static void stop_reading(ev_mc_t *self);
245 static void start_writing(ev_mc_t *self);
246 static void stop_writing(ev_mc_t *self);
247 static void start_connect(pTHX_ ev_mc_t *self);
248 static void cleanup_connection(pTHX_ ev_mc_t *self);
249 static void emit_error(pTHX_ ev_mc_t *self, const char *msg);
250 static void handle_disconnect(pTHX_ ev_mc_t *self, const char *reason);
251 static void schedule_reconnect(pTHX_ ev_mc_t *self);
252 static void apply_keepalive(ev_mc_t *self);
253 static void report_connect_error(pTHX_ ev_mc_t *self, const char *errbuf);
254 static void finish_connect_success(pTHX_ ev_mc_t *self);
255 static void mc_send_sasl_auth(pTHX_ ev_mc_t *self, SV *cb);
256 static void stop_connect_timer(ev_mc_t *self);
257 static void stop_reconnect_timer(ev_mc_t *self);
258 static void stop_waiting_timer(ev_mc_t *self);
259 static void send_next_waiting(pTHX_ ev_mc_t *self);
260 static int check_destroyed(ev_mc_t *self);
261 static void cancel_pending(pTHX_ ev_mc_t *self, SV *err_sv);
262 static void cancel_waiting(pTHX_ ev_mc_t *self, SV *err_sv);
263  
264 /* ================================================================
265 * Binary protocol helpers (portable, no unaligned access)
266 * ================================================================ */
267  
268 38 static void mc_write_u16(char *buf, uint16_t val) {
269 38 val = htons(val);
270 38 memcpy(buf, &val, 2);
271 38 }
272  
273 44 static void mc_write_u32(char *buf, uint32_t val) {
274 44 val = htonl(val);
275 44 memcpy(buf, &val, 4);
276 44 }
277  
278 19 static void mc_write_u64(char *buf, uint64_t val) {
279 19 uint32_t hi = htonl((uint32_t)(val >> 32));
280 19 uint32_t lo = htonl((uint32_t)(val & 0xFFFFFFFF));
281 19 memcpy(buf, &hi, 4);
282 19 memcpy(buf + 4, &lo, 4);
283 19 }
284  
285 20 static uint16_t mc_read_u16(const char *buf) {
286 uint16_t val;
287 20 memcpy(&val, buf, 2);
288 20 return ntohs(val);
289 }
290  
291 30 static uint32_t mc_read_u32(const char *buf) {
292 uint32_t val;
293 30 memcpy(&val, buf, 4);
294 30 return ntohl(val);
295 }
296  
297 10 static uint64_t mc_read_u64(const char *buf) {
298 uint32_t hi, lo;
299 10 memcpy(&hi, buf, 4);
300 10 memcpy(&lo, buf + 4, 4);
301 10 return ((uint64_t)ntohl(hi) << 32) | ntohl(lo);
302 }
303  
304 19 static void mc_encode_header(char *buf, uint8_t opcode, uint16_t key_len,
305 uint8_t extras_len, uint32_t body_len, uint32_t opaque, uint64_t cas)
306 {
307 19 buf[0] = MC_REQ_MAGIC;
308 19 buf[1] = opcode;
309 19 mc_write_u16(buf + 2, key_len);
310 19 buf[4] = extras_len;
311 19 buf[5] = 0; /* data_type = raw */
312 19 mc_write_u16(buf + 6, 0); /* vbucket / reserved */
313 19 mc_write_u32(buf + 8, body_len);
314 19 mc_write_u32(buf + 12, opaque);
315 19 mc_write_u64(buf + 16, cas);
316 19 }
317  
318 /* ================================================================
319 * Status code to string
320 * ================================================================ */
321  
322 0 static const char* mc_status_str(uint16_t status) {
323 0 switch (status) {
324 0 case MC_STATUS_OK: return "OK";
325 0 case MC_STATUS_KEY_NOT_FOUND: return "NOT_FOUND";
326 0 case MC_STATUS_KEY_EXISTS: return "EXISTS";
327 0 case MC_STATUS_VALUE_TOO_LARGE: return "VALUE_TOO_LARGE";
328 0 case MC_STATUS_INVALID_ARGS: return "INVALID_ARGUMENTS";
329 0 case MC_STATUS_NOT_STORED: return "NOT_STORED";
330 0 case MC_STATUS_NON_NUMERIC: return "NON_NUMERIC_VALUE";
331 0 case MC_STATUS_AUTH_ERROR: return "AUTH_ERROR";
332 0 case MC_STATUS_AUTH_CONTINUE: return "AUTH_CONTINUE";
333 0 case MC_STATUS_UNKNOWN_CMD: return "UNKNOWN_COMMAND";
334 0 case MC_STATUS_OUT_OF_MEMORY: return "OUT_OF_MEMORY";
335 0 default: return "UNKNOWN_ERROR";
336 }
337 }
338  
339 /* ================================================================
340 * Callback argument validation
341 * ================================================================ */
342  
343 /* Command-callback argument: undef = no callback; a CODE ref = callback;
344 any other ref is a usage error. Defined non-refs are NOT callbacks
345 (trailing positional args in the sniffing XSUBs) and return NULL. */
346 39 static SV* mc_cb_arg(pTHX_ SV *sv) {
347 39 ▶ if (!SvOK(sv)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return NULL; 0 39 39
348 39 ▶ if (!SvROK(sv)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) return NULL; 1 38 39
349 38 ▶ if (SvTYPE(SvRV(sv)) != SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(sv)) != SVt_PVCV) 20 18 38
350 20 croak("callback must be a code reference");
351 18 return sv;
352 }
353  
354 /* on_* handler argument: undef = clear; anything else must be a CODE
355 ref. Returns the validated SV or NULL. */
356 52 static SV* mc_handler_arg(pTHX_ SV *sv) {
357 52 ▶ if (!SvOK(sv)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return NULL; 3 49 52
358 49 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) return sv;
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) return sv; 49 0 49
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) return sv; 47 2 49
359 2 croak("callback must be a code reference");
360 }
361  
362 /* ================================================================
363 * Buffer management
364 * ================================================================ */
365  
366 15 static void buf_ensure_write(ev_mc_t *self, size_t needed) {
367 /* Compact first: reclaim any already-sent prefix. The capacity check
368 below short-circuits the "compaction alone sufficed" case. */
369 15 ▶ if (self->wbuf_off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_off > 0) { 0 15 15
370 0 size_t live = self->wbuf_len - self->wbuf_off;
371 0 memmove(self->wbuf, self->wbuf + self->wbuf_off, live);
372 0 self->wbuf_len = live;
373 0 self->wbuf_off = 0;
374 }
375 15 size_t total = self->wbuf_len + needed;
376 15 ▶ if (self->wbuf_cap >= total) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_cap >= total) return; 15 0 15
377 0 ▶ size_t new_cap = self->wbuf_cap ? self->wbuf_cap : BUF_INIT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = self->wbuf_cap ? self->wbuf_cap : BUF_INIT_SIZE; 0 0 0
378 0 ▶ while (new_cap < total) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < total) new_cap *= 2; 0 0 0
379 0 Renew(self->wbuf, new_cap, char);
380 0 self->wbuf_cap = new_cap;
381 }
382  
383 11 static void buf_ensure_read(ev_mc_t *self, size_t needed) {
384 11 size_t total = self->rbuf_len + needed;
385 11 ▶ if (self->rbuf_cap >= total) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_cap >= total) return; 11 0 11
386 0 ▶ size_t new_cap = self->rbuf_cap ? self->rbuf_cap : BUF_INIT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = self->rbuf_cap ? self->rbuf_cap : BUF_INIT_SIZE; 0 0 0
387 0 ▶ while (new_cap < total) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < total) new_cap *= 2; 0 0 0
388 0 Renew(self->rbuf, new_cap, char);
389 0 self->rbuf_cap = new_cap;
390 }
391  
392 8 static void buf_append_write(ev_mc_t *self, const char *data, size_t len) {
393 8 buf_ensure_write(self, len);
394 8 memcpy(self->wbuf + self->wbuf_len, data, len);
395 8 self->wbuf_len += len;
396 8 }
397  
398 /* ================================================================
399 * Watcher helpers
400 * ================================================================ */
401  
402 13 static void start_reading(ev_mc_t *self) {
403 13 ▶ if (!self->reading && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->reading && self->fd >= 0) { 13 0 13
if (!self->reading && self->fd >= 0) { 13 0 13
404 13 ev_io_start(self->loop, &self->rio);
405 13 self->reading = 1;
406 }
407 13 }
408  
409 24 static void stop_reading(ev_mc_t *self) {
410 24 ▶ if (self->reading) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reading) { 13 11 24
411 13 ev_io_stop(self->loop, &self->rio);
412 13 self->reading = 0;
413 }
414 24 }
415  
416 36 static void start_writing(ev_mc_t *self) {
417 36 ▶ if (!self->writing && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->writing && self->fd >= 0) { 33 3 36
if (!self->writing && self->fd >= 0) { 33 0 33
418 33 ev_io_start(self->loop, &self->wio);
419 33 self->writing = 1;
420 }
421 36 }
422  
423 53 static void stop_writing(ev_mc_t *self) {
424 53 ▶ if (self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->writing) { 33 20 53
425 33 ev_io_stop(self->loop, &self->wio);
426 33 self->writing = 0;
427 }
428 53 }
429  
430 78 static int check_destroyed(ev_mc_t *self) {
431 78 ▶ if (self->magic == MC_MAGIC_FREED && self->callback_depth == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED && self->callback_depth == 0) { 1 77 78
if (self->magic == MC_MAGIC_FREED && self->callback_depth == 0) { 1 0 1
432 1 Safefree(self);
433 1 return 1;
434 }
435 77 return 0;
436 }
437  
438 /* ================================================================
439 * Callback invocation
440 * ================================================================ */
441  
442 /* Invoke Perl callback with two arguments.
443 * result and error are newly created SVs (refcount=1) that become mortal.
444 * Pass NULL for undef. */
445 17 static void invoke_cb(pTHX_ ev_mc_t *self, SV *cb, SV *result, SV *error) {
446 17 ▶ if (!cb) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cb) return; 0 17 17
447  
448 17 self->callback_depth++;
449  
450 17 dSP;
451 17 ENTER;
452 17 SAVETMPS;
453 17 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 17 17
454 17 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 17 17
455 17 ▶ if (result)
Branch: 1 branch
BranchTrueFalseTotal
if (result) 8 9 17
456 8 mPUSHs(result);
457 else
458 9 PUSHs(&PL_sv_undef);
459 17 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 9 8 17
460 9 mPUSHs(error);
461 else
462 8 PUSHs(&PL_sv_undef);
463 17 PUTBACK;
464 17 call_sv(cb, G_DISCARD | G_EVAL);
465 17 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 17 0 17
if (SvTRUE(ERRSV)) { 0 17 17
466 0 ▶ warn("EV::Memcached: callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Memcached: callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
467 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
468 }
469 17 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 17 0 17
470 17 LEAVE;
471  
472 17 self->callback_depth--;
473 }
474  
475 /* Invoke a user handler with at most one mortal arg; catch exceptions
476 so a die in user code can't unwind through libev. */
477 27 static void invoke_handler(pTHX_ ev_mc_t *self, SV *cb, SV *arg, const char *label) {
478 27 ▶ if (NULL == cb) { if (arg) SvREFCNT_dec(arg); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == cb) { if (arg) SvREFCNT_dec(arg); return; } 7 20 27
if (NULL == cb) { if (arg) SvREFCNT_dec(arg); return; } 0 7 7
479 /* pin: the callback may clear its own handler ($mc->on_error(undef)),
480 which would otherwise free the CV while call_sv is still running it */
481 20 SvREFCNT_inc_simple_void_NN(cb);
482  
483 20 self->callback_depth++;
484  
485 20 dSP;
486 20 ENTER;
487 20 SAVETMPS;
488 20 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 20 20
489 20 ▶ if (arg) XPUSHs(sv_2mortal(arg));
Branch: 2 branches
BranchTrueFalseTotal
if (arg) XPUSHs(sv_2mortal(arg)); 9 11 20
if (arg) XPUSHs(sv_2mortal(arg)); 0 9 9
490 20 PUTBACK;
491 20 call_sv(cb, G_DISCARD | G_EVAL);
492 20 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 20 0 20
if (SvTRUE(ERRSV)) { 0 20 20
493 0 ▶ warn("EV::Memcached: %s callback error: %s", label, SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Memcached: %s callback error: %s", label, SvPV_nolen(ERRSV)); 0 0 0
494 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
495 }
496 20 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 9 11 20
497 20 LEAVE;
498  
499 20 self->callback_depth--;
500 20 SvREFCNT_dec(cb);
501 }
502  
503 9 static void emit_error(pTHX_ ev_mc_t *self, const char *msg) {
504 9 invoke_handler(aTHX_ self, self->on_error, newSVpv(msg, 0), "on_error");
505 9 }
506  
507 13 static void emit_connect(pTHX_ ev_mc_t *self) {
508 13 invoke_handler(aTHX_ self, self->on_connect, NULL, "on_connect");
509 13 }
510  
511 5 static void emit_disconnect(pTHX_ ev_mc_t *self) {
512 5 invoke_handler(aTHX_ self, self->on_disconnect, NULL, "on_disconnect");
513 5 }
514  
515 13 static void apply_keepalive(ev_mc_t *self) {
516 13 ▶ if (self->keepalive <= 0 || self->path) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->keepalive <= 0 || self->path) return; 0 13 13
if (self->keepalive <= 0 || self->path) return; 0 0 0
517 0 int one = 1;
518 0 setsockopt(self->fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
519 #ifdef TCP_KEEPIDLE
520 0 setsockopt(self->fd, IPPROTO_TCP, TCP_KEEPIDLE,
521 0 &self->keepalive, sizeof(self->keepalive));
522 #endif
523 }
524  
525 /* Common tail for synchronous connect-failure paths in start_connect:
526 emit error, run pending callbacks, and arm reconnect if configured.
527 Caller returns immediately after invoking. */
528 5 static void report_connect_error(pTHX_ ev_mc_t *self, const char *errbuf) {
529 5 self->callback_depth++;
530 5 emit_error(aTHX_ self, errbuf);
531 5 self->callback_depth--;
532 5 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 5 5
533 5 ▶ if (!self->intentional_disconnect && self->reconnect) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->reconnect) { 5 0 5
if (!self->intentional_disconnect && self->reconnect) { 3 2 5
534 3 schedule_reconnect(aTHX_ self);
535 } else {
536 /* terminal failure: waiting commands would otherwise hang until DESTROY */
537 2 self->callback_depth++;
538 2 cancel_waiting(aTHX_ self, err_disconnected);
539 2 self->callback_depth--;
540 2 check_destroyed(self);
541 }
542 }
543  
544 /* Shared post-connect-success path. Caller must already have set
545 self->connected = 1 and stopped/initialized the io watchers. */
546 13 static void finish_connect_success(pTHX_ ev_mc_t *self) {
547 13 self->reconnect_attempts = 0;
548  
549 13 start_reading(self);
550 13 apply_keepalive(self);
551  
552 13 ▶ if (self->username && self->password) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->username && self->password) { 0 13 13
if (self->username && self->password) { 0 0 0
553 /* Auto-auth: on_connect fires from the SASL_AUTH success path;
554 on failure the connection drops without it ever firing. */
555 0 mc_send_sasl_auth(aTHX_ self, NULL);
556 0 return;
557 }
558  
559 13 emit_connect(aTHX_ self);
560 13 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 13 13
561  
562 13 send_next_waiting(aTHX_ self);
563 }
564  
565 /* ================================================================
566 * Callback entry management
567 * ================================================================ */
568  
569 14 static ev_mc_cb_t* alloc_cbt(void) {
570 ev_mc_cb_t *cbt;
571 14 Newxz(cbt, 1, ev_mc_cb_t);
572 14 return cbt;
573 }
574  
575 14 static void cleanup_cbt(pTHX_ ev_mc_cb_t *cbt) {
576 14 ▶ CLEAR_HANDLER(cbt->cb);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(cbt->cb); 12 2 14
577 14 ▶ if (cbt->stats_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->stats_hv) { 0 14 14
578 0 SvREFCNT_dec((SV*)cbt->stats_hv);
579 0 cbt->stats_hv = NULL;
580 }
581 14 ▶ if ((cbt->cmd == CB_CMD_MGET_FENCE || cbt->cmd == CB_CMD_MGETS_FENCE) && cbt->mget_results) {
Branch: 3 branches
BranchTrueFalseTotal
if ((cbt->cmd == CB_CMD_MGET_FENCE || cbt->cmd == CB_CMD_MGETS_FENCE) && cbt->mget_results) { 14 0 14
if ((cbt->cmd == CB_CMD_MGET_FENCE || cbt->cmd == CB_CMD_MGETS_FENCE) && cbt->mget_results) { 0 14 14
if ((cbt->cmd == CB_CMD_MGET_FENCE || cbt->cmd == CB_CMD_MGETS_FENCE) && cbt->mget_results) { 0 0 0
582 0 SvREFCNT_dec((SV*)cbt->mget_results);
583 0 cbt->mget_results = NULL;
584 }
585 /* MGET_ENTRY has borrowed ref - don't decrement */
586 14 Safefree(cbt);
587 14 }
588  
589 4 static void cleanup_wait(pTHX_ ev_mc_wait_t *wt) {
590 4 ▶ CLEAR_HANDLER(wt->cb);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(wt->cb); 4 0 4
591 4 ▶ if (wt->packet) { Safefree(wt->packet); wt->packet = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (wt->packet) { Safefree(wt->packet); wt->packet = NULL; } 4 0 4
592 4 ▶ if (wt->stats_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (wt->stats_hv) { 0 4 4
593 0 SvREFCNT_dec((SV*)wt->stats_hv);
594 0 wt->stats_hv = NULL;
595 }
596 4 ▶ if ((wt->cmd == CB_CMD_MGET_FENCE || wt->cmd == CB_CMD_MGETS_FENCE) && wt->mget_results) {
Branch: 3 branches
BranchTrueFalseTotal
if ((wt->cmd == CB_CMD_MGET_FENCE || wt->cmd == CB_CMD_MGETS_FENCE) && wt->mget_results) { 4 0 4
if ((wt->cmd == CB_CMD_MGET_FENCE || wt->cmd == CB_CMD_MGETS_FENCE) && wt->mget_results) { 0 4 4
if ((wt->cmd == CB_CMD_MGET_FENCE || wt->cmd == CB_CMD_MGETS_FENCE) && wt->mget_results) { 0 0 0
597 0 SvREFCNT_dec((SV*)wt->mget_results);
598 0 wt->mget_results = NULL;
599 }
600 4 Safefree(wt);
601 4 }
602  
603 /* Free every queued cb/wait entry WITHOUT invoking Perl callbacks.
604 * Used by the DESTROY teardown paths (deferred and global destruction). */
605 6 static void free_all_queues(pTHX_ ev_mc_t *self) {
606 7 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 1 6 7
607 1 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
608 1 ev_mc_cb_t *cbt = ngx_queue_data(q, ev_mc_cb_t, queue);
609 1 ngx_queue_remove(q);
610 1 cleanup_cbt(aTHX_ cbt);
611 }
612 6 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 0 6 6
613 0 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
614 0 ev_mc_wait_t *wt = ngx_queue_data(q, ev_mc_wait_t, queue);
615 0 ngx_queue_remove(q);
616 0 cleanup_wait(aTHX_ wt);
617 }
618 6 self->pending_count = 0;
619 6 self->waiting_count = 0;
620 6 }
621  
622 /* ================================================================
623 * Cancel pending/waiting commands (on disconnect)
624 * ================================================================ */
625  
626 /* Snapshot-drain cancellation: the live queue is spliced into a local
627 * list first, so commands enqueued by cancellation callbacks survive.
628 * The _list halves let handle_disconnect snapshot BOTH queues before
629 * any callback runs. Callers MUST bump callback_depth before calling
630 * these (and check_destroyed after): DESTROY sets magic=FREED before
631 * running us, so a check_destroyed here would Safefree(self) mid-loop. */
632 19 static void cancel_pending_list(pTHX_ ev_mc_t *self, ngx_queue_t *local, SV *err_sv) {
633 22 ▶ while (!ngx_queue_empty(local)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(local)) { 3 19 22
634 3 ngx_queue_t *q = ngx_queue_head(local);
635 3 ev_mc_cb_t *cbt = ngx_queue_data(q, ev_mc_cb_t, queue);
636 3 ngx_queue_remove(q);
637 3 ▶ if (cbt->counted) { self->pending_count--; cbt->counted = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->counted) { self->pending_count--; cbt->counted = 0; } 3 0 3
638  
639 3 ▶ if (cbt->cb && !cbt->skipped) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->cb && !cbt->skipped) { 3 0 3
if (cbt->cb && !cbt->skipped) { 3 0 3
640 3 invoke_cb(aTHX_ self, cbt->cb, NULL, newSVsv(err_sv));
641 3 ▶ if (self->magic == MC_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED) { 0 3 3
642 /* parked entries are invisible to free_all_queues */
643 0 cleanup_cbt(aTHX_ cbt);
644 0 ▶ while (!ngx_queue_empty(local)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(local)) { 0 0 0
645 0 ngx_queue_t *r = ngx_queue_head(local);
646 0 ev_mc_cb_t *rest = ngx_queue_data(r, ev_mc_cb_t, queue);
647 0 ngx_queue_remove(r);
648 0 ▶ if (rest->counted) { rest->counted = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rest->counted) { rest->counted = 0; } 0 0 0
649 0 cleanup_cbt(aTHX_ rest);
650 }
651 0 return;
652 }
653 }
654 3 cleanup_cbt(aTHX_ cbt);
655 }
656 }
657  
658 13 static void cancel_pending(pTHX_ ev_mc_t *self, SV *err_sv) {
659 13 ▶ if (self->in_cb_cleanup) return; /* nested: outer scope owns the splice */
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_cb_cleanup) return; /* nested: outer scope owns the splice */ 0 13 13
660 13 self->in_cb_cleanup = 1;
661  
662 ngx_queue_t local;
663 13 ngx_queue_init(&local);
664 14 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 1 13 14
665 1 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
666 1 ngx_queue_remove(q);
667 1 ngx_queue_insert_tail(&local, q);
668 }
669  
670 13 cancel_pending_list(aTHX_ self, &local, err_sv);
671 13 self->in_cb_cleanup = 0;
672 }
673  
674 /* skip_pending: mark + detach first (no Perl calls, queue immutable),
675 then fire the detached callbacks. Entries stay on cb_queue so their
676 late responses are consumed by FIFO opaque matching and discarded;
677 commands enqueued by the callbacks arrive unmarked and survive. */
678 2 static void skip_pending_impl(pTHX_ ev_mc_t *self, SV *err_sv) {
679 2 ▶ if (self->in_cb_cleanup) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_cb_cleanup) return; 0 2 2
680 2 self->in_cb_cleanup = 1;
681  
682 /* Phase 1: mark + detach */
683 2 SV **cbs = NULL;
684 2 int n = 0, cap = 0;
685 2 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
686 4 ▶ while (q != ngx_queue_sentinel(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (q != ngx_queue_sentinel(&self->cb_queue)) { 2 2 4
687 2 ev_mc_cb_t *cbt = ngx_queue_data(q, ev_mc_cb_t, queue);
688 2 q = ngx_queue_next(q);
689 2 ▶ if (cbt->skipped) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->skipped) continue; 0 2 2
690 2 cbt->skipped = 1;
691 2 ▶ if (cbt->counted) { self->pending_count--; cbt->counted = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->counted) { self->pending_count--; cbt->counted = 0; } 2 0 2
692 2 ▶ if (cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) { 2 0 2
693 2 ▶ if (n == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == cap) { 2 0 2
694 2 ▶ cap = cap ? cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
cap = cap ? cap * 2 : 8; 0 2 2
695 2 Renew(cbs, cap, SV *);
696 }
697 2 cbs[n++] = cbt->cb;
698 2 cbt->cb = NULL;
699 }
700 }
701  
702 /* Phase 2: fire detached callbacks; entries stay owned by the queue */
703 2 unsigned int gen = self->conn_gen;
704 int i;
705 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 2 4
706 2 invoke_cb(aTHX_ self, cbs[i], NULL, newSVsv(err_sv));
707 2 SvREFCNT_dec(cbs[i]);
708 2 ▶ if (self->magic == MC_MAGIC_FREED || self->conn_gen != gen) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED || self->conn_gen != gen) { 2 0 2
if (self->magic == MC_MAGIC_FREED || self->conn_gen != gen) { 0 2 2
709 /* queue torn down by the callback: release the rest */
710 0 ▶ for (i++; i < n; i++) SvREFCNT_dec(cbs[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i++; i < n; i++) SvREFCNT_dec(cbs[i]); 0 0 0
711 0 break;
712 }
713 }
714 2 Safefree(cbs);
715 2 self->in_cb_cleanup = 0;
716 }
717  
718 22 static void cancel_waiting_list(pTHX_ ev_mc_t *self, ngx_queue_t *local, SV *err_sv) {
719 25 ▶ while (!ngx_queue_empty(local)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(local)) { 3 22 25
720 3 ngx_queue_t *q = ngx_queue_head(local);
721 3 ev_mc_wait_t *wt = ngx_queue_data(q, ev_mc_wait_t, queue);
722 3 ngx_queue_remove(q);
723 3 self->waiting_count--;
724  
725 3 ▶ if (wt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (wt->cb) { 3 0 3
726 3 invoke_cb(aTHX_ self, wt->cb, NULL, newSVsv(err_sv));
727 3 ▶ if (self->magic == MC_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED) { 0 3 3
728 0 cleanup_wait(aTHX_ wt);
729 /* parked entries are invisible to free_all_queues */
730 0 ▶ while (!ngx_queue_empty(local)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(local)) { 0 0 0
731 0 ngx_queue_t *r = ngx_queue_head(local);
732 0 ev_mc_wait_t *rest = ngx_queue_data(r, ev_mc_wait_t, queue);
733 0 ngx_queue_remove(r);
734 0 cleanup_wait(aTHX_ rest);
735 }
736 0 return;
737 }
738 }
739 3 cleanup_wait(aTHX_ wt);
740 }
741 }
742  
743 16 static void cancel_waiting(pTHX_ ev_mc_t *self, SV *err_sv) {
744 16 ▶ if (self->in_wait_cleanup) return; /* nested: outer scope owns the splice */
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_wait_cleanup) return; /* nested: outer scope owns the splice */ 0 16 16
745 16 self->in_wait_cleanup = 1;
746  
747 /* Snapshot-drain: commands queued by cancellation callbacks land on
748 the (now empty) live wait_queue and survive. */
749 ngx_queue_t local;
750 16 ngx_queue_init(&local);
751 18 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 2 16 18
752 2 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
753 2 ngx_queue_remove(q);
754 2 ngx_queue_insert_tail(&local, q);
755 }
756  
757 16 cancel_waiting_list(aTHX_ self, &local, err_sv);
758 16 self->in_wait_cleanup = 0;
759 }
760  
761 /* ================================================================
762 * Connection cleanup and disconnect handling
763 * ================================================================ */
764  
765 23 static void cleanup_connection(pTHX_ ev_mc_t *self) {
766 23 self->conn_gen++; /* invalidates in-flight session state (see process_responses) */
767  
768 23 stop_reading(self);
769 23 stop_writing(self);
770  
771 23 stop_connect_timer(self);
772 23 disarm_cmd_timer(self);
773 23 stop_waiting_timer(self);
774  
775 23 ▶ if (self->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->fd >= 0) { 15 8 23
776 15 close(self->fd);
777 15 self->fd = -1;
778 }
779  
780 23 self->connected = 0;
781 23 self->connecting = 0;
782 23 self->rbuf_len = 0;
783 23 self->wbuf_len = 0;
784 23 self->wbuf_off = 0;
785 23 }
786  
787 6 static void handle_disconnect(pTHX_ ev_mc_t *self, const char *reason) {
788 6 int was_connected = self->connected;
789  
790 6 cleanup_connection(aTHX_ self);
791  
792 /* Snapshot BOTH queues before any teardown callback runs: commands
793 enqueued by cancellation callbacks land on the live queues and
794 survive. The flags stay set across the span so nested cancels
795 no-op instead of splicing (and stranding) a fresh local list. */
796 ngx_queue_t localP, localW;
797 6 int haveP = 0, haveW = 0;
798  
799 6 ▶ if (!self->in_cb_cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->in_cb_cleanup) { 6 0 6
800 6 self->in_cb_cleanup = 1;
801 6 haveP = 1;
802 6 ngx_queue_init(&localP);
803 8 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 2 6 8
804 2 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
805 2 ngx_queue_remove(q);
806 2 ngx_queue_insert_tail(&localP, q);
807 }
808 }
809 6 ▶ if (!self->resume_waiting_on_reconnect && !self->in_wait_cleanup) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->resume_waiting_on_reconnect && !self->in_wait_cleanup) { 6 0 6
if (!self->resume_waiting_on_reconnect && !self->in_wait_cleanup) { 6 0 6
810 6 self->in_wait_cleanup = 1;
811 6 haveW = 1;
812 6 ngx_queue_init(&localW);
813 7 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 1 6 7
814 1 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
815 1 ngx_queue_remove(q);
816 1 ngx_queue_insert_tail(&localW, q);
817 }
818 }
819  
820 6 ▶ if (haveP) {
Branch: 1 branch
BranchTrueFalseTotal
if (haveP) { 6 0 6
821 6 cancel_pending_list(aTHX_ self, &localP, err_disconnected);
822 6 self->in_cb_cleanup = 0;
823 }
824 6 ▶ if (haveW) {
Branch: 1 branch
BranchTrueFalseTotal
if (haveW) { 6 0 6
825 6 ▶ if (self->magic == MC_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED) { 0 6 6
826 /* died during the pending drain: free parked entries silently */
827 0 ▶ while (!ngx_queue_empty(&localW)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&localW)) { 0 0 0
828 0 ngx_queue_t *q = ngx_queue_head(&localW);
829 0 ev_mc_wait_t *wt = ngx_queue_data(q, ev_mc_wait_t, queue);
830 0 ngx_queue_remove(q);
831 0 self->waiting_count--;
832 0 cleanup_wait(aTHX_ wt);
833 }
834 } else {
835 6 cancel_waiting_list(aTHX_ self, &localW, err_disconnected);
836 }
837 6 self->in_wait_cleanup = 0;
838 }
839 6 ▶ if (self->magic == MC_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED) return; 0 6 6
840  
841 6 ▶ if (was_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if (was_connected) { 5 1 6
842 5 emit_disconnect(aTHX_ self);
843 5 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 5 5
844 }
845  
846 6 ▶ if (reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (reason) { 3 3 6
847 3 emit_error(aTHX_ self, reason);
848 3 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 3 3
849 }
850  
851 6 ▶ if (!self->intentional_disconnect && self->reconnect) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->reconnect) { 5 1 6
if (!self->intentional_disconnect && self->reconnect) { 0 5 5
852 0 schedule_reconnect(aTHX_ self);
853 }
854 }
855  
856 /* ================================================================
857 * Reconnection
858 * ================================================================ */
859  
860 3 static void schedule_reconnect(pTHX_ ev_mc_t *self) {
861 3 ▶ if (self->reconnect_timer_active) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timer_active) return; 0 3 3
862 3 ▶ if (self->max_reconnect_attempts > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (self->max_reconnect_attempts > 0 && 3 0 3
863 3 ▶ self->reconnect_attempts >= self->max_reconnect_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
self->reconnect_attempts >= self->max_reconnect_attempts) { 1 2 3
864 /* giving up for good: the waiting queue must not hang forever */
865 1 self->callback_depth++;
866 1 emit_error(aTHX_ self, "max reconnect attempts reached");
867 1 ▶ if (self->magic != MC_MAGIC_FREED)
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != MC_MAGIC_FREED) 1 0 1
868 1 cancel_waiting(aTHX_ self, err_disconnected);
869 1 self->callback_depth--;
870 1 check_destroyed(self);
871 1 return;
872 }
873  
874 2 self->reconnect_attempts++;
875  
876 /* Always defer through a timer (even with delay==0) so that an
877 immediate connect failure cannot recurse:
878 schedule_reconnect -> start_connect -> report_connect_error ->
879 schedule_reconnect -> ... blowing the C stack. */
880 2 ev_tstamp delay = (ev_tstamp)self->reconnect_delay_ms / 1000.0;
881 2 ▶ if (delay < 0) delay = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay < 0) delay = 0; 0 2 2
882 2 ev_timer_init(&self->reconnect_timer, reconnect_timer_cb, delay, 0.0);
883 2 self->reconnect_timer.data = (void *)self;
884 2 ev_timer_start(self->loop, &self->reconnect_timer);
885 2 self->reconnect_timer_active = 1;
886 }
887  
888 14 static void arm_cmd_timer(ev_mc_t *self) {
889 14 ▶ if (self->command_timeout_ms <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->command_timeout_ms <= 0) return; 13 1 14
890 1 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 1 1
891 1 ev_tstamp timeout = (ev_tstamp)self->command_timeout_ms / 1000.0;
892 1 ▶ if (self->cmd_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cmd_timer_active) { 0 1 1
893 /* Adjust repeat in-place; ev_timer_again restarts using the new value.
894 ev_timer_set is forbidden on an active watcher per libev docs. */
895 0 self->cmd_timer.repeat = timeout;
896 0 ev_timer_again(self->loop, &self->cmd_timer);
897 } else {
898 1 ev_timer_init(&self->cmd_timer, cmd_timeout_cb, timeout, timeout);
899 1 self->cmd_timer.data = (void *)self;
900 1 ev_timer_start(self->loop, &self->cmd_timer);
901 1 self->cmd_timer_active = 1;
902 }
903 }
904  
905 42 static void disarm_cmd_timer(ev_mc_t *self) {
906 42 ▶ if (self->cmd_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cmd_timer_active) { 1 41 42
907 1 ev_timer_stop(self->loop, &self->cmd_timer);
908 1 self->cmd_timer_active = 0;
909 }
910 42 }
911  
912 59 static void stop_connect_timer(ev_mc_t *self) {
913 59 ▶ if (self->connect_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timer_active) { 0 59 59
914 0 ev_timer_stop(self->loop, &self->connect_timer);
915 0 self->connect_timer_active = 0;
916 }
917 59 }
918  
919 25 static void stop_reconnect_timer(ev_mc_t *self) {
920 25 ▶ if (self->reconnect_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timer_active) { 0 25 25
921 0 ev_timer_stop(self->loop, &self->reconnect_timer);
922 0 self->reconnect_timer_active = 0;
923 }
924 25 }
925  
926 41 static void stop_waiting_timer(ev_mc_t *self) {
927 41 ▶ if (self->waiting_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timer_active) { 0 41 41
928 0 ev_timer_stop(self->loop, &self->waiting_timer);
929 0 self->waiting_timer_active = 0;
930 }
931 41 }
932  
933 1 static void cmd_timeout_cb(EV_P_ ev_timer *w, int revents) {
934 1 ev_mc_t *self = (ev_mc_t *)w->data;
935 (void)loop; (void)revents;
936  
937 1 ▶ if (self->magic != MC_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != MC_MAGIC_ALIVE) return; 0 1 1
938  
939 1 ▶ if (ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) { 0 1 1
940 0 disarm_cmd_timer(self);
941 0 return;
942 }
943  
944 /* Stop the repeating timer before disconnect */
945 1 disarm_cmd_timer(self);
946 1 self->callback_depth++;
947 1 handle_disconnect(aTHX_ self, "command timeout");
948 1 self->callback_depth--;
949 1 check_destroyed(self);
950 }
951  
952 /* Send SASL PLAIN auth. If cb is NULL, creates an internal callback
953 * that disconnects on auth failure. */
954 0 static void mc_send_sasl_auth(pTHX_ ev_mc_t *self, SV *cb) {
955 0 ▶ if (!self->username || !self->password) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self->username || !self->password) return; 0 0 0
if (!self->username || !self->password) return; 0 0 0
956  
957 0 size_t ulen = strlen(self->username);
958 0 size_t plen = strlen(self->password);
959 0 size_t vlen = 1 + ulen + 1 + plen;
960 char *authdata;
961 0 Newx(authdata, vlen, char);
962 0 authdata[0] = '\0';
963 0 memcpy(authdata + 1, self->username, ulen);
964 0 authdata[1 + ulen] = '\0';
965 0 memcpy(authdata + 2 + ulen, self->password, plen);
966  
967 0 mc_enqueue_cmd(aTHX_ self, MC_OP_SASL_AUTH, "PLAIN", 5,
968 authdata, vlen, NULL, 0, 0, CB_CMD_SASL_AUTH, 0, cb);
969 0 Safefree(authdata);
970 }
971  
972 0 static void connect_timeout_cb(EV_P_ ev_timer *w, int revents) {
973 0 ev_mc_t *self = (ev_mc_t *)w->data;
974 (void)loop; (void)revents;
975  
976 0 ▶ if (self->magic != MC_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != MC_MAGIC_ALIVE) return; 0 0 0
977  
978 0 self->connect_timer_active = 0;
979 0 self->callback_depth++;
980 0 handle_disconnect(aTHX_ self, "connect timeout");
981 0 self->callback_depth--;
982 0 check_destroyed(self);
983 }
984  
985 2 static void reconnect_timer_cb(EV_P_ ev_timer *w, int revents) {
986 2 ev_mc_t *self = (ev_mc_t *)w->data;
987 (void)loop; (void)revents;
988  
989 2 ▶ if (self->magic != MC_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != MC_MAGIC_ALIVE) return; 0 2 2
990  
991 2 self->reconnect_timer_active = 0;
992 2 self->callback_depth++;
993 2 start_connect(aTHX_ self);
994 2 self->callback_depth--;
995 2 check_destroyed(self);
996 }
997  
998 /* ================================================================
999 * Flow control: waiting timer
1000 * ================================================================ */
1001  
1002 static void schedule_waiting_timer(ev_mc_t *self);
1003  
1004 1 static void expire_waiting_commands(pTHX_ ev_mc_t *self) {
1005 1 ev_tstamp now = ev_now(self->loop);
1006 1 ev_tstamp timeout = (ev_tstamp)self->waiting_timeout_ms / 1000.0;
1007  
1008 2 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 1 1 2
1009 1 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
1010 1 ev_mc_wait_t *wt = ngx_queue_data(q, ev_mc_wait_t, queue);
1011  
1012 1 ▶ if (wt->queued_at + timeout > now) break; /* not expired yet */
Branch: 1 branch
BranchTrueFalseTotal
if (wt->queued_at + timeout > now) break; /* not expired yet */ 0 1 1
1013  
1014 1 ngx_queue_remove(q);
1015 1 self->waiting_count--;
1016  
1017 1 ▶ if (wt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (wt->cb) { 1 0 1
1018 1 invoke_cb(aTHX_ self, wt->cb, NULL, newSVsv(err_waiting_timeout));
1019 1 ▶ if (self->magic == MC_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED) { 0 1 1
1020 0 cleanup_wait(aTHX_ wt);
1021 0 return;
1022 }
1023 }
1024 1 cleanup_wait(aTHX_ wt);
1025 }
1026 }
1027  
1028 1 static void waiting_timer_cb(EV_P_ ev_timer *w, int revents) {
1029 1 ev_mc_t *self = (ev_mc_t *)w->data;
1030 (void)loop; (void)revents;
1031  
1032 1 ▶ if (self->magic != MC_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != MC_MAGIC_ALIVE) return; 0 1 1
1033  
1034 1 self->waiting_timer_active = 0;
1035 1 self->callback_depth++;
1036 1 expire_waiting_commands(aTHX_ self);
1037 1 self->callback_depth--;
1038 1 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 1 1
1039  
1040 1 ▶ if (!ngx_queue_empty(&self->wait_queue) && self->waiting_timeout_ms > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!ngx_queue_empty(&self->wait_queue) && self->waiting_timeout_ms > 0) 0 1 1
if (!ngx_queue_empty(&self->wait_queue) && self->waiting_timeout_ms > 0) 0 0 0
1041 0 schedule_waiting_timer(self);
1042 }
1043  
1044 1 static void schedule_waiting_timer(ev_mc_t *self) {
1045 1 ▶ if (self->waiting_timer_active) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timer_active) return; 0 1 1
1046 1 ▶ if (self->waiting_timeout_ms <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timeout_ms <= 0) return; 0 1 1
1047 1 ▶ if (ngx_queue_empty(&self->wait_queue)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->wait_queue)) return; 0 1 1
1048  
1049 1 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
1050 1 ev_mc_wait_t *wt = ngx_queue_data(q, ev_mc_wait_t, queue);
1051 1 ev_tstamp timeout = (ev_tstamp)self->waiting_timeout_ms / 1000.0;
1052 1 ev_tstamp delay = (wt->queued_at + timeout) - ev_now(self->loop);
1053 1 ▶ if (delay < 0.0) delay = 0.0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay < 0.0) delay = 0.0; 0 1 1
1054  
1055 1 ev_timer_init(&self->waiting_timer, waiting_timer_cb, delay, 0.0);
1056 1 self->waiting_timer.data = (void *)self;
1057 1 ev_timer_start(self->loop, &self->waiting_timer);
1058 1 self->waiting_timer_active = 1;
1059 }
1060  
1061 /* ================================================================
1062 * Send next waiting command
1063 * ================================================================ */
1064  
1065 22 static void send_next_waiting(pTHX_ ev_mc_t *self) {
1066 30 ▶ while (!ngx_queue_empty(&self->wait_queue) && self->connected) {
Branch: 2 branches
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue) && self->connected) { 10 20 30
while (!ngx_queue_empty(&self->wait_queue) && self->connected) { 9 1 10
1067 9 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
1068 9 ev_mc_wait_t *wt = ngx_queue_data(q, ev_mc_wait_t, queue);
1069  
1070 /* max_pending gates only counted entries; mc_enqueue_cmd uses the
1071 same exception (|| !counted). Fire-and-forget (no_response) and
1072 mget GETKQ entries are uncounted and must not be blocked. */
1073 9 ▶ if (self->max_pending > 0 && self->pending_count >= self->max_pending
Branch: 2 branches
BranchTrueFalseTotal
if (self->max_pending > 0 && self->pending_count >= self->max_pending 1 8 9
if (self->max_pending > 0 && self->pending_count >= self->max_pending 1 0 1
1074 1 ▶ && !wt->no_response && wt->counted)
Branch: 2 branches
BranchTrueFalseTotal
&& !wt->no_response && wt->counted) 1 0 1
&& !wt->no_response && wt->counted) 1 0 1
1075 1 break;
1076  
1077 8 ngx_queue_remove(q);
1078 8 self->waiting_count--;
1079  
1080 /* Append packet to write buffer */
1081 8 buf_append_write(self, wt->packet, wt->packet_len);
1082  
1083 8 ▶ if (!wt->no_response) {
Branch: 1 branch
BranchTrueFalseTotal
if (!wt->no_response) { 7 1 8
1084 7 ev_mc_cb_t *cbt = alloc_cbt();
1085 7 cbt->cb = wt->cb; wt->cb = NULL;
1086 7 cbt->opaque = wt->opaque;
1087 7 cbt->cmd = wt->cmd;
1088 7 cbt->quiet = wt->quiet;
1089 7 cbt->counted = wt->counted;
1090 7 cbt->stats_hv = wt->stats_hv; wt->stats_hv = NULL;
1091 7 cbt->mget_results = wt->mget_results; wt->mget_results = NULL;
1092 7 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
1093 7 ▶ if (cbt->counted) self->pending_count++;
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->counted) self->pending_count++; 7 0 7
1094 7 arm_cmd_timer(self);
1095 }
1096  
1097 8 Safefree(wt->packet);
1098 8 Safefree(wt);
1099  
1100 8 start_writing(self);
1101 }
1102 22 }
1103  
1104 /* ================================================================
1105 * Build and enqueue a command
1106 * ================================================================ */
1107  
1108 /* Allocate the next opaque, skipping 0 across wraparound (0 is reserved
1109 * for fire-and-forget commands so error responses don't get matched to
1110 * the wrong cb_queue head). */
1111 18 static uint32_t mc_next_opaque(ev_mc_t *self) {
1112 18 uint32_t op = self->next_opaque++;
1113 18 ▶ if (self->next_opaque == 0) self->next_opaque = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (self->next_opaque == 0) self->next_opaque = 1; 0 18 18
1114 18 return op;
1115 }
1116  
1117 /* Encode a complete request packet (header + extras + key + value) into
1118 * the caller-supplied buffer p, which must be at least
1119 * MC_HEADER_SIZE + extras_len + key_len + value_len bytes. */
1120 19 static void mc_pack(char *p, uint8_t opcode,
1121 const char *key, uint16_t key_len,
1122 const char *value, uint32_t value_len,
1123 const char *extras, uint8_t extras_len,
1124 uint32_t opaque, uint64_t cas)
1125 {
1126 19 uint32_t body_len = (uint32_t)extras_len + (uint32_t)key_len + value_len;
1127 19 mc_encode_header(p, opcode, key_len, extras_len, body_len, opaque, cas);
1128 19 p += MC_HEADER_SIZE;
1129 19 ▶ if (extras_len > 0) { memcpy(p, extras, extras_len); p += extras_len; }
Branch: 1 branch
BranchTrueFalseTotal
if (extras_len > 0) { memcpy(p, extras, extras_len); p += extras_len; } 3 16 19
1130 19 ▶ if (key_len > 0) { memcpy(p, key, key_len); p += key_len; }
Branch: 1 branch
BranchTrueFalseTotal
if (key_len > 0) { memcpy(p, key, key_len); p += key_len; } 14 5 19
1131 19 ▶ if (value_len > 0) { memcpy(p, value, value_len); }
Branch: 1 branch
BranchTrueFalseTotal
if (value_len > 0) { memcpy(p, value, value_len); } 3 16 19
1132 19 }
1133  
1134 /* Fire-and-forget using a quiet opcode (SETQ/FLUSHQ/...). No cb_queue
1135 * entry; opaque is always 0 (server only responds on error and that
1136 * response is silently discarded by handle_response_packet). */
1137 1 static void mc_fire_and_forget(pTHX_ ev_mc_t *self,
1138 uint8_t opcode, const char *key, STRLEN key_len,
1139 const char *value, STRLEN value_len,
1140 const char *extras, uint8_t extras_len,
1141 uint64_t cas)
1142 {
1143 1 ▶ if (key_len > MC_MAX_KEY_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len > MC_MAX_KEY_LEN) 0 1 1
1144 0 croak("key too long (%d bytes, max %d)", (int)key_len, MC_MAX_KEY_LEN);
1145 1 ▶ if (value_len > MC_MAX_VALUE_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (value_len > MC_MAX_VALUE_LEN) 0 1 1
1146 0 croak("value too large (%"UVuf" bytes, max %"UVuf")",
1147 (UV)value_len, (UV)MC_MAX_VALUE_LEN);
1148  
1149 1 uint32_t body_len = extras_len + (uint32_t)key_len + (uint32_t)value_len;
1150 1 size_t packet_len = MC_HEADER_SIZE + body_len;
1151  
1152 1 ▶ if (self->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) { 0 1 1
1153 0 buf_ensure_write(self, packet_len);
1154 0 mc_pack(self->wbuf + self->wbuf_len, opcode,
1155 0 key, (uint16_t)key_len, value, (uint32_t)value_len,
1156 extras, extras_len, 0, cas);
1157 0 self->wbuf_len += packet_len;
1158 0 start_writing(self);
1159 } else {
1160 /* Not yet connected (or reconnecting): queue and drain after connect. */
1161 ev_mc_wait_t *wt;
1162 1 Newxz(wt, 1, ev_mc_wait_t);
1163 1 Newx(wt->packet, packet_len, char);
1164 1 wt->packet_len = packet_len;
1165 1 mc_pack(wt->packet, opcode,
1166 1 key, (uint16_t)key_len, value, (uint32_t)value_len,
1167 extras, extras_len, 0, cas);
1168 1 wt->no_response = 1;
1169 1 wt->queued_at = ev_now(self->loop);
1170 1 ngx_queue_insert_tail(&self->wait_queue, &wt->queue);
1171 1 self->waiting_count++;
1172 1 ▶ if (self->waiting_timeout_ms > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timeout_ms > 0) 0 1 1
1173 0 schedule_waiting_timer(self);
1174 }
1175 1 }
1176  
1177 /* Build a binary protocol packet and enqueue for sending.
1178 * Returns the assigned opaque. */
1179 18 static uint32_t mc_enqueue_cmd(pTHX_ ev_mc_t *self,
1180 uint8_t opcode, const char *key, STRLEN key_len,
1181 const char *value, STRLEN value_len,
1182 const char *extras, uint8_t extras_len,
1183 uint64_t cas, int cmd, int quiet, SV *cb)
1184 {
1185 18 ▶ if (key_len > MC_MAX_KEY_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len > MC_MAX_KEY_LEN) 0 18 18
1186 0 croak("key too long (%d bytes, max %d)", (int)key_len, MC_MAX_KEY_LEN);
1187 18 ▶ if (value_len > MC_MAX_VALUE_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (value_len > MC_MAX_VALUE_LEN) 0 18 18
1188 0 croak("value too large (%"UVuf" bytes, max %"UVuf")",
1189 (UV)value_len, (UV)MC_MAX_VALUE_LEN);
1190  
1191 18 uint32_t opaque = mc_next_opaque(self);
1192 18 uint32_t body_len = extras_len + (uint32_t)key_len + (uint32_t)value_len;
1193 18 size_t packet_len = MC_HEADER_SIZE + body_len;
1194  
1195 18 ▶ int counted = (cmd != CB_CMD_MGET_ENTRY && cmd != CB_CMD_MGETS_ENTRY);
Branch: 2 branches
BranchTrueFalseTotal
int counted = (cmd != CB_CMD_MGET_ENTRY && cmd != CB_CMD_MGETS_ENTRY); 18 0 18
int counted = (cmd != CB_CMD_MGET_ENTRY && cmd != CB_CMD_MGETS_ENTRY); 18 0 18
1196  
1197 /* Check if we can send immediately */
1198 26 ▶ int can_send = self->connected &&
Branch: 1 branch
BranchTrueFalseTotal
int can_send = self->connected && 8 10 18
1199 8 ▶ (self->max_pending <= 0 || self->pending_count < self->max_pending || !counted);
Branch: 3 branches
BranchTrueFalseTotal
(self->max_pending <= 0 || self->pending_count < self->max_pending || !counted); 2 6 8
(self->max_pending <= 0 || self->pending_count < self->max_pending || !counted); 1 1 2
(self->max_pending <= 0 || self->pending_count < self->max_pending || !counted); 0 1 1
1200  
1201 18 ▶ if (can_send) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_send) { 7 11 18
1202 7 buf_ensure_write(self, packet_len);
1203 7 mc_pack(self->wbuf + self->wbuf_len, opcode,
1204 7 key, (uint16_t)key_len, value, (uint32_t)value_len,
1205 extras, extras_len, opaque, cas);
1206 7 self->wbuf_len += packet_len;
1207  
1208 7 ev_mc_cb_t *cbt = alloc_cbt();
1209 7 ▶ if (cb) { cbt->cb = newSVsv(cb); }
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { cbt->cb = newSVsv(cb); } 7 0 7
1210 7 cbt->opaque = opaque;
1211 7 cbt->cmd = cmd;
1212 7 cbt->quiet = quiet;
1213 7 cbt->counted = counted;
1214 7 ▶ if (cmd == CB_CMD_STATS) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmd == CB_CMD_STATS) { 0 7 7
1215 0 cbt->stats_hv = newHV();
1216 }
1217 7 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
1218 7 ▶ if (counted) self->pending_count++;
Branch: 1 branch
BranchTrueFalseTotal
if (counted) self->pending_count++; 7 0 7
1219  
1220 7 arm_cmd_timer(self);
1221 7 start_writing(self);
1222 } else {
1223 /* Queue for later */
1224 ev_mc_wait_t *wt;
1225 11 Newxz(wt, 1, ev_mc_wait_t);
1226 11 Newx(wt->packet, packet_len, char);
1227 11 wt->packet_len = packet_len;
1228 11 mc_pack(wt->packet, opcode,
1229 11 key, (uint16_t)key_len, value, (uint32_t)value_len,
1230 extras, extras_len, opaque, cas);
1231  
1232 11 ▶ if (cb) { wt->cb = newSVsv(cb); }
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { wt->cb = newSVsv(cb); } 11 0 11
1233 11 wt->opaque = opaque;
1234 11 wt->cmd = cmd;
1235 11 wt->quiet = quiet;
1236 11 wt->counted = counted;
1237 11 wt->queued_at = ev_now(self->loop);
1238 11 ▶ if (cmd == CB_CMD_STATS) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmd == CB_CMD_STATS) { 0 11 11
1239 0 wt->stats_hv = newHV();
1240 }
1241  
1242 11 ngx_queue_insert_tail(&self->wait_queue, &wt->queue);
1243 11 self->waiting_count++;
1244  
1245 11 ▶ if (self->waiting_timeout_ms > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timeout_ms > 0) 0 11 11
1246 0 schedule_waiting_timer(self);
1247 }
1248  
1249 18 return opaque;
1250 }
1251  
1252 /* ================================================================
1253 * Multi-get: GETKQ + NOOP fence
1254 * ================================================================ */
1255  
1256 /* full_info: 0 = mget (key=>value), 1 = mgets (key=>{value,flags,cas}) */
1257 0 static void mc_enqueue_mget(pTHX_ ev_mc_t *self, AV *keys_av, SV *cb, int full_info) {
1258 0 SSize_t count = av_len(keys_av) + 1;
1259 0 ▶ if (count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count <= 0) { 0 0 0
1260 0 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 0 0 0
1261 0 HV *hv = newHV();
1262 0 self->callback_depth++;
1263 0 invoke_cb(aTHX_ self, cb, newRV_noinc((SV*)hv), NULL);
1264 0 self->callback_depth--;
1265 0 check_destroyed(self);
1266 }
1267 0 return;
1268 }
1269  
1270 0 ▶ int entry_cmd = full_info ? CB_CMD_MGETS_ENTRY : CB_CMD_MGET_ENTRY;
Branch: 1 branch
BranchTrueFalseTotal
int entry_cmd = full_info ? CB_CMD_MGETS_ENTRY : CB_CMD_MGET_ENTRY; 0 0 0
1271 0 ▶ int fence_cmd = full_info ? CB_CMD_MGETS_FENCE : CB_CMD_MGET_FENCE;
Branch: 1 branch
BranchTrueFalseTotal
int fence_cmd = full_info ? CB_CMD_MGETS_FENCE : CB_CMD_MGET_FENCE; 0 0 0
1272  
1273 SSize_t i;
1274  
1275 /* Pre-validate all key lengths before mutating wbuf / queues / refcounts.
1276 Croaking mid-loop would leave half-built GETKQ packets queued without
1277 a NOOP fence, corrupting opaque ordering on the wire. */
1278 0 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 0 0 0
1279 0 SV **sv = av_fetch(keys_av, i, 0);
1280 0 ▶ if (!sv || !SvOK(*sv)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(*sv)) continue; 0 0 0
if (!sv || !SvOK(*sv)) continue; 0 0 0
1281 STRLEN key_len;
1282 0 (void)SvPV(*sv, key_len);
1283 0 ▶ if (key_len > MC_MAX_KEY_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len > MC_MAX_KEY_LEN) 0 0 0
1284 0 croak("mget key too long (%d bytes, max %d)", (int)key_len, MC_MAX_KEY_LEN);
1285 }
1286  
1287 0 HV *results = newHV();
1288  
1289 0 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 0 0 0
1290 0 SV **sv = av_fetch(keys_av, i, 0);
1291 0 ▶ if (!sv || !SvOK(*sv)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(*sv)) continue; 0 0 0
if (!sv || !SvOK(*sv)) continue; 0 0 0
1292  
1293 STRLEN key_len;
1294 0 const char *key = SvPV(*sv, key_len);
1295  
1296 0 uint32_t opaque = mc_next_opaque(self);
1297 0 uint32_t body_len = (uint32_t)key_len;
1298 0 size_t packet_len = MC_HEADER_SIZE + body_len;
1299  
1300 0 int can_send = self->connected;
1301  
1302 0 ▶ if (can_send) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_send) { 0 0 0
1303 0 buf_ensure_write(self, packet_len);
1304 0 char *p = self->wbuf + self->wbuf_len;
1305 0 mc_encode_header(p, MC_OP_GETKQ, (uint16_t)key_len, 0, body_len, opaque, 0);
1306 0 memcpy(p + MC_HEADER_SIZE, key, key_len);
1307 0 self->wbuf_len += packet_len;
1308  
1309 0 ev_mc_cb_t *cbt = alloc_cbt();
1310 0 cbt->opaque = opaque;
1311 0 cbt->cmd = entry_cmd;
1312 0 cbt->quiet = 1;
1313 0 cbt->counted = 0;
1314 0 cbt->mget_results = results;
1315 0 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
1316 } else {
1317 ev_mc_wait_t *wt;
1318 0 Newxz(wt, 1, ev_mc_wait_t);
1319 0 Newx(wt->packet, packet_len, char);
1320 0 wt->packet_len = packet_len;
1321 0 mc_encode_header(wt->packet, MC_OP_GETKQ, (uint16_t)key_len, 0, body_len, opaque, 0);
1322 0 memcpy(wt->packet + MC_HEADER_SIZE, key, key_len);
1323 0 wt->opaque = opaque;
1324 0 wt->cmd = entry_cmd;
1325 0 wt->quiet = 1;
1326 0 wt->counted = 0;
1327 0 wt->mget_results = results;
1328 0 wt->queued_at = ev_now(self->loop);
1329 0 ngx_queue_insert_tail(&self->wait_queue, &wt->queue);
1330 0 self->waiting_count++;
1331 }
1332 }
1333  
1334 /* NOOP fence */
1335 {
1336 0 uint32_t opaque = mc_next_opaque(self);
1337 0 size_t packet_len = MC_HEADER_SIZE;
1338 0 int can_send = self->connected;
1339  
1340 0 ▶ if (can_send) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_send) { 0 0 0
1341 0 buf_ensure_write(self, packet_len);
1342 0 char *p = self->wbuf + self->wbuf_len;
1343 0 mc_encode_header(p, MC_OP_NOOP, 0, 0, 0, opaque, 0);
1344 0 self->wbuf_len += packet_len;
1345  
1346 0 ev_mc_cb_t *cbt = alloc_cbt();
1347 0 ▶ if (cb) { cbt->cb = newSVsv(cb); }
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { cbt->cb = newSVsv(cb); } 0 0 0
1348 0 cbt->opaque = opaque;
1349 0 cbt->cmd = fence_cmd;
1350 0 cbt->quiet = 0;
1351 0 cbt->counted = 1;
1352 0 cbt->mget_results = results;
1353 0 SvREFCNT_inc_simple_void_NN((SV*)results);
1354 0 ngx_queue_insert_tail(&self->cb_queue, &cbt->queue);
1355 0 self->pending_count++;
1356  
1357 0 arm_cmd_timer(self);
1358 0 start_writing(self);
1359 } else {
1360 ev_mc_wait_t *wt;
1361 0 Newxz(wt, 1, ev_mc_wait_t);
1362 0 Newx(wt->packet, packet_len, char);
1363 0 wt->packet_len = packet_len;
1364 0 mc_encode_header(wt->packet, MC_OP_NOOP, 0, 0, 0, opaque, 0);
1365 0 ▶ if (cb) { wt->cb = newSVsv(cb); }
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { wt->cb = newSVsv(cb); } 0 0 0
1366 0 wt->opaque = opaque;
1367 0 wt->cmd = fence_cmd;
1368 0 wt->quiet = 0;
1369 0 wt->counted = 1;
1370 0 wt->mget_results = results; /* owned ref */
1371 0 SvREFCNT_inc_simple_void_NN((SV*)results);
1372 0 wt->queued_at = ev_now(self->loop);
1373 0 ngx_queue_insert_tail(&self->wait_queue, &wt->queue);
1374 0 self->waiting_count++;
1375 }
1376 }
1377  
1378 /* results starts with refcnt=1, fence owns +1 = total 2.
1379 * Drop our initial ref, fence now owns it (refcnt=1) */
1380 0 SvREFCNT_dec((SV*)results);
1381  
1382 0 ▶ if (self->waiting_timeout_ms > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timeout_ms > 0) 0 0 0
1383 0 schedule_waiting_timer(self);
1384 }
1385  
1386 /* ================================================================
1387 * Response processing
1388 * ================================================================ */
1389  
1390 /* Drain quiet entries before the given opaque.
1391 * Quiet entries that got no response are "quiet success":
1392 * - MGET_ENTRY: miss (nothing to do)
1393 * - Other quiet commands: invoke callback with success */
1394 10 static void drain_quiet_before(pTHX_ ev_mc_t *self, uint32_t opaque) {
1395 10 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 10 0 10
1396 10 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
1397 10 ev_mc_cb_t *cbt = ngx_queue_data(q, ev_mc_cb_t, queue);
1398  
1399 /* Use signed comparison to handle wraparound */
1400 10 ▶ if ((int32_t)(cbt->opaque - opaque) >= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((int32_t)(cbt->opaque - opaque) >= 0) break; 10 0 10
1401 0 ▶ if (!cbt->quiet) break; /* non-quiet should always get response */
Branch: 1 branch
BranchTrueFalseTotal
if (!cbt->quiet) break; /* non-quiet should always get response */ 0 0 0
1402  
1403 0 ngx_queue_remove(q);
1404 0 ▶ if (cbt->counted) self->pending_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->counted) self->pending_count--; 0 0 0
1405  
1406 /* Quiet success: for storage = stored, for MGET_ENTRY = miss */
1407 0 ▶ if (cbt->cb && !cbt->skipped &&
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->cb && !cbt->skipped && 0 0 0
if (cbt->cb && !cbt->skipped && 0 0 0
1408 0 ▶ cbt->cmd != CB_CMD_MGET_ENTRY && cbt->cmd != CB_CMD_MGETS_ENTRY) {
Branch: 2 branches
BranchTrueFalseTotal
cbt->cmd != CB_CMD_MGET_ENTRY && cbt->cmd != CB_CMD_MGETS_ENTRY) { 0 0 0
cbt->cmd != CB_CMD_MGET_ENTRY && cbt->cmd != CB_CMD_MGETS_ENTRY) { 0 0 0
1409 0 invoke_cb(aTHX_ self, cbt->cb, newSViv(1), NULL);
1410 0 ▶ if (self->magic == MC_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED) { 0 0 0
1411 0 cleanup_cbt(aTHX_ cbt);
1412 0 return;
1413 }
1414 }
1415 0 cleanup_cbt(aTHX_ cbt);
1416 }
1417 }
1418  
1419 10 static void handle_response_packet(pTHX_ ev_mc_t *self) {
1420 10 const char *pkt = self->rbuf;
1421 10 uint16_t key_len = mc_read_u16(pkt + 2);
1422 10 uint8_t extras_len = (uint8_t)pkt[4];
1423 10 uint16_t status = mc_read_u16(pkt + 6);
1424 10 uint32_t body_len = mc_read_u32(pkt + 8);
1425 10 uint32_t opaque = mc_read_u32(pkt + 12);
1426 10 uint64_t cas = mc_read_u64(pkt + 16);
1427  
1428 10 const char *body = pkt + MC_HEADER_SIZE;
1429  
1430 10 ▶ if ((uint32_t)extras_len + key_len > body_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ((uint32_t)extras_len + key_len > body_len) { 0 10 10
1431 0 handle_disconnect(aTHX_ self, "malformed response: body too short");
1432 0 return;
1433 }
1434  
1435 10 const char *extras = body;
1436 10 const char *key_ptr = body + extras_len;
1437 10 const char *value_ptr = body + extras_len + key_len;
1438 10 uint32_t value_len = body_len - extras_len - key_len;
1439  
1440 /* Drain quiet entries that were skipped (no response) */
1441 10 drain_quiet_before(aTHX_ self, opaque);
1442 10 ▶ if (self->magic == MC_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED) return; 0 10 10
1443  
1444 /* Find matching callback entry */
1445 10 ▶ if (ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) { 0 10 10
1446 /* Stray response (e.g., quiet opcode error) — discard */
1447 0 return;
1448 }
1449  
1450 10 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
1451 10 ev_mc_cb_t *cbt = ngx_queue_data(q, ev_mc_cb_t, queue);
1452  
1453 10 ▶ if (cbt->opaque != opaque) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->opaque != opaque) { 0 10 10
1454 /* opaque=0 is fire-and-forget error response — discard silently */
1455 0 ▶ if (opaque == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (opaque == 0) return; 0 0 0
1456 char errbuf[128];
1457 0 snprintf(errbuf, sizeof(errbuf),
1458 "protocol error: expected opaque %u, got %u", cbt->opaque, opaque);
1459 0 handle_disconnect(aTHX_ self, errbuf);
1460 0 return;
1461 }
1462  
1463 /* STAT accumulation: don't remove from queue until terminator */
1464 10 ▶ if (cbt->cmd == CB_CMD_STATS && status == MC_STATUS_OK && key_len > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (cbt->cmd == CB_CMD_STATS && status == MC_STATUS_OK && key_len > 0) { 0 10 10
if (cbt->cmd == CB_CMD_STATS && status == MC_STATUS_OK && key_len > 0) { 0 0 0
if (cbt->cmd == CB_CMD_STATS && status == MC_STATUS_OK && key_len > 0) { 0 0 0
1465 0 ▶ if (cbt->stats_hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->stats_hv) { 0 0 0
1466 0 hv_store(cbt->stats_hv, key_ptr, key_len,
1467 newSVpvn(value_ptr, value_len), 0);
1468 }
1469 0 return; /* wait for more stats or terminator */
1470 }
1471  
1472 /* Remove from queue */
1473 10 ngx_queue_remove(q);
1474 10 ▶ if (cbt->counted) self->pending_count--;
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->counted) self->pending_count--; 8 2 10
1475  
1476 /* Handle error status */
1477 10 ▶ if (status != MC_STATUS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != MC_STATUS_OK) { 2 8 10
1478 /* GET/GETS/GAT/GATS miss: return (undef, undef) */
1479 2 ▶ if (status == MC_STATUS_KEY_NOT_FOUND &&
Branch: 1 branch
BranchTrueFalseTotal
if (status == MC_STATUS_KEY_NOT_FOUND && 2 0 2
1480 2 ▶ (cbt->cmd == CB_CMD_GET || cbt->cmd == CB_CMD_GETS ||
Branch: 2 branches
BranchTrueFalseTotal
(cbt->cmd == CB_CMD_GET || cbt->cmd == CB_CMD_GETS || 0 2 2
(cbt->cmd == CB_CMD_GET || cbt->cmd == CB_CMD_GETS || 0 0 0
1481 0 ▶ cbt->cmd == CB_CMD_GAT || cbt->cmd == CB_CMD_GATS)) {
Branch: 2 branches
BranchTrueFalseTotal
cbt->cmd == CB_CMD_GAT || cbt->cmd == CB_CMD_GATS)) { 0 0 0
cbt->cmd == CB_CMD_GAT || cbt->cmd == CB_CMD_GATS)) { 0 0 0
1482 2 ▶ if (cbt->cb && !cbt->skipped)
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->cb && !cbt->skipped) 0 2 2
if (cbt->cb && !cbt->skipped) 0 0 0
1483 0 invoke_cb(aTHX_ self, cbt->cb, NULL, NULL);
1484 }
1485 0 ▶ else if (cbt->cmd == CB_CMD_MGET_ENTRY || cbt->cmd == CB_CMD_MGETS_ENTRY) {
Branch: 2 branches
BranchTrueFalseTotal
else if (cbt->cmd == CB_CMD_MGET_ENTRY || cbt->cmd == CB_CMD_MGETS_ENTRY) { 0 0 0
else if (cbt->cmd == CB_CMD_MGET_ENTRY || cbt->cmd == CB_CMD_MGETS_ENTRY) { 0 0 0
1486 /* mget miss — nothing to add */
1487 }
1488 /* SASL auth failure: disconnect (auto-auth) or report to callback */
1489 0 ▶ else if (cbt->cmd == CB_CMD_SASL_AUTH) {
Branch: 1 branch
BranchTrueFalseTotal
else if (cbt->cmd == CB_CMD_SASL_AUTH) { 0 0 0
1490 0 ▶ if (cbt->cb && !cbt->skipped) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->cb && !cbt->skipped) { 0 0 0
if (cbt->cb && !cbt->skipped) { 0 0 0
1491 0 const char *errstr = mc_status_str(status);
1492 0 ▶ if (value_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (value_len > 0) 0 0 0
1493 0 invoke_cb(aTHX_ self, cbt->cb, NULL,
1494 newSVpvf("%s: %.*s", errstr, (int)value_len, value_ptr));
1495 else
1496 0 invoke_cb(aTHX_ self, cbt->cb, NULL, newSVpv(errstr, 0));
1497 0 ▶ } else if (!cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!cbt->cb) { 0 0 0
1498 /* Auto-auth failed — disconnect with error */
1499 char errbuf[256];
1500 0 ▶ if (value_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (value_len > 0) 0 0 0
1501 0 snprintf(errbuf, sizeof(errbuf), "SASL auth failed: %.*s",
1502 (int)value_len, value_ptr);
1503 else
1504 0 snprintf(errbuf, sizeof(errbuf), "SASL auth failed: %s",
1505 mc_status_str(status));
1506 0 cleanup_cbt(aTHX_ cbt);
1507 0 handle_disconnect(aTHX_ self, errbuf);
1508 0 return;
1509 }
1510 }
1511 else {
1512 /* Real error */
1513 0 ▶ if (cbt->cb && !cbt->skipped) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->cb && !cbt->skipped) { 0 0 0
if (cbt->cb && !cbt->skipped) { 0 0 0
1514 0 const char *errstr = mc_status_str(status);
1515 /* Include server error message if present */
1516 0 ▶ if (value_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (value_len > 0) 0 0 0
1517 0 invoke_cb(aTHX_ self, cbt->cb, NULL,
1518 newSVpvf("%s: %.*s", errstr, (int)value_len, value_ptr));
1519 else
1520 0 invoke_cb(aTHX_ self, cbt->cb, NULL, newSVpv(errstr, 0));
1521 }
1522 }
1523 2 ▶ if (self->magic != MC_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != MC_MAGIC_FREED) { 2 0 2
1524 2 cleanup_cbt(aTHX_ cbt);
1525 2 send_next_waiting(aTHX_ self);
1526 } else {
1527 0 cleanup_cbt(aTHX_ cbt);
1528 }
1529 2 return;
1530 }
1531  
1532 /* Handle success */
1533 8 ▶ if (cbt->skipped) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->skipped) { 0 8 8
1534 0 cleanup_cbt(aTHX_ cbt);
1535 0 send_next_waiting(aTHX_ self);
1536 0 return;
1537 }
1538  
1539 8 switch (cbt->cmd) {
1540 4 case CB_CMD_GET:
1541 case CB_CMD_GAT:
1542 4 ▶ if (cbt->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) 4 0 4
1543 4 invoke_cb(aTHX_ self, cbt->cb, newSVpvn(value_ptr, value_len), NULL);
1544 4 break;
1545  
1546 0 case CB_CMD_GETS:
1547 case CB_CMD_GATS:
1548 0 ▶ if (cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) { 0 0 0
1549 0 HV *hv = newHV();
1550 0 hv_stores(hv, "value", newSVpvn(value_ptr, value_len));
1551 0 ▶ if (extras_len >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (extras_len >= 4) 0 0 0
1552 0 hv_stores(hv, "flags", newSVuv(mc_read_u32(extras)));
1553 0 hv_stores(hv, "cas", newSVuv(cas));
1554 0 invoke_cb(aTHX_ self, cbt->cb, newRV_noinc((SV*)hv), NULL);
1555 }
1556 0 break;
1557  
1558 4 case CB_CMD_STORE:
1559 case CB_CMD_DELETE:
1560 case CB_CMD_TOUCH:
1561 case CB_CMD_FLUSH:
1562 case CB_CMD_NOOP:
1563 case CB_CMD_QUIT:
1564 4 ▶ if (cbt->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) 4 0 4
1565 4 invoke_cb(aTHX_ self, cbt->cb, newSViv(1), NULL);
1566 4 break;
1567  
1568 0 case CB_CMD_ARITH:
1569 0 ▶ if (cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) { 0 0 0
1570 0 ▶ if (value_len >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (value_len >= 8) { 0 0 0
1571 0 uint64_t new_val = mc_read_u64(value_ptr);
1572 0 invoke_cb(aTHX_ self, cbt->cb, newSVuv((UV)new_val), NULL);
1573 } else {
1574 0 invoke_cb(aTHX_ self, cbt->cb, NULL,
1575 newSVpv("malformed ARITH response", 0));
1576 }
1577 }
1578 0 break;
1579  
1580 0 case CB_CMD_VERSION:
1581 case CB_CMD_SASL_LIST:
1582 0 ▶ if (cbt->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) 0 0 0
1583 0 invoke_cb(aTHX_ self, cbt->cb, newSVpvn(value_ptr, value_len), NULL);
1584 0 break;
1585  
1586 0 case CB_CMD_SASL_AUTH:
1587 0 ▶ if (cbt->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cbt->cb) { 0 0 0
1588 0 invoke_cb(aTHX_ self, cbt->cb, newSViv(1), NULL);
1589 } else {
1590 /* Auto-auth completed: the connection is now fully
1591 established — fire on_connect before the common tail
1592 drains the wait queue. */
1593 0 emit_connect(aTHX_ self);
1594 }
1595 0 break;
1596  
1597 0 case CB_CMD_STATS:
1598 /* Terminator (key_len==0): deliver accumulated stats */
1599 0 ▶ if (cbt->cb && cbt->stats_hv) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->cb && cbt->stats_hv) { 0 0 0
if (cbt->cb && cbt->stats_hv) { 0 0 0
1600 0 SV *rv = newRV_noinc((SV*)cbt->stats_hv);
1601 0 cbt->stats_hv = NULL; /* transferred ownership */
1602 0 invoke_cb(aTHX_ self, cbt->cb, rv, NULL);
1603 }
1604 0 break;
1605  
1606 0 case CB_CMD_MGET_ENTRY:
1607 0 ▶ if (cbt->mget_results && key_len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->mget_results && key_len > 0) { 0 0 0
if (cbt->mget_results && key_len > 0) { 0 0 0
1608 0 hv_store(cbt->mget_results, key_ptr, key_len,
1609 newSVpvn(value_ptr, value_len), 0);
1610 }
1611 0 break;
1612  
1613 0 case CB_CMD_MGETS_ENTRY:
1614 0 ▶ if (cbt->mget_results && key_len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->mget_results && key_len > 0) { 0 0 0
if (cbt->mget_results && key_len > 0) { 0 0 0
1615 0 HV *info = newHV();
1616 0 hv_stores(info, "value", newSVpvn(value_ptr, value_len));
1617 0 ▶ if (extras_len >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (extras_len >= 4) 0 0 0
1618 0 hv_stores(info, "flags", newSVuv(mc_read_u32(extras)));
1619 0 hv_stores(info, "cas", newSVuv(cas));
1620 0 hv_store(cbt->mget_results, key_ptr, key_len,
1621 newRV_noinc((SV*)info), 0);
1622 }
1623 0 break;
1624  
1625 0 case CB_CMD_MGET_FENCE:
1626 case CB_CMD_MGETS_FENCE:
1627 0 ▶ if (cbt->cb && cbt->mget_results) {
Branch: 2 branches
BranchTrueFalseTotal
if (cbt->cb && cbt->mget_results) { 0 0 0
if (cbt->cb && cbt->mget_results) { 0 0 0
1628 0 SV *rv = newRV_noinc((SV*)cbt->mget_results);
1629 0 cbt->mget_results = NULL;
1630 0 invoke_cb(aTHX_ self, cbt->cb, rv, NULL);
1631 }
1632 0 break;
1633 }
1634  
1635 8 ▶ if (self->magic != MC_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != MC_MAGIC_FREED) { 7 1 8
1636 7 cleanup_cbt(aTHX_ cbt);
1637 7 send_next_waiting(aTHX_ self);
1638 } else {
1639 1 cleanup_cbt(aTHX_ cbt);
1640 }
1641 }
1642  
1643 9 static void process_responses(pTHX_ ev_mc_t *self) {
1644 9 int processed = 0;
1645 17 ▶ while (self->rbuf_len >= MC_HEADER_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->rbuf_len >= MC_HEADER_SIZE) { 10 7 17
1646 /* Verify magic byte */
1647 10 ▶ if ((uint8_t)self->rbuf[0] != MC_RES_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if ((uint8_t)self->rbuf[0] != MC_RES_MAGIC) { 0 10 10
1648 0 handle_disconnect(aTHX_ self, "invalid response magic byte");
1649 0 return;
1650 }
1651  
1652 10 uint32_t body_len = mc_read_u32(self->rbuf + 8);
1653 /* Sanity bound: memcached's hard limit is 128 MB; cap at 256 MB to
1654 catch garbage/MITM responses without rejecting any real reply.
1655 Also avoids size_t overflow on 32-bit perls when MC_HEADER_SIZE
1656 is added below. */
1657 10 ▶ if (body_len > 0x10000000u) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_len > 0x10000000u) { 0 10 10
1658 0 handle_disconnect(aTHX_ self, "response body too large");
1659 0 return;
1660 }
1661 10 size_t total_len = (size_t)MC_HEADER_SIZE + body_len;
1662  
1663 10 ▶ if (self->rbuf_len < total_len) break; /* incomplete packet */
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < total_len) break; /* incomplete packet */ 0 10 10
1664  
1665 10 unsigned int gen = self->conn_gen;
1666 10 handle_response_packet(aTHX_ self);
1667 10 ▶ if (self->magic == MC_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED) return; 1 9 10
1668 9 ▶ if (self->conn_gen != gen) return; /* connection torn down/replaced by callback */
Branch: 1 branch
BranchTrueFalseTotal
if (self->conn_gen != gen) return; /* connection torn down/replaced by callback */ 1 8 9
1669 8 ▶ if (!self->connected) return; /* disconnect() called from callback */
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; /* disconnect() called from callback */ 0 8 8
1670 8 ▶ if (self->rbuf_len < total_len) return; /* buffer reset mid-callback */
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < total_len) return; /* buffer reset mid-callback */ 0 8 8
1671  
1672 /* Consume from buffer */
1673 8 self->rbuf_len -= total_len;
1674 8 ▶ if (self->rbuf_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len > 0) 1 7 8
1675 1 memmove(self->rbuf, self->rbuf + total_len, self->rbuf_len);
1676 8 processed++;
1677 }
1678  
1679 /* Reset command timeout on activity */
1680 7 ▶ if (processed > 0 && self->cmd_timer_active) {
Branch: 2 branches
BranchTrueFalseTotal
if (processed > 0 && self->cmd_timer_active) { 7 0 7
if (processed > 0 && self->cmd_timer_active) { 0 7 7
1681 0 ▶ if (ngx_queue_empty(&self->cb_queue))
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) 0 0 0
1682 0 disarm_cmd_timer(self);
1683 else
1684 0 arm_cmd_timer(self);
1685 }
1686 }
1687  
1688 /* ================================================================
1689 * IO callbacks
1690 * ================================================================ */
1691  
1692 11 static void on_readable(pTHX_ ev_mc_t *self) {
1693 11 buf_ensure_read(self, 4096);
1694 11 ssize_t n = read(self->fd, self->rbuf + self->rbuf_len,
1695 11 self->rbuf_cap - self->rbuf_len);
1696 11 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 9 2 11
1697 9 self->rbuf_len += n;
1698 9 process_responses(aTHX_ self);
1699 2 ▶ } else if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (n == 0) { 2 0 2
1700 /* EOF; expected after quit()/disconnect() — no error reason then */
1701 2 handle_disconnect(aTHX_ self,
1702 2 ▶ self->intentional_disconnect ? NULL : "connection closed by server");
Branch: 1 branch
BranchTrueFalseTotal
self->intentional_disconnect ? NULL : "connection closed by server"); 2 0 2
1703 } else {
1704 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
Branch: 3 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
1705 0 return;
1706 char errbuf[128];
1707 0 snprintf(errbuf, sizeof(errbuf), "read error: %s", strerror(errno));
1708 0 handle_disconnect(aTHX_ self, errbuf);
1709 }
1710 }
1711  
1712 11 static int try_write(ev_mc_t *self) {
1713 22 ▶ while (self->wbuf_off < self->wbuf_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->wbuf_off < self->wbuf_len) { 11 11 22
1714 #ifdef MSG_NOSIGNAL
1715 /* a peer close must yield EPIPE, not SIGPIPE */
1716 11 ssize_t n = send(self->fd, self->wbuf + self->wbuf_off,
1717 11 self->wbuf_len - self->wbuf_off, MSG_NOSIGNAL);
1718 #else
1719 ssize_t n = write(self->fd, self->wbuf + self->wbuf_off,
1720 self->wbuf_len - self->wbuf_off);
1721 #endif
1722 11 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 11 0 11
1723 11 self->wbuf_off += n;
1724 0 ▶ } else if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (n == 0) { 0 0 0
1725 0 return 0; /* nothing written — retry on next writable event */
1726 } else {
1727 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
Branch: 3 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
1728 0 return 0; /* try again later */
1729 0 return -1; /* error */
1730 }
1731 }
1732 /* All written */
1733 11 self->wbuf_len = 0;
1734 11 self->wbuf_off = 0;
1735 11 stop_writing(self);
1736 11 return 1;
1737 }
1738  
1739 18 static void on_connect_complete(pTHX_ ev_mc_t *self) {
1740 18 int err = 0;
1741 18 socklen_t len = sizeof(err);
1742 18 ▶ if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0 || err != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0 || err != 0) { 18 0 18
if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0 || err != 0) { 5 13 18
1743 char errbuf[128];
1744 5 snprintf(errbuf, sizeof(errbuf), "connect failed: %s",
1745 5 ▶ strerror(err ? err : errno));
Branch: 1 branch
BranchTrueFalseTotal
strerror(err ? err : errno)); 0 5 5
1746 5 close(self->fd);
1747 5 self->fd = -1;
1748 5 self->connecting = 0;
1749 5 stop_writing(self);
1750 5 stop_connect_timer(self);
1751  
1752 5 report_connect_error(aTHX_ self, errbuf);
1753 5 return;
1754 }
1755  
1756 13 self->connecting = 0;
1757 13 self->connected = 1;
1758  
1759 13 stop_writing(self);
1760 13 stop_connect_timer(self);
1761  
1762 13 finish_connect_success(aTHX_ self);
1763 }
1764  
1765 40 static void io_cb(EV_P_ ev_io *w, int revents) {
1766 40 ev_mc_t *self = (ev_mc_t *)w->data;
1767 (void)loop;
1768  
1769 40 ▶ if (self->magic != MC_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != MC_MAGIC_ALIVE) return; 0 40 40
1770  
1771 40 self->callback_depth++;
1772  
1773 40 ▶ if (self->connecting) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connecting) { 18 22 40
1774 18 ▶ if (revents & EV_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) { 18 0 18
1775 18 on_connect_complete(aTHX_ self);
1776 }
1777 18 self->callback_depth--;
1778 18 check_destroyed(self);
1779 18 return;
1780 }
1781  
1782 22 ▶ if (revents & EV_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_READ) { 11 11 22
1783 11 on_readable(aTHX_ self);
1784 11 ▶ if (self->magic != MC_MAGIC_ALIVE) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != MC_MAGIC_ALIVE) { 1 10 11
1785 1 self->callback_depth--;
1786 1 check_destroyed(self);
1787 1 return;
1788 }
1789 }
1790  
1791 21 ▶ if (revents & EV_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) { 11 10 21
1792 11 int rv = try_write(self);
1793 11 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 0 11 11
1794 char errbuf[128];
1795 0 snprintf(errbuf, sizeof(errbuf), "write error: %s", strerror(errno));
1796 0 handle_disconnect(aTHX_ self, errbuf);
1797 }
1798 }
1799  
1800 21 self->callback_depth--;
1801 21 check_destroyed(self);
1802 }
1803  
1804 /* ================================================================
1805 * Connection
1806 * ================================================================ */
1807  
1808 21 static void start_connect(pTHX_ ev_mc_t *self) {
1809 int fd, ret;
1810  
1811 21 ▶ if (self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->path) { 14 7 21
1812 /* Unix socket */
1813 struct sockaddr_un addr;
1814 14 memset(&addr, 0, sizeof(addr));
1815 14 addr.sun_family = AF_UNIX;
1816 14 ▶ if (strlen(self->path) >= sizeof(addr.sun_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(self->path) >= sizeof(addr.sun_path)) { 0 14 14
1817 0 emit_error(aTHX_ self, "unix socket path too long");
1818 0 return;
1819 }
1820 14 strncpy(addr.sun_path, self->path, sizeof(addr.sun_path) - 1);
1821  
1822 14 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1823 14 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 14 14
1824 char errbuf[128];
1825 0 snprintf(errbuf, sizeof(errbuf), "socket: %s", strerror(errno));
1826 0 report_connect_error(aTHX_ self, errbuf);
1827 0 return;
1828 }
1829 #ifdef SO_NOSIGPIPE
1830 { /* no MSG_NOSIGNAL on macOS/BSD */
1831 int one_ns = 1;
1832 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one_ns, sizeof one_ns);
1833 }
1834 #endif
1835  
1836 /* non-blocking */
1837 {
1838 14 int fl = fcntl(fd, F_GETFL);
1839 14 ▶ if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) { 14 0 14
if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) { 0 14 14
1840 0 close(fd);
1841 0 report_connect_error(aTHX_ self, "fcntl O_NONBLOCK failed");
1842 0 return;
1843 }
1844 }
1845  
1846 14 self->fd = fd;
1847 14 ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
1848 } else {
1849 /* TCP */
1850 7 struct addrinfo hints, *res = NULL;
1851 char port_str[16];
1852  
1853 7 snprintf(port_str, sizeof(port_str), "%d", self->port);
1854 7 memset(&hints, 0, sizeof(hints));
1855 7 hints.ai_family = AF_UNSPEC;
1856 7 hints.ai_socktype = SOCK_STREAM;
1857  
1858 7 ret = getaddrinfo(self->host, port_str, &hints, &res);
1859 7 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 7 7
1860 char errbuf[256];
1861 0 snprintf(errbuf, sizeof(errbuf), "getaddrinfo: %s", gai_strerror(ret));
1862 0 report_connect_error(aTHX_ self, errbuf);
1863 0 return;
1864 }
1865  
1866 7 fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1867 7 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 7 7
1868 0 freeaddrinfo(res);
1869 char errbuf[128];
1870 0 snprintf(errbuf, sizeof(errbuf), "socket: %s", strerror(errno));
1871 0 report_connect_error(aTHX_ self, errbuf);
1872 0 return;
1873 }
1874 #ifdef SO_NOSIGPIPE
1875 { /* no MSG_NOSIGNAL on macOS/BSD */
1876 int one_ns = 1;
1877 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one_ns, sizeof one_ns);
1878 }
1879 #endif
1880  
1881 /* non-blocking */
1882 {
1883 7 int fl = fcntl(fd, F_GETFL);
1884 7 ▶ if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) { 7 0 7
if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) { 0 7 7
1885 0 freeaddrinfo(res);
1886 0 close(fd);
1887 0 report_connect_error(aTHX_ self, "fcntl O_NONBLOCK failed");
1888 0 return;
1889 }
1890 }
1891  
1892 /* TCP_NODELAY */
1893 {
1894 7 int one = 1;
1895 7 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
1896 }
1897  
1898 7 self->fd = fd;
1899 7 ret = connect(fd, res->ai_addr, res->ai_addrlen);
1900 7 freeaddrinfo(res);
1901 }
1902  
1903 21 ▶ if (ret == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) { 14 7 21
1904 /* Connected immediately (unix / loopback). Complete via the
1905 event loop, never synchronously: the armed EV_WRITE watcher
1906 fires on the next iteration (a connected socket is writable)
1907 and drives the normal on_connect_complete path. EVAPI exports
1908 no ev_feed_event. No connect_timer for immediate connects. */
1909 14 self->connecting = 1;
1910 14 ev_io_init(&self->rio, io_cb, self->fd, EV_READ);
1911 14 self->rio.data = (void *)self;
1912 14 ev_io_init(&self->wio, io_cb, self->fd, EV_WRITE);
1913 14 self->wio.data = (void *)self;
1914 14 ev_set_priority(&self->rio, self->priority);
1915 14 ev_set_priority(&self->wio, self->priority);
1916 14 start_writing(self);
1917 14 return;
1918 }
1919  
1920 7 ▶ if (errno != EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno != EINPROGRESS) { 0 7 7
1921 char errbuf[128];
1922 0 snprintf(errbuf, sizeof(errbuf), "connect: %s", strerror(errno));
1923 0 close(self->fd);
1924 0 self->fd = -1;
1925 0 report_connect_error(aTHX_ self, errbuf);
1926 0 return;
1927 }
1928  
1929 /* In progress - wait for writability */
1930 7 self->connecting = 1;
1931 7 ev_io_init(&self->rio, io_cb, self->fd, EV_READ);
1932 7 self->rio.data = (void *)self;
1933 7 ev_io_init(&self->wio, io_cb, self->fd, EV_WRITE);
1934 7 self->wio.data = (void *)self;
1935 7 ev_set_priority(&self->rio, self->priority);
1936 7 ev_set_priority(&self->wio, self->priority);
1937 7 start_writing(self);
1938  
1939 7 ▶ if (self->connect_timeout_ms > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timeout_ms > 0) { 0 7 7
1940 0 ev_tstamp delay = (ev_tstamp)self->connect_timeout_ms / 1000.0;
1941 0 ev_timer_init(&self->connect_timer, connect_timeout_cb, delay, 0.0);
1942 0 self->connect_timer.data = (void *)self;
1943 0 ev_timer_start(self->loop, &self->connect_timer);
1944 0 self->connect_timer_active = 1;
1945 }
1946 }
1947  
1948 /* ================================================================
1949 * XS interface
1950 * ================================================================ */
1951  
1952 MODULE = EV::Memcached PACKAGE = EV::Memcached
1953  
1954 BOOT:
1955 {
1956 18 ▶ I_EV_API("EV::Memcached");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::Memcached"); 0 18 18
I_EV_API("EV::Memcached"); 18 0 18
I_EV_API("EV::Memcached"); 0 18 18
1957 18 err_skipped = newSVpvs("skipped");
1958 18 SvREADONLY_on(err_skipped);
1959 18 err_disconnected = newSVpvs("disconnected");
1960 18 SvREADONLY_on(err_disconnected);
1961 18 err_waiting_timeout = newSVpvs("waiting timeout");
1962 18 SvREADONLY_on(err_waiting_timeout);
1963 }
1964  
1965 EV::Memcached
1966 new(char *class, ...)
1967 CODE:
1968 {
1969 PERL_UNUSED_VAR(class);
1970 22 ▶ if ((items - 1) % 2 != 0) croak("odd number of arguments");
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2 != 0) croak("odd number of arguments"); 0 22 22
1971  
1972 /* validate loop before allocating RETVAL so the croak leaks nothing */
1973 {
1974 int j;
1975 71 ▶ for (j = 1; j < items; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < items; j += 2) { 53 18 71
1976 53 const char *k = SvPV_nolen(ST(j));
1977 53 SV *v = ST(j + 1);
1978 53 ▶ if (strEQ(k, "loop") &&
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "loop") && 6 47 53
1979 6 ▶ !(SvROK(v) && SvOBJECT(SvRV(v)) && sv_derived_from(v, "EV::Loop")))
Branch: 3 branches
BranchTrueFalseTotal
!(SvROK(v) && SvOBJECT(SvRV(v)) && sv_derived_from(v, "EV::Loop"))) 5 1 6
!(SvROK(v) && SvOBJECT(SvRV(v)) && sv_derived_from(v, "EV::Loop"))) 3 2 5
!(SvROK(v) && SvOBJECT(SvRV(v)) && sv_derived_from(v, "EV::Loop"))) 0 3 3
1980 3 croak("loop must be an EV::Loop object");
1981 50 ▶ if ((strEQ(k, "on_error") || strEQ(k, "on_connect") ||
Branch: 2 branches
BranchTrueFalseTotal
if ((strEQ(k, "on_error") || strEQ(k, "on_connect") || 33 17 50
if ((strEQ(k, "on_error") || strEQ(k, "on_connect") || 29 4 33
1982 29 ▶ strEQ(k, "on_disconnect")))
Branch: 1 branch
BranchTrueFalseTotal
strEQ(k, "on_disconnect"))) 1 28 29
1983 22 (void)mc_handler_arg(aTHX_ v); /* croaks on bad value */
1984 }
1985 }
1986  
1987 18 Newxz(RETVAL, 1, ev_mc_t);
1988 18 RETVAL->magic = MC_MAGIC_ALIVE;
1989 18 RETVAL->fd = -1;
1990 18 RETVAL->port = 11211;
1991 18 RETVAL->next_opaque = 1; /* reserve 0 for fire-and-forget quiet ops */
1992 18 ngx_queue_init(&RETVAL->cb_queue);
1993 18 ngx_queue_init(&RETVAL->wait_queue);
1994 18 Newx(RETVAL->rbuf, BUF_INIT_SIZE, char);
1995 18 RETVAL->rbuf_cap = BUF_INIT_SIZE;
1996 18 Newx(RETVAL->wbuf, BUF_INIT_SIZE, char);
1997 18 RETVAL->wbuf_cap = BUF_INIT_SIZE;
1998  
1999 /* Default error handler: warn. Callback exceptions are caught by
2000 G_EVAL in emit_error, so a `die` would be demoted to a warning
2001 anyway — emit it directly and avoid the double prefix. */
2002 18 RETVAL->on_error = eval_pv("sub { warn \"EV::Memcached error: @_\\n\" }", TRUE);
2003 18 SvREFCNT_inc_simple_void_NN(RETVAL->on_error);
2004  
2005 /* Parse options */
2006 18 SV *host_sv = NULL, *path_sv = NULL;
2007 18 int port = 11211;
2008 18 int do_reconnect = 0, reconnect_delay = 1000, max_reconnect_attempts = 0;
2009 18 RETVAL->loop = EV_DEFAULT;
2010 int i;
2011  
2012 67 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 49 18 67
2013 49 const char *k = SvPV_nolen(ST(i));
2014 49 SV *v = ST(i + 1);
2015  
2016 49 ▶ if (strEQ(k, "host")) host_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "host")) host_sv = v; 5 44 49
2017 44 ▶ else if (strEQ(k, "port")) port = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "port")) port = SvIV(v); 5 39 44
2018 39 ▶ else if (strEQ(k, "path")) path_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "path")) path_sv = v; 10 29 39
2019 29 ▶ else if (strEQ(k, "on_error")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "on_error")) { 17 12 29
2020 17 ▶ CLEAR_HANDLER(RETVAL->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_error); 17 0 17
2021 17 SV *h = mc_handler_arg(aTHX_ v);
2022 17 ▶ if (h) RETVAL->on_error = newSVsv(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h) RETVAL->on_error = newSVsv(h); 16 1 17
2023 }
2024 12 ▶ else if (strEQ(k, "on_connect")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "on_connect")) { 3 9 12
2025 3 SV *h = mc_handler_arg(aTHX_ v);
2026 3 ▶ if (h) RETVAL->on_connect = newSVsv(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h) RETVAL->on_connect = newSVsv(h); 3 0 3
2027 }
2028 9 ▶ else if (strEQ(k, "on_disconnect")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "on_disconnect")) { 1 8 9
2029 1 SV *h = mc_handler_arg(aTHX_ v);
2030 1 ▶ if (h) RETVAL->on_disconnect = newSVsv(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h) RETVAL->on_disconnect = newSVsv(h); 1 0 1
2031 }
2032 8 ▶ else if (strEQ(k, "max_pending")) RETVAL->max_pending = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "max_pending")) RETVAL->max_pending = SvIV(v); 1 7 8
2033 7 ▶ else if (strEQ(k, "waiting_timeout")) RETVAL->waiting_timeout_ms = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "waiting_timeout")) RETVAL->waiting_timeout_ms = SvIV(v); 0 7 7
2034 7 ▶ else if (strEQ(k, "connect_timeout")) RETVAL->connect_timeout_ms = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "connect_timeout")) RETVAL->connect_timeout_ms = SvIV(v); 0 7 7
2035 7 ▶ else if (strEQ(k, "command_timeout")) RETVAL->command_timeout_ms = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "command_timeout")) RETVAL->command_timeout_ms = SvIV(v); 1 6 7
2036 6 ▶ else if (strEQ(k, "resume_waiting_on_reconnect")) RETVAL->resume_waiting_on_reconnect = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "resume_waiting_on_reconnect")) RETVAL->resume_waiting_on_reconnect = SvTRUE(v) ? 1 : 0; 0 6 6
2037 6 ▶ else if (strEQ(k, "priority")) RETVAL->priority = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "priority")) RETVAL->priority = SvIV(v); 0 6 6
2038 6 ▶ else if (strEQ(k, "keepalive")) RETVAL->keepalive = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "keepalive")) RETVAL->keepalive = SvIV(v); 0 6 6
2039 6 ▶ else if (strEQ(k, "reconnect")) do_reconnect = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "reconnect")) do_reconnect = SvTRUE(v) ? 1 : 0; 1 5 6
2040 5 ▶ else if (strEQ(k, "reconnect_delay")) reconnect_delay = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "reconnect_delay")) reconnect_delay = SvIV(v); 1 4 5
2041 4 ▶ else if (strEQ(k, "max_reconnect_attempts")) max_reconnect_attempts = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "max_reconnect_attempts")) max_reconnect_attempts = SvIV(v); 1 3 4
2042 3 ▶ else if (strEQ(k, "username")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "username")) { 0 3 3
2043 0 ▶ if (SvOK(v)) RETVAL->username = savepv(SvPV_nolen(v));
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) RETVAL->username = savepv(SvPV_nolen(v)); 0 0 0
2044 }
2045 3 ▶ else if (strEQ(k, "password")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "password")) { 0 3 3
2046 0 ▶ if (SvOK(v)) RETVAL->password = savepv(SvPV_nolen(v));
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) RETVAL->password = savepv(SvPV_nolen(v)); 0 0 0
2047 }
2048 3 ▶ else if (strEQ(k, "loop")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "loop")) { 3 0 3
2049 /* EV's T_LOOP convention: pointer in the IV slot of the
2050 blessed referent (SvPVX there is NULL) */
2051 3 RETVAL->loop = INT2PTR(struct ev_loop *, SvIVX(SvRV(v)));
2052 3 RETVAL->loop_sv = newSVsv(v); /* keep a user loop alive */
2053 }
2054 }
2055  
2056 18 ▶ if (host_sv && path_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (host_sv && path_sv) { 5 13 18
if (host_sv && path_sv) { 0 5 5
2057 0 Safefree(RETVAL->rbuf);
2058 0 Safefree(RETVAL->wbuf);
2059 0 ▶ CLEAR_HANDLER(RETVAL->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_error); 0 0 0
2060 0 ▶ CLEAR_HANDLER(RETVAL->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_connect); 0 0 0
2061 0 ▶ CLEAR_HANDLER(RETVAL->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_disconnect); 0 0 0
2062 0 ▶ CLEAR_HANDLER(RETVAL->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->loop_sv); 0 0 0
2063 0 ▶ if (RETVAL->username) Safefree(RETVAL->username);
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->username) Safefree(RETVAL->username); 0 0 0
2064 0 ▶ if (RETVAL->password) Safefree(RETVAL->password);
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->password) Safefree(RETVAL->password); 0 0 0
2065 0 Safefree(RETVAL);
2066 0 croak("cannot specify both 'host' and 'path'");
2067 }
2068  
2069 18 RETVAL->port = port;
2070 18 ▶ if (do_reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (do_reconnect) { 1 17 18
2071 1 RETVAL->reconnect = 1;
2072 1 RETVAL->reconnect_delay_ms = reconnect_delay >= 0 ? reconnect_delay : 0;
2073 1 RETVAL->max_reconnect_attempts = max_reconnect_attempts >= 0 ? max_reconnect_attempts : 0;
2074 }
2075  
2076 18 ▶ if (host_sv && SvOK(host_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (host_sv && SvOK(host_sv)) { 5 13 18
if (host_sv && SvOK(host_sv)) { 5 0 5
2077 5 RETVAL->host = savepv(SvPV_nolen(host_sv));
2078 5 start_connect(aTHX_ RETVAL);
2079 }
2080 13 ▶ else if (path_sv && SvOK(path_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (path_sv && SvOK(path_sv)) { 10 3 13
else if (path_sv && SvOK(path_sv)) { 10 0 10
2081 10 RETVAL->path = savepv(SvPV_nolen(path_sv));
2082 10 start_connect(aTHX_ RETVAL);
2083 }
2084 }
2085 OUTPUT:
2086 RETVAL
2087  
2088 void
2089 DESTROY(EV::Memcached self)
2090 CODE:
2091 {
2092 18 ▶ if (self->magic == MC_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == MC_MAGIC_FREED) return; 0 18 18
2093  
2094 /* If we're inside a callback, defer destruction.
2095 * check_destroyed() in io_cb/timer_cb will Safefree after unwind. */
2096 18 ▶ if (self->callback_depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->callback_depth > 0) { 1 17 18
2097 /* Fire pending/waiting callbacks once with "disconnected" (the
2098 DESTRUCTION contract). callback_depth is already >0 and pins
2099 self; refcount 0 means no callback can resurrect it. Under
2100 global destruction, calling into Perl is unsafe: free silently. */
2101 1 ▶ if (!PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) { 1 0 1
2102 1 cancel_pending(aTHX_ self, err_disconnected);
2103 1 cancel_waiting(aTHX_ self, err_disconnected);
2104 }
2105  
2106 1 self->magic = MC_MAGIC_FREED;
2107  
2108 /* Stop watchers */
2109 1 stop_reading(self);
2110 1 stop_writing(self);
2111 1 stop_connect_timer(self);
2112 1 disarm_cmd_timer(self);
2113 1 stop_reconnect_timer(self);
2114 1 stop_waiting_timer(self);
2115 1 ▶ if (self->fd >= 0) { close(self->fd); self->fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->fd >= 0) { close(self->fd); self->fd = -1; } 1 0 1
2116  
2117 /* Clean up queues without invoking Perl callbacks */
2118 1 free_all_queues(aTHX_ self);
2119  
2120 1 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 1 0 1
2121 1 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 0 1 1
2122 1 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 0 1 1
2123 1 ▶ CLEAR_HANDLER(self->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->loop_sv); 0 1 1
2124 1 Safefree(self->host); self->host = NULL;
2125 1 Safefree(self->path); self->path = NULL;
2126 1 Safefree(self->username); self->username = NULL;
2127 1 Safefree(self->password); self->password = NULL;
2128 1 Safefree(self->rbuf); self->rbuf = NULL;
2129 1 Safefree(self->wbuf); self->wbuf = NULL;
2130 1 return;
2131 }
2132  
2133 /* Stop timers */
2134 17 stop_connect_timer(self);
2135 17 disarm_cmd_timer(self);
2136 17 stop_reconnect_timer(self);
2137 17 stop_waiting_timer(self);
2138  
2139 17 cleanup_connection(aTHX_ self);
2140  
2141 /* Cancel pending/waiting with disconnected error.
2142 Don't pre-set magic=FREED here: cancel_pending bails its
2143 loop on FREED, which would leak every entry past the first if
2144 any callback was set. Bump callback_depth so any nested DESTROY
2145 (e.g. a callback drops a separate strong ref) takes the deferred
2146 path at the top of this function. */
2147 17 ▶ if (!PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) { 12 5 17
2148 12 self->callback_depth++;
2149 12 cancel_pending(aTHX_ self, err_disconnected);
2150 12 cancel_waiting(aTHX_ self, err_disconnected);
2151 12 self->callback_depth--;
2152 } else {
2153 /* Global destruction: free entries without calling Perl */
2154 5 free_all_queues(aTHX_ self);
2155 }
2156  
2157 17 self->magic = MC_MAGIC_FREED;
2158  
2159 17 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 15 2 17
2160 17 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 8 9 17
2161 17 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 1 16 17
2162 17 ▶ CLEAR_HANDLER(self->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->loop_sv); 3 14 17
2163  
2164 17 Safefree(self->host);
2165 17 Safefree(self->path);
2166 17 Safefree(self->username);
2167 17 Safefree(self->password);
2168 17 Safefree(self->rbuf);
2169 17 Safefree(self->wbuf);
2170  
2171 17 Safefree(self);
2172 }
2173  
2174 void
2175 connect(EV::Memcached self, const char *host, int port = 11211)
2176 CODE:
2177 {
2178 0 ▶ if (self->connected || self->connecting)
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) 0 0 0
if (self->connected || self->connecting) 0 0 0
2179 0 croak("already connected");
2180  
2181 /* An auto-reconnect timer may be pending; cancel it to avoid a
2182 second start_connect when the timer fires. */
2183 0 stop_reconnect_timer(self);
2184  
2185 0 Safefree(self->host);
2186 0 self->host = savepv(host);
2187 0 self->port = port;
2188 0 Safefree(self->path); self->path = NULL;
2189 0 self->intentional_disconnect = 0;
2190  
2191 0 start_connect(aTHX_ self);
2192 }
2193  
2194 void
2195 connect_unix(EV::Memcached self, const char *path)
2196 CODE:
2197 {
2198 4 ▶ if (self->connected || self->connecting)
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) 4 0 4
if (self->connected || self->connecting) 0 4 4
2199 0 croak("already connected");
2200  
2201 4 stop_reconnect_timer(self);
2202  
2203 4 Safefree(self->path);
2204 4 self->path = savepv(path);
2205 4 Safefree(self->host); self->host = NULL;
2206 4 self->intentional_disconnect = 0;
2207  
2208 4 start_connect(aTHX_ self);
2209 }
2210  
2211 void
2212 disconnect(EV::Memcached self)
2213 CODE:
2214 {
2215 3 self->intentional_disconnect = 1;
2216  
2217 3 stop_reconnect_timer(self);
2218  
2219 /* Pin the object across pending-callback dispatch so that an
2220 `undef $mc` from a callback defers DESTROY rather than freeing
2221 us mid-call; check_destroyed below handles the deferred free. */
2222 3 self->callback_depth++;
2223 3 ▶ if (self->connected || self->connecting) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) { 1 2 3
if (self->connected || self->connecting) { 1 0 1
2224 3 handle_disconnect(aTHX_ self, NULL);
2225 } else {
2226 0 cancel_waiting(aTHX_ self, err_disconnected);
2227 }
2228 3 self->callback_depth--;
2229 3 check_destroyed(self);
2230 }
2231  
2232 int
2233 is_connected(EV::Memcached self)
2234 CODE:
2235 3 ▶ RETVAL = self->connected || self->connecting;
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = self->connected || self->connecting; 0 3 3
RETVAL = self->connected || self->connecting; 0 0 0
RETVAL = self->connected || self->connecting; 0 3 3
2236 OUTPUT:
2237 RETVAL
2238  
2239 SV *
2240 on_error(EV::Memcached self, ...)
2241 CODE:
2242 {
2243 4 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 3 1 4
2244 3 SV *h = mc_handler_arg(aTHX_ ST(1));
2245 2 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 2 0 2
2246 2 ▶ if (h) self->on_error = newSVsv(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h) self->on_error = newSVsv(h); 1 1 2
2247 }
2248 3 ▶ RETVAL = self->on_error ? newSVsv(self->on_error) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->on_error ? newSVsv(self->on_error) : &PL_sv_undef; 1 2 3
2249 }
2250 OUTPUT:
2251 RETVAL
2252  
2253 SV *
2254 on_connect(EV::Memcached self, ...)
2255 CODE:
2256 {
2257 6 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 6 0 6
2258 6 SV *h = mc_handler_arg(aTHX_ ST(1));
2259 6 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 1 5 6
2260 6 ▶ if (h) self->on_connect = newSVsv(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h) self->on_connect = newSVsv(h); 6 0 6
2261 }
2262 6 ▶ RETVAL = self->on_connect ? newSVsv(self->on_connect) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->on_connect ? newSVsv(self->on_connect) : &PL_sv_undef; 6 0 6
2263 }
2264 OUTPUT:
2265 RETVAL
2266  
2267 SV *
2268 on_disconnect(EV::Memcached self, ...)
2269 CODE:
2270 {
2271 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2272 0 SV *h = mc_handler_arg(aTHX_ ST(1));
2273 0 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 0 0 0
2274 0 ▶ if (h) self->on_disconnect = newSVsv(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h) self->on_disconnect = newSVsv(h); 0 0 0
2275 }
2276 0 ▶ RETVAL = self->on_disconnect ? newSVsv(self->on_disconnect) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->on_disconnect ? newSVsv(self->on_disconnect) : &PL_sv_undef; 0 0 0
2277 }
2278 OUTPUT:
2279 RETVAL
2280  
2281 void
2282 set(EV::Memcached self, SV *key_sv, SV *value_sv, ...)
2283 ALIAS:
2284 add = 1
2285 replace = 2
2286 CODE:
2287 {
2288 static const uint8_t opcodes[] = { MC_OP_SET, MC_OP_ADD, MC_OP_REPLACE };
2289 5 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 5 0 5
MC_CROAK_UNLESS_CONNECTED(self); 0 5 5
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2290  
2291 STRLEN key_len, value_len;
2292 5 ▶ const char *key = SvPV(key_sv, key_len);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = SvPV(key_sv, key_len); 5 0 5
2293 5 const char *value = SvPV(value_sv, value_len);
2294  
2295 5 int extra = items - 3;
2296 5 SV *cb = NULL;
2297 5 ▶ if (extra > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra > 0) { 5 0 5
2298 5 SV *cand = mc_cb_arg(aTHX_ ST(items-1));
2299 3 ▶ if (cand) { cb = cand; extra--; }
Branch: 1 branch
BranchTrueFalseTotal
if (cand) { cb = cand; extra--; } 2 1 3
2300 }
2301 3 ▶ UV expiry = extra > 0 ? SvUV(ST(3)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
UV expiry = extra > 0 ? SvUV(ST(3)) : 0; 1 2 3
2302 3 ▶ UV flags = extra > 1 ? SvUV(ST(4)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
UV flags = extra > 1 ? SvUV(ST(4)) : 0; 0 3 3
2303  
2304 char extras[8];
2305 3 mc_write_u32(extras, (uint32_t)flags);
2306 3 mc_write_u32(extras + 4, (uint32_t)expiry);
2307  
2308 /* SET fire-and-forget uses SETQ (quiet) — server suppresses response.
2309 * ADD/REPLACE can fail, so they always use normal opcodes. */
2310 3 ▶ if (!cb && ix == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!cb && ix == 0) { 1 2 3
if (!cb && ix == 0) { 1 0 1
2311 1 mc_fire_and_forget(aTHX_ self, MC_OP_SETQ, key, key_len, value, value_len,
2312 extras, 8, 0);
2313 } else {
2314 2 mc_enqueue_cmd(aTHX_ self, opcodes[ix], key, key_len, value, value_len,
2315 extras, 8, 0, CB_CMD_STORE, 0, cb);
2316 }
2317 }
2318  
2319 void
2320 cas(EV::Memcached self, SV *key_sv, SV *value_sv, SV *cas_sv, ...)
2321 CODE:
2322 {
2323 1 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 1 0 1
MC_CROAK_UNLESS_CONNECTED(self); 0 1 1
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2324  
2325 STRLEN key_len, value_len;
2326 1 ▶ const char *key = SvPV(key_sv, key_len);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = SvPV(key_sv, key_len); 1 0 1
2327 1 const char *value = SvPV(value_sv, value_len);
2328 1 uint64_t cas_val = SvUV(cas_sv);
2329  
2330 1 int extra = items - 4;
2331 1 SV *cb = NULL;
2332 1 ▶ if (extra > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra > 0) { 1 0 1
2333 1 SV *cand = mc_cb_arg(aTHX_ ST(items-1));
2334 0 ▶ if (cand) { cb = cand; extra--; }
Branch: 1 branch
BranchTrueFalseTotal
if (cand) { cb = cand; extra--; } 0 0 0
2335 }
2336 0 ▶ UV expiry = extra > 0 ? SvUV(ST(4)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
UV expiry = extra > 0 ? SvUV(ST(4)) : 0; 0 0 0
2337 0 ▶ UV flags = extra > 1 ? SvUV(ST(5)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
UV flags = extra > 1 ? SvUV(ST(5)) : 0; 0 0 0
2338  
2339 char extras[8];
2340 0 mc_write_u32(extras, (uint32_t)flags);
2341 0 mc_write_u32(extras + 4, (uint32_t)expiry);
2342  
2343 0 mc_enqueue_cmd(aTHX_ self, MC_OP_SET, key, key_len, value, value_len,
2344 extras, 8, cas_val, CB_CMD_STORE, 0, cb);
2345 }
2346  
2347 void
2348 get(EV::Memcached self, SV *key_sv, SV *cb_sv = &PL_sv_undef)
2349 ALIAS:
2350 gets = 1
2351 CODE:
2352 {
2353 13 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 5 8 13
MC_CROAK_UNLESS_CONNECTED(self); 0 5 5
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2354  
2355 STRLEN key_len;
2356 13 const char *key = SvPV(key_sv, key_len);
2357 13 SV *cb = mc_cb_arg(aTHX_ cb_sv);
2358  
2359 11 mc_enqueue_cmd(aTHX_ self, MC_OP_GET, key, key_len, NULL, 0,
2360 NULL, 0, 0, ix == 0 ? CB_CMD_GET : CB_CMD_GETS, 0, cb);
2361 }
2362  
2363 void
2364 delete(EV::Memcached self, SV *key_sv, SV *cb_sv = &PL_sv_undef)
2365 CODE:
2366 {
2367 1 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 1 0 1
MC_CROAK_UNLESS_CONNECTED(self); 0 1 1
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2368  
2369 STRLEN key_len;
2370 1 const char *key = SvPV(key_sv, key_len);
2371 1 SV *cb = mc_cb_arg(aTHX_ cb_sv);
2372  
2373 0 mc_enqueue_cmd(aTHX_ self, MC_OP_DELETE, key, key_len, NULL, 0,
2374 NULL, 0, 0, CB_CMD_DELETE, 0, cb);
2375 }
2376  
2377 void
2378 incr(EV::Memcached self, SV *key_sv, ...)
2379 ALIAS:
2380 decr = 1
2381 CODE:
2382 {
2383 2 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 2 0 2
MC_CROAK_UNLESS_CONNECTED(self); 0 2 2
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2384  
2385 STRLEN key_len;
2386 2 const char *key = SvPV(key_sv, key_len);
2387  
2388 2 int extra = items - 2;
2389 2 SV *cb = NULL;
2390 2 ▶ if (extra > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra > 0) { 2 0 2
2391 2 SV *cand = mc_cb_arg(aTHX_ ST(items-1));
2392 0 ▶ if (cand) { cb = cand; extra--; }
Branch: 1 branch
BranchTrueFalseTotal
if (cand) { cb = cand; extra--; } 0 0 0
2393 }
2394 0 ▶ UV delta = extra > 0 ? SvUV(ST(2)) : 1;
Branch: 1 branch
BranchTrueFalseTotal
UV delta = extra > 0 ? SvUV(ST(2)) : 1; 0 0 0
2395 0 ▶ UV initial = extra > 1 ? SvUV(ST(3)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
UV initial = extra > 1 ? SvUV(ST(3)) : 0; 0 0 0
2396 0 ▶ UV expiry = extra > 2 ? SvUV(ST(4)) : 0xFFFFFFFF;
Branch: 1 branch
BranchTrueFalseTotal
UV expiry = extra > 2 ? SvUV(ST(4)) : 0xFFFFFFFF; 0 0 0
2397  
2398 char extras[20];
2399 0 mc_write_u64(extras, (uint64_t)delta);
2400 0 mc_write_u64(extras + 8, (uint64_t)initial);
2401 0 mc_write_u32(extras + 16, (uint32_t)expiry);
2402  
2403 0 ▶ mc_enqueue_cmd(aTHX_ self, ix == 0 ? MC_OP_INCR : MC_OP_DECR,
Branch: 1 branch
BranchTrueFalseTotal
mc_enqueue_cmd(aTHX_ self, ix == 0 ? MC_OP_INCR : MC_OP_DECR, 0 0 0
2404 key, key_len, NULL, 0, extras, 20, 0, CB_CMD_ARITH, 0, cb);
2405 }
2406  
2407 void
2408 append(EV::Memcached self, SV *key_sv, SV *value_sv, SV *cb_sv = &PL_sv_undef)
2409 ALIAS:
2410 prepend = 1
2411 CODE:
2412 {
2413 1 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 1 0 1
MC_CROAK_UNLESS_CONNECTED(self); 0 1 1
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2414  
2415 STRLEN key_len, value_len;
2416 1 ▶ const char *key = SvPV(key_sv, key_len);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = SvPV(key_sv, key_len); 1 0 1
2417 1 const char *value = SvPV(value_sv, value_len);
2418 1 SV *cb = mc_cb_arg(aTHX_ cb_sv);
2419  
2420 0 ▶ mc_enqueue_cmd(aTHX_ self, ix == 0 ? MC_OP_APPEND : MC_OP_PREPEND,
Branch: 1 branch
BranchTrueFalseTotal
mc_enqueue_cmd(aTHX_ self, ix == 0 ? MC_OP_APPEND : MC_OP_PREPEND, 0 0 0
2421 key, key_len, value, value_len, NULL, 0, 0, CB_CMD_STORE, 0, cb);
2422 }
2423  
2424 void
2425 touch(EV::Memcached self, SV *key_sv, UV expiry, SV *cb_sv = &PL_sv_undef)
2426 ALIAS:
2427 gat = 1
2428 gats = 2
2429 CODE:
2430 {
2431 2 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 2 0 2
MC_CROAK_UNLESS_CONNECTED(self); 0 2 2
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2432  
2433 STRLEN key_len;
2434 2 const char *key = SvPV(key_sv, key_len);
2435 2 SV *cb = mc_cb_arg(aTHX_ cb_sv);
2436  
2437 char extras[4];
2438 0 mc_write_u32(extras, (uint32_t)expiry);
2439  
2440 static const int cmds[] = { CB_CMD_TOUCH, CB_CMD_GAT, CB_CMD_GATS };
2441 /* gats deliberately reuses the GAT opcode: there is no distinct GATS
2442 * opcode in the binary protocol. gat/gats differ only in response
2443 * decoding (CB_CMD_GATS additionally returns the CAS). */
2444 static const uint8_t ops[] = { MC_OP_TOUCH, MC_OP_GAT, MC_OP_GAT };
2445  
2446 0 mc_enqueue_cmd(aTHX_ self, ops[ix], key, key_len, NULL, 0,
2447 0 extras, 4, 0, cmds[ix], 0, cb);
2448 }
2449  
2450 void
2451 flush(EV::Memcached self, ...)
2452 CODE:
2453 {
2454 1 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 1 0 1
MC_CROAK_UNLESS_CONNECTED(self); 0 1 1
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2455  
2456 1 int extra = items - 1;
2457 1 SV *cb = NULL;
2458 1 ▶ if (extra > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra > 0) { 1 0 1
2459 1 SV *cand = mc_cb_arg(aTHX_ ST(items-1));
2460 0 ▶ if (cand) { cb = cand; extra--; }
Branch: 1 branch
BranchTrueFalseTotal
if (cand) { cb = cand; extra--; } 0 0 0
2461 }
2462 0 ▶ UV expiry = extra > 0 ? SvUV(ST(1)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
UV expiry = extra > 0 ? SvUV(ST(1)) : 0; 0 0 0
2463  
2464 char extras[4];
2465 0 const char *xp = NULL;
2466 0 uint8_t xl = 0;
2467 0 ▶ if (expiry > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (expiry > 0) { 0 0 0
2468 0 mc_write_u32(extras, (uint32_t)expiry);
2469 0 xp = extras;
2470 0 xl = 4;
2471 }
2472  
2473 0 ▶ if (cb)
Branch: 1 branch
BranchTrueFalseTotal
if (cb) 0 0 0
2474 0 mc_enqueue_cmd(aTHX_ self, MC_OP_FLUSH, NULL, 0, NULL, 0,
2475 xp, xl, 0, CB_CMD_FLUSH, 0, cb);
2476 else
2477 0 mc_fire_and_forget(aTHX_ self, MC_OP_FLUSHQ, NULL, 0, NULL, 0,
2478 xp, xl, 0);
2479 }
2480  
2481 void
2482 version(EV::Memcached self, SV *cb_sv = &PL_sv_undef)
2483 ALIAS:
2484 noop = 1
2485 quit = 2
2486 CODE:
2487 {
2488 8 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 8 0 8
MC_CROAK_UNLESS_CONNECTED(self); 0 8 8
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2489  
2490 8 SV *cb = mc_cb_arg(aTHX_ cb_sv);
2491  
2492 static const uint8_t ops[] = { MC_OP_VERSION, MC_OP_NOOP, MC_OP_QUIT };
2493 static const int cmds[] = { CB_CMD_VERSION, CB_CMD_NOOP, CB_CMD_QUIT };
2494  
2495 5 ▶ if (ix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 2) { 0 5 5
2496 /* quit = intentional teardown: no on_error / reconnect on close */
2497 0 self->intentional_disconnect = 1;
2498 }
2499  
2500 5 mc_enqueue_cmd(aTHX_ self, ops[ix], NULL, 0, NULL, 0,
2501 5 NULL, 0, 0, cmds[ix], 0, cb);
2502 }
2503  
2504 void
2505 stats(EV::Memcached self, ...)
2506 CODE:
2507 {
2508 1 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 1 0 1
MC_CROAK_UNLESS_CONNECTED(self); 0 1 1
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2509  
2510 1 int extra = items - 1;
2511 1 SV *cb = NULL;
2512 1 ▶ if (extra > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra > 0) { 1 0 1
2513 1 SV *cand = mc_cb_arg(aTHX_ ST(items-1));
2514 0 ▶ if (cand) { cb = cand; extra--; }
Branch: 1 branch
BranchTrueFalseTotal
if (cand) { cb = cand; extra--; } 0 0 0
2515 }
2516  
2517 0 STRLEN name_len = 0;
2518 0 const char *name = NULL;
2519 0 ▶ if (extra > 0 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (extra > 0 && SvOK(ST(1))) { 0 0 0
if (extra > 0 && SvOK(ST(1))) { 0 0 0
2520 0 name = SvPV(ST(1), name_len);
2521 }
2522  
2523 0 mc_enqueue_cmd(aTHX_ self, MC_OP_STAT, name, name_len, NULL, 0,
2524 NULL, 0, 0, CB_CMD_STATS, 0, cb);
2525 }
2526  
2527 void
2528 mget(EV::Memcached self, SV *keys_sv, SV *cb_sv = &PL_sv_undef)
2529 ALIAS:
2530 mgets = 1
2531 CODE:
2532 {
2533 2 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 2 0 2
MC_CROAK_UNLESS_CONNECTED(self); 0 2 2
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2534  
2535 2 ▶ if (!SvROK(keys_sv) || SvTYPE(SvRV(keys_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(keys_sv) || SvTYPE(SvRV(keys_sv)) != SVt_PVAV) 2 0 2
if (!SvROK(keys_sv) || SvTYPE(SvRV(keys_sv)) != SVt_PVAV) 0 2 2
2536 0 croak("mget requires an array reference");
2537  
2538 2 SV *cb = mc_cb_arg(aTHX_ cb_sv);
2539 0 AV *keys_av = (AV *)SvRV(keys_sv);
2540  
2541 0 mc_enqueue_mget(aTHX_ self, keys_av, cb, ix);
2542 }
2543  
2544 void
2545 sasl_auth(EV::Memcached self, SV *user_sv, SV *pass_sv, SV *cb_sv = &PL_sv_undef)
2546 CODE:
2547 {
2548 1 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 1 0 1
MC_CROAK_UNLESS_CONNECTED(self); 0 1 1
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2549  
2550 STRLEN ulen, plen;
2551 1 ▶ const char *user = SvPV(user_sv, ulen);
Branch: 1 branch
BranchTrueFalseTotal
const char *user = SvPV(user_sv, ulen); 1 0 1
2552 1 const char *pass = SvPV(pass_sv, plen);
2553 1 SV *cb = mc_cb_arg(aTHX_ cb_sv);
2554  
2555 0 size_t vlen = 1 + ulen + 1 + plen;
2556 char *authdata;
2557 0 Newx(authdata, vlen, char);
2558 0 authdata[0] = '\0';
2559 0 memcpy(authdata + 1, user, ulen);
2560 0 authdata[1 + ulen] = '\0';
2561 0 memcpy(authdata + 2 + ulen, pass, plen);
2562  
2563 0 mc_enqueue_cmd(aTHX_ self, MC_OP_SASL_AUTH, "PLAIN", 5, authdata, vlen,
2564 NULL, 0, 0, CB_CMD_SASL_AUTH, 0, cb);
2565 0 Safefree(authdata);
2566 }
2567  
2568 void
2569 sasl_list_mechs(EV::Memcached self, SV *cb_sv = &PL_sv_undef)
2570 CODE:
2571 {
2572 1 ▶ MC_CROAK_UNLESS_CONNECTED(self);
Branch: 4 branches
BranchTrueFalseTotal
MC_CROAK_UNLESS_CONNECTED(self); 1 0 1
MC_CROAK_UNLESS_CONNECTED(self); 0 1 1
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
MC_CROAK_UNLESS_CONNECTED(self); 0 0 0
2573  
2574 1 SV *cb = mc_cb_arg(aTHX_ cb_sv);
2575  
2576 0 mc_enqueue_cmd(aTHX_ self, MC_OP_SASL_LIST_MECHS, NULL, 0, NULL, 0,
2577 NULL, 0, 0, CB_CMD_SASL_LIST, 0, cb);
2578 }
2579  
2580 int
2581 pending_count(EV::Memcached self)
2582 CODE:
2583 1 ▶ RETVAL = self->pending_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->pending_count; 0 1 1
2584 OUTPUT:
2585 RETVAL
2586  
2587 int
2588 waiting_count(EV::Memcached self)
2589 CODE:
2590 3 ▶ RETVAL = self->waiting_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->waiting_count; 0 3 3
2591 OUTPUT:
2592 RETVAL
2593  
2594 int
2595 max_pending(EV::Memcached self, ...)
2596 CODE:
2597 {
2598 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2599 0 int old = self->max_pending;
2600 0 self->max_pending = SvIV(ST(1));
2601 0 ▶ if (self->max_pending < 0) self->max_pending = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->max_pending < 0) self->max_pending = 0; 0 0 0
2602 /* If limit increased, drain waiting queue */
2603 0 ▶ if (self->connected && self->max_pending > old)
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected && self->max_pending > old) 0 0 0
if (self->connected && self->max_pending > old) 0 0 0
2604 0 send_next_waiting(aTHX_ self);
2605 }
2606 0 ▶ RETVAL = self->max_pending;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->max_pending; 0 0 0
2607 }
2608 OUTPUT:
2609 RETVAL
2610  
2611 int
2612 waiting_timeout(EV::Memcached self, ...)
2613 CODE:
2614 {
2615 1 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 1 0 1
2616 1 self->waiting_timeout_ms = SvIV(ST(1));
2617 1 ▶ if (self->waiting_timeout_ms < 0) self->waiting_timeout_ms = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timeout_ms < 0) self->waiting_timeout_ms = 0; 0 1 1
2618 1 ▶ if (self->waiting_timeout_ms == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->waiting_timeout_ms == 0) 0 1 1
2619 0 stop_waiting_timer(self);
2620 1 ▶ else if (!ngx_queue_empty(&self->wait_queue))
Branch: 1 branch
BranchTrueFalseTotal
else if (!ngx_queue_empty(&self->wait_queue)) 1 0 1
2621 1 schedule_waiting_timer(self);
2622 }
2623 1 ▶ RETVAL = self->waiting_timeout_ms;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->waiting_timeout_ms; 0 1 1
2624 }
2625 OUTPUT:
2626 RETVAL
2627  
2628 int
2629 resume_waiting_on_reconnect(EV::Memcached self, ...)
2630 CODE:
2631 {
2632 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2633 0 self->resume_waiting_on_reconnect = SvTRUE(ST(1)) ? 1 : 0;
2634 }
2635 0 ▶ RETVAL = self->resume_waiting_on_reconnect;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->resume_waiting_on_reconnect; 0 0 0
2636 }
2637 OUTPUT:
2638 RETVAL
2639  
2640 int
2641 connect_timeout(EV::Memcached self, ...)
2642 CODE:
2643 {
2644 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2645 0 self->connect_timeout_ms = SvIV(ST(1));
2646 0 ▶ if (self->connect_timeout_ms < 0) self->connect_timeout_ms = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timeout_ms < 0) self->connect_timeout_ms = 0; 0 0 0
2647 }
2648 0 ▶ RETVAL = self->connect_timeout_ms;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->connect_timeout_ms; 0 0 0
2649 }
2650 OUTPUT:
2651 RETVAL
2652  
2653 int
2654 command_timeout(EV::Memcached self, ...)
2655 CODE:
2656 {
2657 3 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 1 2 3
2658 1 self->command_timeout_ms = SvIV(ST(1));
2659 1 ▶ if (self->command_timeout_ms < 0) self->command_timeout_ms = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->command_timeout_ms < 0) self->command_timeout_ms = 0; 0 1 1
2660 1 ▶ if (self->command_timeout_ms == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->command_timeout_ms == 0) 0 1 1
2661 0 disarm_cmd_timer(self);
2662 1 ▶ else if (!ngx_queue_empty(&self->cb_queue))
Branch: 1 branch
BranchTrueFalseTotal
else if (!ngx_queue_empty(&self->cb_queue)) 0 1 1
2663 0 arm_cmd_timer(self);
2664 }
2665 3 ▶ RETVAL = self->command_timeout_ms;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->command_timeout_ms; 0 3 3
2666 }
2667 OUTPUT:
2668 RETVAL
2669  
2670 void
2671 reconnect(EV::Memcached self, int enable, int delay_ms = 1000, int max_attempts = 0)
2672 CODE:
2673 {
2674 0 self->reconnect = enable ? 1 : 0;
2675 0 self->reconnect_delay_ms = delay_ms >= 0 ? delay_ms : 0;
2676 0 self->max_reconnect_attempts = max_attempts >= 0 ? max_attempts : 0;
2677 0 ▶ if (!enable) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enable) { 0 0 0
2678 0 self->reconnect_attempts = 0;
2679 0 stop_reconnect_timer(self);
2680 }
2681 }
2682  
2683 int
2684 reconnect_enabled(EV::Memcached self)
2685 CODE:
2686 0 ▶ RETVAL = self->reconnect;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->reconnect; 0 0 0
2687 OUTPUT:
2688 RETVAL
2689  
2690 int
2691 priority(EV::Memcached self, ...)
2692 CODE:
2693 {
2694 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2695 0 self->priority = SvIV(ST(1));
2696 0 ▶ if (self->priority < -2) self->priority = -2;
Branch: 1 branch
BranchTrueFalseTotal
if (self->priority < -2) self->priority = -2; 0 0 0
2697 0 ▶ if (self->priority > 2) self->priority = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (self->priority > 2) self->priority = 2; 0 0 0
2698 /* Apply to active watchers */
2699 0 ▶ if (self->reading) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reading) { 0 0 0
2700 0 ev_io_stop(self->loop, &self->rio);
2701 0 ev_set_priority(&self->rio, self->priority);
2702 0 ev_io_start(self->loop, &self->rio);
2703 } else {
2704 0 ev_set_priority(&self->rio, self->priority);
2705 }
2706 0 ▶ if (self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->writing) { 0 0 0
2707 0 ev_io_stop(self->loop, &self->wio);
2708 0 ev_set_priority(&self->wio, self->priority);
2709 0 ev_io_start(self->loop, &self->wio);
2710 } else {
2711 0 ev_set_priority(&self->wio, self->priority);
2712 }
2713 }
2714 0 ▶ RETVAL = self->priority;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->priority; 0 0 0
2715 }
2716 OUTPUT:
2717 RETVAL
2718  
2719 int
2720 keepalive(EV::Memcached self, ...)
2721 CODE:
2722 {
2723 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2724 0 self->keepalive = SvIV(ST(1));
2725 0 ▶ if (self->keepalive < 0) self->keepalive = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->keepalive < 0) self->keepalive = 0; 0 0 0
2726 0 ▶ if (self->connected && self->fd >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected && self->fd >= 0) 0 0 0
if (self->connected && self->fd >= 0) 0 0 0
2727 0 apply_keepalive(self);
2728 }
2729 0 ▶ RETVAL = self->keepalive;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->keepalive; 0 0 0
2730 }
2731 OUTPUT:
2732 RETVAL
2733  
2734 void
2735 skip_pending(EV::Memcached self)
2736 CODE:
2737 {
2738 2 self->callback_depth++;
2739 2 skip_pending_impl(aTHX_ self, err_skipped);
2740 2 self->callback_depth--;
2741 2 check_destroyed(self);
2742 }
2743  
2744 void
2745 skip_waiting(EV::Memcached self)
2746 CODE:
2747 {
2748 0 self->callback_depth++;
2749 0 cancel_waiting(aTHX_ self, err_skipped);
2750 0 self->callback_depth--;
2751 0 check_destroyed(self);
2752 }
↑ Summary blib/lib/EV/Memcached.pm »