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