Memcached.xs

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