Gearman.xs

filter: Statementstmt 73.8% 1124 / 1522 Branchbran 51.9% 659 / 1268 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 63.9% 1783 / 2790 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/EV/Gearman/Job.pm ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #define NEED_mg_findext /* ppport fallback for perls < 5.13.8 */
6 #include "ppport.h"
7  
8 #include "EVAPI.h"
9 #include "ngx-queue.h"
10  
11 #include <sys/socket.h>
12 #include <sys/un.h>
13 #include <netinet/in.h>
14 #include <netinet/tcp.h>
15 #include <netdb.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <arpa/inet.h>
23  
24 /* ================================================================
25 * Gearman binary protocol constants
26 *
27 * Wire format (all integers big-endian):
28 * Bytes 0-3 : magic - "\0REQ" for client->server, "\0RES" for server->client
29 * Bytes 4-7 : command (uint32 BE)
30 * Bytes 8-11: data length (uint32 BE)
31 * Bytes 12+ : data, with NUL-separated args; final arg has no terminator
32 * ================================================================ */
33  
34 #define GM_MAGIC_REQ "\0REQ"
35 #define GM_MAGIC_RES "\0RES"
36 #define GM_HEADER_SIZE 12
37  
38 #define GM_MAGIC_ALIVE 0xCA7C00FE
39 #define GM_MAGIC_FREED 0xDEAD0FF0
40  
41 /* Commands (request: client/worker -> server) */
42 #define GM_CMD_CAN_DO 1
43 #define GM_CMD_CANT_DO 2
44 #define GM_CMD_RESET_ABILITIES 3
45 #define GM_CMD_PRE_SLEEP 4
46 #define GM_CMD_NOOP 6
47 #define GM_CMD_SUBMIT_JOB 7
48 #define GM_CMD_JOB_CREATED 8
49 #define GM_CMD_GRAB_JOB 9
50 #define GM_CMD_NO_JOB 10
51 #define GM_CMD_JOB_ASSIGN 11
52 #define GM_CMD_WORK_STATUS 12
53 #define GM_CMD_WORK_COMPLETE 13
54 #define GM_CMD_WORK_FAIL 14
55 #define GM_CMD_GET_STATUS 15
56 #define GM_CMD_ECHO_REQ 16
57 #define GM_CMD_ECHO_RES 17
58 #define GM_CMD_SUBMIT_JOB_BG 18
59 #define GM_CMD_ERROR 19
60 #define GM_CMD_STATUS_RES 20
61 #define GM_CMD_SUBMIT_JOB_HIGH 21
62 #define GM_CMD_SET_CLIENT_ID 22
63 #define GM_CMD_CAN_DO_TIMEOUT 23
64 #define GM_CMD_ALL_YOURS 24
65 #define GM_CMD_WORK_EXCEPTION 25
66 #define GM_CMD_OPTION_REQ 26
67 #define GM_CMD_OPTION_RES 27
68 #define GM_CMD_WORK_DATA 28
69 #define GM_CMD_WORK_WARNING 29
70 #define GM_CMD_GRAB_JOB_UNIQ 30
71 #define GM_CMD_JOB_ASSIGN_UNIQ 31
72 #define GM_CMD_SUBMIT_JOB_HIGH_BG 32
73 #define GM_CMD_SUBMIT_JOB_LOW 33
74 #define GM_CMD_SUBMIT_JOB_LOW_BG 34
75 #define GM_CMD_SUBMIT_JOB_SCHED 35
76 #define GM_CMD_SUBMIT_JOB_EPOCH 36
77 #define GM_CMD_GET_STATUS_UNIQUE 41
78 #define GM_CMD_STATUS_RES_UNIQUE 42
79  
80 /* Internal request types (CB_*) — describe what we're waiting for */
81 #define CB_ECHO 1 /* expects ECHO_RES */
82 #define CB_SUBMIT 2 /* expects JOB_CREATED + work events */
83 #define CB_SUBMIT_BG 3 /* expects JOB_CREATED only */
84 #define CB_GET_STATUS 4 /* expects STATUS_RES */
85 #define CB_GET_STATUS_UNIQ 5 /* expects STATUS_RES_UNIQUE */
86 #define CB_OPTION 6 /* expects OPTION_RES */
87 #define CB_GRAB_JOB 7 /* expects JOB_ASSIGN | JOB_ASSIGN_UNIQ | NO_JOB */
88 #define CB_ADMIN 8 /* expects text-protocol response */
89  
90 #define BUF_INIT_SIZE 16384
91 #define GM_MAX_PACKET (256u * 1024u * 1024u) /* 256 MB sanity bound */
92 /* Admin (text-protocol) replies carry no length field, only a line
93 terminator, so GM_MAX_PACKET cannot bound them. A peer that never
94 sends the terminator would otherwise grow the read buffer without
95 limit; cap the buffered reply and drop the connection past this. */
96 #define GM_MAX_ADMIN_RESPONSE (16u * 1024u * 1024u)
97 /* Once a buffer has fully drained, release it back to BUF_INIT_SIZE if
98 it grew past this — a single large packet otherwise pins its
99 high-water-mark allocation for the life of the connection. */
100 #define BUF_SHRINK_THRESHOLD (1u * 1024u * 1024u)
101  
102 #define CLEAR_HANDLER(field) \
103 do { if (NULL != (field)) { SvREFCNT_dec(field); (field) = NULL; } } while(0)
104  
105 /* Silently drain an ngx_queue, freeing each entry via its cleanup
106 function. Used during global destruction and DESTROY where invoking
107 Perl callbacks would be unsafe / pointless. */
108 #define DRAIN_QUEUE_SILENT(qhead, type, cleanup) \
109 while (!ngx_queue_empty(qhead)) { \
110 ngx_queue_t *_q = ngx_queue_head(qhead); \
111 type *_e = ngx_queue_data(_q, type, queue); \
112 ngx_queue_remove(_q); \
113 cleanup(aTHX_ _e); \
114 }
115  
116 #define GM_CROAK_UNLESS_ALIVE(self) \
117 do { \
118 if (!(self)->connected && !(self)->connecting && \
119 !((self)->reconnect && (self)->reconnect_timer_active)) \
120 croak("not connected"); \
121 } while(0)
122  
123 /* ================================================================
124 * Type declarations
125 * ================================================================ */
126  
127 typedef struct ev_gm_s ev_gm_t;
128 typedef struct ev_gm_req_s ev_gm_req_t;
129 typedef struct ev_gm_wait_s ev_gm_wait_t;
130 typedef struct ev_gm_func_s ev_gm_func_t;
131 typedef struct ev_gm_active_s ev_gm_active_t;
132  
133 typedef ev_gm_t* EV__Gearman;
134  
135 /* Request awaiting a response (FIFO). For SUBMIT_JOB, holds the
136 * callback bundle that gets transferred to active_jobs once the
137 * JOB_CREATED handle is known. */
138 struct ev_gm_req_s {
139 ngx_queue_t queue;
140 int kind; /* CB_* */
141 SV *cb; /* primary callback (handle for BG, result for FG) */
142 SV *on_data;
143 SV *on_warning;
144 SV *on_status;
145 SV *on_exception;
146 int admin_terminator; /* 0 = single-line; 1 = ".\n" terminator (status/workers) */
147 ev_tstamp sent_at; /* ev_now() when queued on cb_queue; per-request
148 command_timeout budget starts here */
149 /* conn_gen at queue time. A teardown drain only fails entries from
150 dead sessions (gen != current): work submitted on a nested new
151 session mid-drain is live and must survive the outer drain. */
152 unsigned int gen;
153 };
154  
155 /* Pending packet awaiting connect / drain */
156 struct ev_gm_wait_s {
157 ngx_queue_t queue;
158 char *packet;
159 size_t packet_len;
160 /* if non-NULL, becomes a request entry on send */
161 ev_gm_req_t *req;
162 /* conn_gen at queue time; teardown drains only dead sessions */
163 unsigned int gen;
164 };
165  
166 /* Active foreground job (after JOB_CREATED) */
167 struct ev_gm_active_s {
168 ngx_queue_t queue;
169 char *handle;
170 STRLEN handle_len;
171 SV *on_complete;
172 SV *on_data;
173 SV *on_warning;
174 SV *on_status;
175 SV *on_exception;
176 };
177  
178 /* Registered worker function */
179 struct ev_gm_func_s {
180 ngx_queue_t queue;
181 char *name;
182 SV *cb;
183 int async; /* if 1, callback returns nothing; user calls $job->complete */
184 int timeout; /* seconds; 0 = no timeout */
185 int no_cb_warned; /* warned once about dispatching to a cb-less can_do entry */
186 };
187  
188 struct ev_gm_s {
189 unsigned int magic;
190 struct ev_loop *loop;
191 SV *loop_sv; /* strong ref on a user-supplied loop => EV::Loop object */
192 int fd;
193 int connected;
194 int connecting;
195 /* Bumped by cleanup_connection. The read path snapshots it across
196 callbacks: a disconnect()+connect() flip defeats `connected`
197 checks, so only the generation proves the session survived. */
198 unsigned int conn_gen;
199  
200 /* IO watchers */
201 ev_io rio, wio;
202 int reading, writing;
203  
204 /* Buffers */
205 char *rbuf;
206 size_t rbuf_len, rbuf_cap;
207 char *wbuf;
208 size_t wbuf_len, wbuf_off, wbuf_cap;
209  
210 /* User-level callbacks */
211 SV *on_error;
212 SV *on_connect;
213 SV *on_disconnect;
214  
215 /* FIFO of requests awaiting a binary response */
216 ngx_queue_t cb_queue;
217 int pending_count;
218  
219 /* Pending packets awaiting transmission (e.g. before connect) */
220 ngx_queue_t wait_queue;
221 int waiting_count;
222  
223 /* Active jobs (foreground) by handle */
224 ngx_queue_t active_jobs;
225 int active_count_cached;
226  
227 /* Worker state */
228 ngx_queue_t functions;
229 int worker_active; /* in worker loop */
230 int worker_sleeping; /* PRE_SLEEP issued, awaiting NOOP */
231 int worker_grab_inflight;/* GRAB_JOB[_UNIQ] in flight */
232 int worker_grab_uniq; /* prefer GRAB_JOB_UNIQ */
233 SV *worker_on_idle; /* fires when no jobs available */
234 int worker_one_shot; /* if 1, exit worker loop after first job */
235 char *client_id; /* for SET_CLIENT_ID */
236  
237 /* Connection target */
238 char *host;
239 int port;
240 char *path;
241  
242 /* getaddrinfo fallback list (TCP): live only while a connect
243 sequence walks it; freed on success, exhaustion or disconnect. */
244 struct addrinfo *ai_list;
245 struct addrinfo *ai_cur;
246  
247 /* Reconnect */
248 int reconnect;
249 int reconnect_delay_ms;
250 int max_reconnect_attempts;
251 int reconnect_attempts;
252 ev_timer reconnect_timer;
253 int reconnect_timer_active;
254 int intentional_disconnect;
255  
256 /* Timeouts */
257 int connect_timeout_ms;
258 ev_timer connect_timer;
259 int connect_timer_active;
260 int command_timeout_ms;
261 ev_timer cmd_timer;
262 int cmd_timer_active;
263  
264 /* Safety */
265 int callback_depth;
266 int in_wait_cleanup;
267 int in_active_cleanup;
268 /* Live EV::Gearman::Job objects holding a tombstone reference on
269 this struct (via sv_magicext on the job HV). While > 0 the
270 struct is never Safefreed — a DESTROYed connection becomes an
271 inert tombstone until the last job releases it, so job_resolve's
272 magic-word check never reads freed memory. */
273 U32 job_refs;
274 SV *job_session; /* shared identity for jobs on this connection */
275  
276 /* Options */
277 int priority;
278 int keepalive;
279  
280 /* Options sent on connect */
281 int opt_exceptions;
282 };
283  
284 /* ================================================================
285 * Forward declarations
286 * ================================================================ */
287  
288 static void io_cb(EV_P_ ev_io *w, int revents);
289 static void reconnect_timer_cb(EV_P_ ev_timer *w, int revents);
290 static void connect_timeout_cb(EV_P_ ev_timer *w, int revents);
291 static void cmd_timeout_cb(EV_P_ ev_timer *w, int revents);
292 static void start_reading(ev_gm_t *self);
293 static void stop_reading(ev_gm_t *self);
294 static void start_writing(ev_gm_t *self);
295 static void stop_writing(ev_gm_t *self);
296 static void start_connect(pTHX_ ev_gm_t *self);
297 static void cleanup_connection(pTHX_ ev_gm_t *self);
298 static void emit_error(pTHX_ ev_gm_t *self, const char *msg);
299 static void handle_disconnect(pTHX_ ev_gm_t *self, const char *reason);
300 static void schedule_reconnect(pTHX_ ev_gm_t *self);
301 static void apply_keepalive(ev_gm_t *self);
302 static void report_connect_error(pTHX_ ev_gm_t *self, const char *errbuf);
303 static void finish_connect_success(pTHX_ ev_gm_t *self);
304 static void stop_connect_timer(ev_gm_t *self);
305 static void stop_reconnect_timer(ev_gm_t *self);
306 static int check_destroyed(ev_gm_t *self);
307 static void clear_addrinfo(ev_gm_t *self);
308 static void connect_try_from(pTHX_ ev_gm_t *self, const char *prev_err);
309 static void cancel_pending(pTHX_ ev_gm_t *self, const char *reason);
310 static void cancel_waiting(pTHX_ ev_gm_t *self, const char *reason);
311 static void cancel_active(pTHX_ ev_gm_t *self, const char *reason);
312 static void send_pending_waits(pTHX_ ev_gm_t *self);
313 static void arm_cmd_timer(ev_gm_t *self);
314 static void disarm_cmd_timer(ev_gm_t *self);
315 static void enqueue_packet(pTHX_ ev_gm_t *self,
316 uint32_t cmd, const char *data, size_t data_len, ev_gm_req_t *req);
317 static void worker_continue(pTHX_ ev_gm_t *self);
318 static void worker_send_grab(pTHX_ ev_gm_t *self);
319 static void worker_send_pre_sleep(pTHX_ ev_gm_t *self);
320 static ev_gm_func_t* find_function(ev_gm_t *self, const char *name, STRLEN len);
321  
322 /* ================================================================
323 * Big-endian helpers (no unaligned access)
324 * ================================================================ */
325  
326 202 static void gm_write_u32(char *buf, uint32_t val) {
327 202 val = htonl(val);
328 202 memcpy(buf, &val, 4);
329 202 }
330  
331 160 static uint32_t gm_read_u32(const char *buf) {
332 uint32_t val;
333 160 memcpy(&val, buf, 4);
334 160 return ntohl(val);
335 }
336  
337 /* atoi/strtol on a length-bounded ASCII run that may not be NUL-terminated
338 (gm_arg's last field returns a pointer with no trailing NUL since the
339 separator-NUL only lives between fields). Copies up to 31 bytes onto
340 the stack first. */
341 0 static IV gm_atoi_n(const char *p, size_t len) {
342 char buf[32];
343 0 ▶ if (len > sizeof(buf) - 1) len = sizeof(buf) - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (len > sizeof(buf) - 1) len = sizeof(buf) - 1; 0 0 0
344 0 memcpy(buf, p, len);
345 0 buf[len] = '\0';
346 0 return (IV)atoi(buf);
347 }
348  
349 /* ================================================================
350 * Buffer management
351 * ================================================================ */
352  
353 100 static void buf_ensure_write(ev_gm_t *self, size_t needed) {
354 /* Compact first to reclaim already-sent prefix. */
355 100 ▶ if (self->wbuf_off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_off > 0) { 0 100 100
356 0 size_t live = self->wbuf_len - self->wbuf_off;
357 0 ▶ if (live > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (live > 0) 0 0 0
358 0 memmove(self->wbuf, self->wbuf + self->wbuf_off, live);
359 0 self->wbuf_len = live;
360 0 self->wbuf_off = 0;
361 }
362 100 size_t total = self->wbuf_len + needed;
363 100 ▶ if (self->wbuf_cap >= total) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_cap >= total) return; 99 1 100
364 1 ▶ size_t new_cap = self->wbuf_cap ? self->wbuf_cap : BUF_INIT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = self->wbuf_cap ? self->wbuf_cap : BUF_INIT_SIZE; 1 0 1
365 3 ▶ while (new_cap < total) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < total) new_cap *= 2; 2 1 3
366 1 Renew(self->wbuf, new_cap, char);
367 1 self->wbuf_cap = new_cap;
368 }
369  
370 344 static void buf_ensure_read(ev_gm_t *self, size_t needed) {
371 344 size_t total = self->rbuf_len + needed;
372 344 ▶ if (self->rbuf_cap >= total) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_cap >= total) return; 331 13 344
373 13 ▶ size_t new_cap = self->rbuf_cap ? self->rbuf_cap : BUF_INIT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = self->rbuf_cap ? self->rbuf_cap : BUF_INIT_SIZE; 13 0 13
374 26 ▶ while (new_cap < total) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < total) new_cap *= 2; 13 13 26
375 13 Renew(self->rbuf, new_cap, char);
376 13 self->rbuf_cap = new_cap;
377 }
378  
379 100 static void buf_append_write(ev_gm_t *self, const char *data, size_t len) {
380 100 buf_ensure_write(self, len);
381 100 memcpy(self->wbuf + self->wbuf_len, data, len);
382 100 self->wbuf_len += len;
383 100 }
384  
385 /* Release an oversized, fully-drained buffer back to the initial size.
386 Caller guarantees the buffer holds no live bytes. */
387 117 static void buf_maybe_shrink(char **buf, size_t *cap) {
388 117 ▶ if (*cap > BUF_SHRINK_THRESHOLD) {
Branch: 1 branch
BranchTrueFalseTotal
if (*cap > BUF_SHRINK_THRESHOLD) { 0 117 117
389 0 Renew(*buf, BUF_INIT_SIZE, char);
390 0 *cap = BUF_INIT_SIZE;
391 }
392 117 }
393  
394 /* ================================================================
395 * Watcher helpers
396 * ================================================================ */
397  
398 53 static void start_reading(ev_gm_t *self) {
399 53 ▶ if (!self->reading && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->reading && self->fd >= 0) { 53 0 53
if (!self->reading && self->fd >= 0) { 53 0 53
400 53 ev_io_start(self->loop, &self->rio);
401 53 self->reading = 1;
402 }
403 53 }
404  
405 90 static void stop_reading(ev_gm_t *self) {
406 90 ▶ if (self->reading) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reading) { 53 37 90
407 53 ev_io_stop(self->loop, &self->rio);
408 53 self->reading = 0;
409 }
410 90 }
411  
412 164 static void start_writing(ev_gm_t *self) {
413 164 ▶ if (!self->writing && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->writing && self->fd >= 0) { 134 30 164
if (!self->writing && self->fd >= 0) { 134 0 134
414 134 ev_io_start(self->loop, &self->wio);
415 134 self->writing = 1;
416 }
417 164 }
418  
419 215 static void stop_writing(ev_gm_t *self) {
420 215 ▶ if (self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->writing) { 134 81 215
421 134 ev_io_stop(self->loop, &self->wio);
422 134 self->writing = 0;
423 }
424 215 }
425  
426 /* "Is dead" predicate with the free as a side effect. Callers use the
427 return value as "the object is dead, STOP TOUCHING IT" — which must
428 not be conflated with "it was freed": with jobs stashed, a destroyed
429 client has magic == FREED but job_refs > 0, and falls through to
430 nothing. The struct is Safefreed exactly once: when it is dead AND
431 the callback depth has unwound AND no job tombstone refs remain. */
432 780 static int check_destroyed(ev_gm_t *self) {
433 780 ▶ if (self->magic != GM_MAGIC_FREED) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_FREED) return 0; 714 66 780
434 66 ▶ if (self->callback_depth == 0 && self->job_refs == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (self->callback_depth == 0 && self->job_refs == 0) 66 0 66
if (self->callback_depth == 0 && self->job_refs == 0) 66 0 66
435 66 Safefree(self);
436 66 return 1; /* dead either way — caller must stop */
437 }
438  
439 /* ================================================================
440 * Callback invocation helpers
441 * ================================================================ */
442  
443 /* Invoke Perl callback with up to two args (any may be NULL = undef).
444 * Mortal SV transfer: caller passes refcnt=1 SVs, we mortalize them. */
445 41 static void invoke_cb2(pTHX_ ev_gm_t *self, SV *cb, SV *a, SV *b) {
446 41 ▶ if (!cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cb) { 0 41 41
447 0 ▶ if (a) SvREFCNT_dec(a);
Branch: 1 branch
BranchTrueFalseTotal
if (a) SvREFCNT_dec(a); 0 0 0
448 0 ▶ if (b) SvREFCNT_dec(b);
Branch: 1 branch
BranchTrueFalseTotal
if (b) SvREFCNT_dec(b); 0 0 0
449 0 return;
450 }
451 41 self->callback_depth++;
452 41 dSP;
453 41 ENTER;
454 41 SAVETMPS;
455 41 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 41 41
456 41 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 41 41
457 41 ▶ if (a) mPUSHs(a); else PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if (a) mPUSHs(a); else PUSHs(&PL_sv_undef); 21 20 41
458 41 ▶ if (b) mPUSHs(b); else PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if (b) mPUSHs(b); else PUSHs(&PL_sv_undef); 20 21 41
459 41 PUTBACK;
460 41 call_sv(cb, G_DISCARD | G_EVAL);
461 41 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 41 0 41
if (SvTRUE(ERRSV)) { 0 41 41
462 0 ▶ warn("EV::Gearman: callback error: %s", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Gearman: callback error: %s", SvPV_nolen(ERRSV)); 0 0 0
463 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
464 }
465 41 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 41 0 41
466 41 LEAVE;
467 41 self->callback_depth--;
468 }
469  
470 /* Invoke a handler with at most one mortal arg; eat exceptions. */
471 134 static void invoke_handler(pTHX_ ev_gm_t *self, SV *cb, SV *arg, const char *label) {
472 134 ▶ if (!cb) { if (arg) SvREFCNT_dec(arg); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (!cb) { if (arg) SvREFCNT_dec(arg); return; } 42 92 134
if (!cb) { if (arg) SvREFCNT_dec(arg); return; } 0 42 42
473 92 self->callback_depth++;
474 92 dSP;
475 92 ENTER;
476 92 SAVETMPS;
477 92 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 92 92
478 92 ▶ if (arg) XPUSHs(sv_2mortal(arg));
Branch: 2 branches
BranchTrueFalseTotal
if (arg) XPUSHs(sv_2mortal(arg)); 50 42 92
if (arg) XPUSHs(sv_2mortal(arg)); 0 50 50
479 92 PUTBACK;
480 92 call_sv(cb, G_DISCARD | G_EVAL);
481 92 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 92 0 92
if (SvTRUE(ERRSV)) { 0 92 92
482 0 ▶ warn("EV::Gearman: %s callback error: %s", label, SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("EV::Gearman: %s callback error: %s", label, SvPV_nolen(ERRSV)); 0 0 0
483 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
484 }
485 92 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 50 42 92
486 92 LEAVE;
487 92 self->callback_depth--;
488 }
489  
490 50 static void emit_error(pTHX_ ev_gm_t *self, const char *msg) {
491 50 invoke_handler(aTHX_ self, self->on_error, newSVpv(msg, 0), "on_error");
492 50 }
493  
494 53 static void emit_connect(pTHX_ ev_gm_t *self) {
495 53 invoke_handler(aTHX_ self, self->on_connect, NULL, "on_connect");
496 53 }
497  
498 24 static void emit_disconnect(pTHX_ ev_gm_t *self) {
499 24 invoke_handler(aTHX_ self, self->on_disconnect, NULL, "on_disconnect");
500 24 }
501  
502 0 static void apply_keepalive(ev_gm_t *self) {
503 0 ▶ if (self->path) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->path) return; 0 0 0
504 0 int enabled = self->keepalive > 0;
505 0 setsockopt(self->fd, SOL_SOCKET, SO_KEEPALIVE, &enabled, sizeof(enabled));
506 0 ▶ if (!enabled) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!enabled) return; 0 0 0
507 #ifdef TCP_KEEPIDLE
508 0 setsockopt(self->fd, IPPROTO_TCP, TCP_KEEPIDLE,
509 0 &self->keepalive, sizeof(self->keepalive));
510 #endif
511 }
512  
513 /* Set O_NONBLOCK and FD_CLOEXEC on an already-open socket fd. The
514 CLOEXEC step prevents the fd leaking into child processes spawned
515 via fork+exec while the connection is up. Returns 0 on success,
516 -1 on failure (caller should close and error out). */
517 85 static int gm_set_socket_flags(int fd) {
518 85 int fl = fcntl(fd, F_GETFL);
519 85 ▶ if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) return -1; 85 0 85
if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) return -1; 0 85 85
520 #ifdef FD_CLOEXEC
521 85 fl = fcntl(fd, F_GETFD);
522 85 ▶ if (fl >= 0) (void)fcntl(fd, F_SETFD, fl | FD_CLOEXEC);
Branch: 1 branch
BranchTrueFalseTotal
if (fl >= 0) (void)fcntl(fd, F_SETFD, fl | FD_CLOEXEC); 85 0 85
523 #endif
524 #ifdef SO_NOSIGPIPE
525 /* macOS/BSD lack MSG_NOSIGNAL; this is their per-socket way to
526 keep a write to a closed peer from raising SIGPIPE. A library
527 must never be able to kill its embedder with a signal. */
528 { int one = 1; (void)setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); }
529 #endif
530 85 return 0;
531 }
532  
533 /* Shared get/set body for on_error / on_connect / on_disconnect.
534 Explicit undef clears the slot; a defined non-coderef is ignored
535 (the old handler survives) instead of silently clearing it. */
536 19 static SV* handler_accessor(pTHX_ SV **slot, int items, SV *new_cb) {
537 19 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 15 4 19
538 15 ▶ if (!SvOK(new_cb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(new_cb)) { 2 13 15
539 2 ▶ CLEAR_HANDLER(*slot);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(*slot); 2 0 2
540 13 ▶ } else if (SvROK(new_cb) && SvTYPE(SvRV(new_cb)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvROK(new_cb) && SvTYPE(SvRV(new_cb)) == SVt_PVCV) { 12 1 13
} else if (SvROK(new_cb) && SvTYPE(SvRV(new_cb)) == SVt_PVCV) { 12 0 12
541 12 ▶ CLEAR_HANDLER(*slot);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(*slot); 1 11 12
542 12 *slot = newSVsv(new_cb);
543 }
544 }
545 19 ▶ return *slot ? newSVsv(*slot) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return *slot ? newSVsv(*slot) : &PL_sv_undef; 16 3 19
546 }
547  
548 /* ================================================================
549 * Allocators / cleaners
550 * ================================================================ */
551  
552 /* Allocate a request entry with kind set; if cb_sv is a valid coderef,
553 bumps its refcount into r->cb. Pass NULL (or a non-coderef) for no cb. */
554 66 static ev_gm_req_t* alloc_req(int kind, SV *cb_sv) {
555 dTHX;
556 ev_gm_req_t *r;
557 66 Newxz(r, 1, ev_gm_req_t);
558 66 r->kind = kind;
559 66 ▶ if (cb_sv && SvOK(cb_sv) && SvROK(cb_sv))
Branch: 3 branches
BranchTrueFalseTotal
if (cb_sv && SvOK(cb_sv) && SvROK(cb_sv)) 41 25 66
if (cb_sv && SvOK(cb_sv) && SvROK(cb_sv)) 41 0 41
if (cb_sv && SvOK(cb_sv) && SvROK(cb_sv)) 41 0 41
560 41 r->cb = newSVsv(cb_sv);
561 66 return r;
562 }
563  
564 66 static void cleanup_req(pTHX_ ev_gm_req_t *r) {
565 66 ▶ CLEAR_HANDLER(r->cb);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->cb); 40 26 66
566 66 ▶ CLEAR_HANDLER(r->on_data);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->on_data); 0 66 66
567 66 ▶ CLEAR_HANDLER(r->on_warning);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->on_warning); 0 66 66
568 66 ▶ CLEAR_HANDLER(r->on_status);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->on_status); 0 66 66
569 66 ▶ CLEAR_HANDLER(r->on_exception);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->on_exception); 0 66 66
570 66 Safefree(r);
571 66 }
572  
573 6 static void cleanup_wait(pTHX_ ev_gm_wait_t *w) {
574 6 Safefree(w->packet);
575 6 ▶ if (w->req) cleanup_req(aTHX_ w->req);
Branch: 1 branch
BranchTrueFalseTotal
if (w->req) cleanup_req(aTHX_ w->req); 6 0 6
576 6 Safefree(w);
577 6 }
578  
579 1 static void cleanup_active(pTHX_ ev_gm_active_t *a) {
580 1 Safefree(a->handle);
581 1 ▶ CLEAR_HANDLER(a->on_complete);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_complete); 1 0 1
582 1 ▶ CLEAR_HANDLER(a->on_data);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_data); 0 1 1
583 1 ▶ CLEAR_HANDLER(a->on_warning);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_warning); 0 1 1
584 1 ▶ CLEAR_HANDLER(a->on_status);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_status); 0 1 1
585 1 ▶ CLEAR_HANDLER(a->on_exception);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_exception); 0 1 1
586 1 Safefree(a);
587 1 }
588  
589 14 static void cleanup_func(pTHX_ ev_gm_func_t *f) {
590 14 Safefree(f->name);
591 14 ▶ CLEAR_HANDLER(f->cb);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(f->cb); 12 2 14
592 14 Safefree(f);
593 14 }
594  
595 /* ================================================================
596 * Cancel queues
597 * ================================================================ */
598  
599 /* Drain a queue, firing each entry's user callback with reason before
600 freeing it. Bails if a callback triggers DESTROY (magic flips to
601 FREED). The flag prevents reentrancy if a callback also calls
602 disconnect() while we are mid-loop. */
603 #define CANCEL_QUEUE(qhead, type, cleanup, counter, flag, cb_expr) do { \
604 if (self->flag) break; \
605 self->flag = 1; \
606 while (!ngx_queue_empty(qhead)) { \
607 ngx_queue_t *_q = ngx_queue_head(qhead); \
608 type *_e = ngx_queue_data(_q, type, queue); \
609 ngx_queue_remove(_q); \
610 self->counter--; \
611 if (cb_expr) \
612 invoke_cb2(aTHX_ self, cb_expr, NULL, newSVpv(reason, 0)); \
613 if (self->magic == GM_MAGIC_FREED) { \
614 cleanup(aTHX_ _e); \
615 self->flag = 0; \
616 return; \
617 } \
618 cleanup(aTHX_ _e); \
619 } \
620 self->flag = 0; \
621 } while (0)
622  
623 /* Drain cb_queue, failing entries from dead sessions (gen != current).
624 No reentrancy flag: nested disconnects drain what their own session
625 orphaned, while entries submitted on a nested live session survive.
626 New entries land at the tail, so stopping at the first live one is
627 exact. */
628 80 static void cancel_pending(pTHX_ ev_gm_t *self, const char *reason) {
629 94 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 15 79 94
630 15 ev_gm_req_t *head = ngx_queue_data(ngx_queue_head(&self->cb_queue),
631 ev_gm_req_t, queue);
632 15 ▶ if (head->gen == self->conn_gen) break;
Branch: 1 branch
BranchTrueFalseTotal
if (head->gen == self->conn_gen) break; 1 14 15
633 14 ngx_queue_remove(&head->queue);
634 14 self->pending_count--;
635 14 ▶ if (head->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (head->cb) 11 3 14
636 11 invoke_cb2(aTHX_ self, head->cb, NULL, newSVpv(reason, 0));
637 14 ▶ if (self->magic == GM_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) { 0 14 14
638 0 cleanup_req(aTHX_ head);
639 0 return;
640 }
641 14 cleanup_req(aTHX_ head);
642 }
643 }
644  
645 /* Drain wait_queue, failing entries from dead sessions (gen !=
646 current) and leaving entries submitted on a nested live session —
647 e.g. a retry queued while a nested connect is still in flight. The
648 count tracks removals exactly, so no reset is needed. */
649 86 static void cancel_waiting(pTHX_ ev_gm_t *self, const char *reason) {
650 86 ▶ if (self->in_wait_cleanup) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_wait_cleanup) return; 0 86 86
651 86 self->in_wait_cleanup = 1;
652 92 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 8 84 92
653 8 ev_gm_wait_t *head = ngx_queue_data(ngx_queue_head(&self->wait_queue),
654 ev_gm_wait_t, queue);
655 8 ▶ if (head->gen == self->conn_gen) break;
Branch: 1 branch
BranchTrueFalseTotal
if (head->gen == self->conn_gen) break; 2 6 8
656 6 ngx_queue_remove(&head->queue);
657 6 self->waiting_count--;
658 6 ▶ if (head->req && head->req->cb)
Branch: 2 branches
BranchTrueFalseTotal
if (head->req && head->req->cb) 6 0 6
if (head->req && head->req->cb) 6 0 6
659 6 invoke_cb2(aTHX_ self, head->req->cb, NULL, newSVpv(reason, 0));
660 6 ▶ if (self->magic == GM_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) { 0 6 6
661 0 cleanup_wait(aTHX_ head);
662 0 self->in_wait_cleanup = 0;
663 0 return;
664 }
665 6 cleanup_wait(aTHX_ head);
666 }
667 86 self->in_wait_cleanup = 0;
668 }
669  
670 80 static void cancel_active(pTHX_ ev_gm_t *self, const char *reason) {
671 80 ▶ if (self->in_active_cleanup) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->in_active_cleanup) return; 0 80 80
672 81 ▶ CANCEL_QUEUE(&self->active_jobs, ev_gm_active_t, cleanup_active,
Branch: 4 branches
BranchTrueFalseTotal
CANCEL_QUEUE(&self->active_jobs, ev_gm_active_t, cleanup_active, 0 80 80
CANCEL_QUEUE(&self->active_jobs, ev_gm_active_t, cleanup_active, 1 0 1
CANCEL_QUEUE(&self->active_jobs, ev_gm_active_t, cleanup_active, 0 1 1
CANCEL_QUEUE(&self->active_jobs, ev_gm_active_t, cleanup_active, 1 80 81
673 active_count_cached, in_active_cleanup, _e->on_complete);
674 80 self->active_count_cached = 0;
675 }
676  
677 /* ================================================================
678 * Connection cleanup / disconnect
679 * ================================================================ */
680  
681 90 static void cleanup_connection(pTHX_ ev_gm_t *self) {
682 90 stop_reading(self);
683 90 stop_writing(self);
684 90 stop_connect_timer(self);
685 90 disarm_cmd_timer(self);
686  
687 90 ▶ if (self->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->fd >= 0) { 59 31 90
688 59 close(self->fd);
689 59 self->fd = -1;
690 }
691 90 self->connected = 0;
692 90 self->connecting = 0;
693 90 self->conn_gen++;
694 90 self->rbuf_len = 0;
695 90 self->wbuf_len = 0;
696 90 self->wbuf_off = 0;
697 90 ▶ CLEAR_HANDLER(self->job_session);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->job_session); 10 80 90
698  
699 /* Worker state resets: a reconnect must re-issue CAN_DO and re-start
700 the GRAB loop from scratch. Registered functions are kept. */
701 90 self->worker_sleeping = 0;
702 90 self->worker_grab_inflight = 0;
703  
704 90 clear_addrinfo(self);
705 90 }
706  
707 /* Drop the resolved-address list, if any. It is only live while a
708 connect sequence is walking it. */
709 210 static void clear_addrinfo(ev_gm_t *self) {
710 210 ▶ if (self->ai_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ai_list) { 63 147 210
711 63 freeaddrinfo(self->ai_list);
712 63 self->ai_list = NULL;
713 63 self->ai_cur = NULL;
714 }
715 210 }
716  
717 24 static void handle_disconnect(pTHX_ ev_gm_t *self, const char *reason) {
718 24 int was_connected = self->connected;
719 24 cleanup_connection(aTHX_ self);
720  
721 24 cancel_pending(aTHX_ self, "disconnected");
722 24 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 24 24
723  
724 24 cancel_active(aTHX_ self, "disconnected");
725 24 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 24 24
726  
727 24 cancel_waiting(aTHX_ self, "disconnected");
728 24 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 24 24
729  
730 /* Arm the reconnect BEFORE firing user callbacks: re-queueing work
731 from on_disconnect/on_error is the documented purpose of the
732 wait queue, and GM_CROAK_UNLESS_ALIVE keys off the reconnect
733 timer being active. schedule_reconnect may itself emit_error
734 ("max reconnect attempts reached"), so re-check for DESTROY. */
735 24 ▶ if (!self->intentional_disconnect && self->reconnect)
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->reconnect) 16 8 24
if (!self->intentional_disconnect && self->reconnect) 5 11 16
736 5 schedule_reconnect(aTHX_ self);
737 24 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 24 24
738  
739 24 ▶ if (was_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if (was_connected) { 24 0 24
740 24 emit_disconnect(aTHX_ self);
741 24 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 24 24
742 }
743  
744 24 ▶ if (reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (reason) { 17 7 24
745 17 emit_error(aTHX_ self, reason);
746 17 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 17 17
747 }
748 }
749  
750 30 static void schedule_reconnect(pTHX_ ev_gm_t *self) {
751 /* DESTROY during a callback leaves a zombie (magic FREED, struct
752 kept alive by callback_depth); arming a timer on it would point
753 libev at freed memory once the depth unwinds. */
754 30 ▶ if (self->magic != GM_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) return; 0 30 30
755 /* A nested manual connect() can leave a live session behind by
756 the time the outer disconnect arms this timer; never arm onto
757 a live session. */
758 30 ▶ if (self->connected || self->connecting) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) return; 29 1 30
if (self->connected || self->connecting) return; 1 28 29
759 28 ▶ if (self->reconnect_timer_active) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timer_active) return; 0 28 28
760 28 ▶ if (self->max_reconnect_attempts > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (self->max_reconnect_attempts > 0 && 6 22 28
761 6 ▶ self->reconnect_attempts >= self->max_reconnect_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
self->reconnect_attempts >= self->max_reconnect_attempts) { 2 4 6
762 2 emit_error(aTHX_ self, "max reconnect attempts reached");
763 2 return;
764 }
765 26 self->reconnect_attempts++;
766 26 ev_tstamp delay = (ev_tstamp)self->reconnect_delay_ms / 1000.0;
767 26 ▶ if (delay < 0) delay = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay < 0) delay = 0; 0 26 26
768 26 ev_timer_init(&self->reconnect_timer, reconnect_timer_cb, delay, 0.0);
769 26 self->reconnect_timer.data = (void *)self;
770 26 ev_timer_start(self->loop, &self->reconnect_timer);
771 26 self->reconnect_timer_active = 1;
772 }
773  
774 213 static void stop_connect_timer(ev_gm_t *self) {
775 213 ▶ if (self->connect_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timer_active) { 0 213 213
776 0 ev_timer_stop(self->loop, &self->connect_timer);
777 0 self->connect_timer_active = 0;
778 }
779 213 }
780  
781 90 static void stop_reconnect_timer(ev_gm_t *self) {
782 90 ▶ if (self->reconnect_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timer_active) { 4 86 90
783 4 ev_timer_stop(self->loop, &self->reconnect_timer);
784 4 self->reconnect_timer_active = 0;
785 }
786 90 }
787  
788 /* The command timeout is per-request: the request at the head of
789 cb_queue must be answered within command_timeout_ms of its sent_at
790 stamp, regardless of how much unrelated traffic flows meanwhile.
791 Arm a one-shot timer for the current head's remaining budget;
792 callers re-arm whenever the head changes (pop) or the queue empties
793 (disarm via the empty-check here). */
794 61 static void arm_cmd_timer(ev_gm_t *self) {
795 61 ▶ if (self->cmd_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cmd_timer_active) { 0 61 61
796 0 ev_timer_stop(self->loop, &self->cmd_timer);
797 0 self->cmd_timer_active = 0;
798 }
799 61 ▶ if (self->command_timeout_ms <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->command_timeout_ms <= 0) return; 57 4 61
800 4 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 4 4
801 4 ▶ if (ngx_queue_empty(&self->cb_queue)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) return; 0 4 4
802 4 ev_gm_req_t *head = ngx_queue_data(ngx_queue_head(&self->cb_queue),
803 ev_gm_req_t, queue);
804 4 ev_tstamp budget = (ev_tstamp)self->command_timeout_ms / 1000.0;
805 4 ev_tstamp remaining = head->sent_at + budget - ev_now(self->loop);
806 4 ▶ if (remaining < 0) remaining = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (remaining < 0) remaining = 0; 0 4 4
807 4 ev_timer_init(&self->cmd_timer, cmd_timeout_cb, remaining, 0.0);
808 4 self->cmd_timer.data = (void *)self;
809 4 ev_timer_start(self->loop, &self->cmd_timer);
810 4 self->cmd_timer_active = 1;
811 }
812  
813 201 static void disarm_cmd_timer(ev_gm_t *self) {
814 201 ▶ if (self->cmd_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cmd_timer_active) { 1 200 201
815 1 ev_timer_stop(self->loop, &self->cmd_timer);
816 1 self->cmd_timer_active = 0;
817 }
818 201 }
819  
820 3 static void cmd_timeout_cb(EV_P_ ev_timer *w, int revents) {
821 dTHX;
822 3 ev_gm_t *self = (ev_gm_t *)w->data;
823 (void)loop; (void)revents;
824 3 ▶ if (self->magic != GM_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) return; 0 3 3
825 3 self->cmd_timer_active = 0; /* one-shot: already fired */
826 3 ▶ if (ngx_queue_empty(&self->cb_queue)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) return; 0 3 3
827 /* The head may have changed since this timer was armed (every arm
828 is for the then-head's budget); a current head still within
829 budget gets a re-arm for its remainder, not a disconnect. */
830 3 ev_gm_req_t *head = ngx_queue_data(ngx_queue_head(&self->cb_queue),
831 ev_gm_req_t, queue);
832 3 ev_tstamp budget = (ev_tstamp)self->command_timeout_ms / 1000.0;
833 3 ▶ if (ev_now(self->loop) - head->sent_at < budget) {
Branch: 1 branch
BranchTrueFalseTotal
if (ev_now(self->loop) - head->sent_at < budget) { 0 3 3
834 0 arm_cmd_timer(self);
835 0 return;
836 }
837 3 self->callback_depth++;
838 3 handle_disconnect(aTHX_ self, "command timeout");
839 3 self->callback_depth--;
840 3 check_destroyed(self);
841 }
842  
843 0 static void connect_timeout_cb(EV_P_ ev_timer *w, int revents) {
844 dTHX;
845 0 ev_gm_t *self = (ev_gm_t *)w->data;
846 (void)loop; (void)revents;
847 0 ▶ if (self->magic != GM_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) return; 0 0 0
848 0 self->connect_timer_active = 0;
849 0 self->callback_depth++;
850 0 handle_disconnect(aTHX_ self, "connect timeout");
851 0 self->callback_depth--;
852 0 check_destroyed(self);
853 }
854  
855 22 static void reconnect_timer_cb(EV_P_ ev_timer *w, int revents) {
856 dTHX;
857 22 ev_gm_t *self = (ev_gm_t *)w->data;
858 (void)loop; (void)revents;
859 22 ▶ if (self->magic != GM_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) return; 0 22 22
860 22 self->reconnect_timer_active = 0;
861 /* Defense in depth with schedule_reconnect's guard: never run a
862 connect sequence onto a live session. */
863 22 ▶ if (self->connected || self->connecting) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) return; 22 0 22
if (self->connected || self->connecting) return; 0 22 22
864 22 self->callback_depth++;
865 22 start_connect(aTHX_ self);
866 22 self->callback_depth--;
867 22 check_destroyed(self);
868 }
869  
870 /* ================================================================
871 * Packet build/send
872 * ================================================================ */
873  
874 /* Pack a binary packet into a fresh buffer. Caller frees with Safefree. */
875 101 static char* gm_pack(uint32_t cmd, const char *data, size_t data_len, size_t *out_len) {
876 101 size_t total = GM_HEADER_SIZE + data_len;
877 char *p;
878 101 Newx(p, total, char);
879 101 memcpy(p, GM_MAGIC_REQ, 4);
880 101 gm_write_u32(p + 4, cmd);
881 101 gm_write_u32(p + 8, (uint32_t)data_len);
882 101 ▶ if (data_len > 0) memcpy(p + 12, data, data_len);
Branch: 1 branch
BranchTrueFalseTotal
if (data_len > 0) memcpy(p + 12, data, data_len); 65 36 101
883 101 *out_len = total;
884 101 return p;
885 }
886  
887 /* Hand `pkt` (heap-allocated, plen bytes) to the write path. If
888 * already connected, append to the write buffer and Safefree pkt;
889 * otherwise transfer ownership to a new wait entry. If req is
890 * non-NULL its ownership transfers too, into cb_queue or the wait
891 * entry respectively. */
892 106 static void submit_bytes(pTHX_ ev_gm_t *self,
893 char *pkt, size_t plen, ev_gm_req_t *req)
894 {
895 106 ▶ if (self->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) { 94 12 106
896 94 buf_append_write(self, pkt, plen);
897 94 Safefree(pkt);
898 94 ▶ if (req) {
Branch: 1 branch
BranchTrueFalseTotal
if (req) { 54 40 94
899 54 req->sent_at = ev_now(self->loop);
900 54 req->gen = self->conn_gen;
901 54 ngx_queue_insert_tail(&self->cb_queue, &req->queue);
902 54 self->pending_count++;
903 54 arm_cmd_timer(self);
904 }
905 94 start_writing(self);
906 } else {
907 ev_gm_wait_t *w;
908 12 Newxz(w, 1, ev_gm_wait_t);
909 12 w->packet = pkt;
910 12 w->packet_len = plen;
911 12 w->req = req;
912 12 w->gen = self->conn_gen;
913 12 ngx_queue_insert_tail(&self->wait_queue, &w->queue);
914 12 self->waiting_count++;
915 }
916 106 }
917  
918 101 static void enqueue_packet(pTHX_ ev_gm_t *self,
919 uint32_t cmd, const char *data, size_t data_len, ev_gm_req_t *req)
920 {
921 size_t plen;
922 101 char *pkt = gm_pack(cmd, data, data_len, &plen);
923 101 submit_bytes(aTHX_ self, pkt, plen, req);
924 101 }
925  
926 /* The wire length field is 32-bit and the peer caps reads at
927 GM_MAX_PACKET; reject anything larger before it would truncate and
928 desync the protocol. Callers validate at the point of entry (before
929 allocating request state) so a croak here never leaks. */
930 37 static void gm_check_size(pTHX_ size_t len) {
931 37 ▶ if (len > GM_MAX_PACKET)
Branch: 1 branch
BranchTrueFalseTotal
if (len > GM_MAX_PACKET) 2 35 37
932 2 croak("EV::Gearman: payload too large (%zu bytes, max %u)",
933 len, (unsigned)GM_MAX_PACKET);
934 35 }
935  
936 /* Non-final packet fields are NUL-separated: a NUL inside one would
937 silently reframe the packet server-side. The registry also stores
938 names NUL-truncated while the wire sends full bytes. */
939 47 static void gm_check_no_nul(pTHX_ const char *p, STRLEN len, const char *what) {
940 47 ▶ if (p && len > 0 && memchr(p, '\0', len))
Branch: 3 branches
BranchTrueFalseTotal
if (p && len > 0 && memchr(p, '\0', len)) 47 0 47
if (p && len > 0 && memchr(p, '\0', len)) 39 8 47
if (p && len > 0 && memchr(p, '\0', len)) 8 31 39
941 8 croak("EV::Gearman: %s may not contain NUL bytes", what);
942 39 }
943  
944 53 static void send_pending_waits(pTHX_ ev_gm_t *self) {
945 59 ▶ while (!ngx_queue_empty(&self->wait_queue) && self->connected) {
Branch: 2 branches
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue) && self->connected) { 6 53 59
while (!ngx_queue_empty(&self->wait_queue) && self->connected) { 6 0 6
946 6 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
947 6 ev_gm_wait_t *w = ngx_queue_data(q, ev_gm_wait_t, queue);
948 6 ngx_queue_remove(q);
949 6 self->waiting_count--;
950  
951 6 buf_append_write(self, w->packet, w->packet_len);
952  
953 6 ▶ if (w->req) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->req) { 6 0 6
954 6 w->req->sent_at = ev_now(self->loop);
955 6 w->req->gen = self->conn_gen;
956 6 ngx_queue_insert_tail(&self->cb_queue, &w->req->queue);
957 6 self->pending_count++;
958 6 w->req = NULL;
959 6 arm_cmd_timer(self);
960 }
961  
962 6 Safefree(w->packet); w->packet = NULL;
963 6 Safefree(w);
964  
965 6 start_writing(self);
966 }
967 53 }
968  
969 /* ================================================================
970 * Worker helpers (state machine for GRAB / NOOP / PRE_SLEEP)
971 * ================================================================ */
972  
973 27 static ev_gm_func_t* find_function(ev_gm_t *self, const char *name, STRLEN len) {
974 ngx_queue_t *q;
975 30 ▶ for (q = ngx_queue_head(&self->functions); q != ngx_queue_sentinel(&self->functions);
Branch: 1 branch
BranchTrueFalseTotal
for (q = ngx_queue_head(&self->functions); q != ngx_queue_sentinel(&self->functions); 16 14 30
976 3 q = ngx_queue_next(q))
977 {
978 16 ev_gm_func_t *f = ngx_queue_data(q, ev_gm_func_t, queue);
979 16 ▶ if (strlen(f->name) == len && memcmp(f->name, name, len) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(f->name) == len && memcmp(f->name, name, len) == 0) 16 0 16
if (strlen(f->name) == len && memcmp(f->name, name, len) == 0) 13 3 16
980 13 return f;
981 }
982 14 return NULL;
983 }
984  
985 25 static void worker_send_grab(pTHX_ ev_gm_t *self) {
986 25 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 25 25
987 25 ▶ if (self->worker_grab_inflight) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_grab_inflight) return; 0 25 25
988 25 ▶ if (ngx_queue_empty(&self->functions)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->functions)) return; 0 25 25
989 25 self->worker_grab_inflight = 1;
990 25 ev_gm_req_t *r = alloc_req(CB_GRAB_JOB, NULL);
991 25 enqueue_packet(aTHX_ self,
992 25 ▶ self->worker_grab_uniq ? GM_CMD_GRAB_JOB_UNIQ : GM_CMD_GRAB_JOB,
Branch: 1 branch
BranchTrueFalseTotal
self->worker_grab_uniq ? GM_CMD_GRAB_JOB_UNIQ : GM_CMD_GRAB_JOB, 0 25 25
993 NULL, 0, r);
994 }
995  
996 9 static void worker_send_pre_sleep(pTHX_ ev_gm_t *self) {
997 9 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 9 9
998 9 ▶ if (self->worker_sleeping) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_sleeping) return; 0 9 9
999 9 self->worker_sleeping = 1;
1000 9 enqueue_packet(aTHX_ self, GM_CMD_PRE_SLEEP, NULL, 0, NULL);
1001 }
1002  
1003 /* Drive the worker loop forward: if active and not busy, GRAB. */
1004 86 static void worker_continue(pTHX_ ev_gm_t *self) {
1005 86 ▶ if (!self->worker_active || !self->connected) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self->worker_active || !self->connected) return; 29 57 86
if (!self->worker_active || !self->connected) return; 4 25 29
1006 25 ▶ if (self->worker_grab_inflight || self->worker_sleeping) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->worker_grab_inflight || self->worker_sleeping) return; 25 0 25
if (self->worker_grab_inflight || self->worker_sleeping) return; 0 25 25
1007 25 ▶ if (ngx_queue_empty(&self->functions)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->functions)) return; 0 25 25
1008 25 worker_send_grab(aTHX_ self);
1009 }
1010  
1011 /* ================================================================
1012 * Connect-success path: re-register worker functions, drain queue
1013 * ================================================================ */
1014  
1015 /* Send a WORK_* packet whose body is "handle" + optional "\0data".
1016 For WORK_FAIL pass dp=NULL; otherwise pass payload bytes. */
1017 11 static void send_work_event(pTHX_ ev_gm_t *self, uint32_t cmd,
1018 const char *h, STRLEN hl, const char *dp, STRLEN dl)
1019 {
1020 /* No size croak here: this runs on the worker completion / event
1021 path (worker_dispatch_job, $job->complete), where a longjmp would
1022 unwind the EV loop mid-iteration. A >4 GiB result is not a real
1023 concern (the worker would OOM building it first). */
1024 11 ▶ size_t plen = dp ? hl + 1 + dl : hl;
Branch: 1 branch
BranchTrueFalseTotal
size_t plen = dp ? hl + 1 + dl : hl; 10 1 11
1025 char *body;
1026 11 ▶ Newx(body, plen ? plen : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(body, plen ? plen : 1, char); 11 0 11
1027 11 memcpy(body, h, hl);
1028 11 ▶ if (dp) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp) { 10 1 11
1029 10 body[hl] = '\0';
1030 10 ▶ if (dl) memcpy(body + hl + 1, dp, dl);
Branch: 1 branch
BranchTrueFalseTotal
if (dl) memcpy(body + hl + 1, dp, dl); 10 0 10
1031 }
1032 11 enqueue_packet(aTHX_ self, cmd, body, plen, NULL);
1033 11 Safefree(body);
1034 11 }
1035  
1036 /* Send CAN_DO[_TIMEOUT] for one function name. Builds the
1037 "name\0timeout" body on the heap to avoid the 512-byte stack-buffer
1038 truncation that the older inline pattern was prone to. */
1039 22 static void send_can_do(pTHX_ ev_gm_t *self,
1040 const char *name, STRLEN nlen, int timeout)
1041 {
1042 22 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 5 17 22
1043 17 ▶ if (timeout > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (timeout > 0) { 0 17 17
1044 char tbuf[16];
1045 0 int tlen = snprintf(tbuf, sizeof(tbuf), "%d", timeout);
1046 0 ▶ if (tlen <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (tlen <= 0) return; 0 0 0
1047 0 size_t plen = nlen + 1 + (size_t)tlen;
1048 char *body;
1049 0 Newx(body, plen, char);
1050 0 memcpy(body, name, nlen);
1051 0 body[nlen] = '\0';
1052 0 memcpy(body + nlen + 1, tbuf, tlen);
1053 0 enqueue_packet(aTHX_ self, GM_CMD_CAN_DO_TIMEOUT, body, plen, NULL);
1054 0 Safefree(body);
1055 } else {
1056 17 enqueue_packet(aTHX_ self, GM_CMD_CAN_DO, name, nlen, NULL);
1057 }
1058 }
1059  
1060 53 static void register_all_functions(pTHX_ ev_gm_t *self) {
1061 53 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 53 53
1062 ngx_queue_t *q;
1063 61 ▶ for (q = ngx_queue_head(&self->functions); q != ngx_queue_sentinel(&self->functions);
Branch: 1 branch
BranchTrueFalseTotal
for (q = ngx_queue_head(&self->functions); q != ngx_queue_sentinel(&self->functions); 8 53 61
1064 8 q = ngx_queue_next(q))
1065 {
1066 8 ev_gm_func_t *f = ngx_queue_data(q, ev_gm_func_t, queue);
1067 8 send_can_do(aTHX_ self, f->name, strlen(f->name), f->timeout);
1068 }
1069 }
1070  
1071 53 static void send_options_and_id(pTHX_ ev_gm_t *self) {
1072 53 ▶ if (self->client_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->client_id) { 0 53 53
1073 0 enqueue_packet(aTHX_ self, GM_CMD_SET_CLIENT_ID,
1074 0 self->client_id, strlen(self->client_id), NULL);
1075 }
1076 53 ▶ if (self->opt_exceptions) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->opt_exceptions) { 0 53 53
1077 0 const char *opt = "exceptions";
1078 0 ev_gm_req_t *r = alloc_req(CB_OPTION, NULL);
1079 0 enqueue_packet(aTHX_ self, GM_CMD_OPTION_REQ, opt, strlen(opt), r);
1080 }
1081 53 }
1082  
1083 28 static void report_connect_error(pTHX_ ev_gm_t *self, const char *errbuf) {
1084 28 self->callback_depth++;
1085 28 ▶ if (!self->intentional_disconnect && self->reconnect)
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->reconnect) 28 0 28
if (!self->intentional_disconnect && self->reconnect) 25 3 28
1086 25 schedule_reconnect(aTHX_ self);
1087 28 ▶ if (self->magic == GM_MAGIC_ALIVE)
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_ALIVE) 28 0 28
1088 28 emit_error(aTHX_ self, errbuf);
1089 28 ▶ if (self->magic == GM_MAGIC_ALIVE && !self->connected &&
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_ALIVE && !self->connected && 28 0 28
if (self->magic == GM_MAGIC_ALIVE && !self->connected && 27 1 28
1090 27 ▶ !self->connecting && !self->reconnect_timer_active) {
Branch: 2 branches
BranchTrueFalseTotal
!self->connecting && !self->reconnect_timer_active) { 27 0 27
!self->connecting && !self->reconnect_timer_active) { 5 22 27
1091 5 self->conn_gen++;
1092 5 cancel_waiting(aTHX_ self, "disconnected");
1093 }
1094 28 self->callback_depth--;
1095 28 check_destroyed(self);
1096 28 }
1097  
1098 53 static void finish_connect_success(pTHX_ ev_gm_t *self) {
1099 53 self->reconnect_attempts = 0;
1100  
1101 53 start_reading(self);
1102 53 ▶ if (self->keepalive > 0) apply_keepalive(self);
Branch: 1 branch
BranchTrueFalseTotal
if (self->keepalive > 0) apply_keepalive(self); 0 53 53
1103  
1104 53 send_options_and_id(aTHX_ self);
1105 53 register_all_functions(aTHX_ self);
1106  
1107 53 emit_connect(aTHX_ self);
1108 53 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 53 53
1109  
1110 53 send_pending_waits(aTHX_ self);
1111 53 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 53 53
1112  
1113 53 worker_continue(aTHX_ self);
1114 }
1115  
1116 /* ================================================================
1117 * Response routing
1118 * ================================================================ */
1119  
1120 /* Return pointer to first byte of the i-th NUL-separated argument
1121 * (zero-indexed) and its length in *out_len. Returns NULL if not
1122 * present. The final argument is whatever remains after the last
1123 * NUL — it may itself contain NUL bytes for binary data. */
1124 47 static const char* gm_arg(const char *body, size_t body_len, int idx,
1125 int total, size_t *out_len)
1126 {
1127 /* Walk through, splitting by NUL. The (total-1) NULs separate
1128 total fields; the final field has no trailing NUL. */
1129 47 const char *p = body;
1130 47 const char *end = body + body_len;
1131 47 int cur = 0;
1132 90 ▶ while (cur < idx) {
Branch: 1 branch
BranchTrueFalseTotal
while (cur < idx) { 43 47 90
1133 43 const char *nul = memchr(p, '\0', end - p);
1134 43 ▶ if (!nul) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!nul) return NULL; 0 43 43
1135 43 p = nul + 1;
1136 43 cur++;
1137 }
1138 /* p is start of arg `idx`; find its length */
1139 47 ▶ if (idx == total - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx == total - 1) { 17 30 47
1140 /* Last arg runs to end */
1141 17 *out_len = end - p;
1142 } else {
1143 30 const char *nul = memchr(p, '\0', end - p);
1144 30 ▶ if (!nul) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!nul) return NULL; 0 30 30
1145 30 *out_len = nul - p;
1146 }
1147 47 return p;
1148 }
1149  
1150 0 static ev_gm_active_t* find_active_by_handle(ev_gm_t *self,
1151 const char *handle, STRLEN handle_len)
1152 {
1153 ngx_queue_t *q;
1154 0 ▶ for (q = ngx_queue_head(&self->active_jobs); q != ngx_queue_sentinel(&self->active_jobs);
Branch: 1 branch
BranchTrueFalseTotal
for (q = ngx_queue_head(&self->active_jobs); q != ngx_queue_sentinel(&self->active_jobs); 0 0 0
1155 0 q = ngx_queue_next(q))
1156 {
1157 0 ev_gm_active_t *a = ngx_queue_data(q, ev_gm_active_t, queue);
1158 0 ▶ if (a->handle_len == handle_len &&
Branch: 1 branch
BranchTrueFalseTotal
if (a->handle_len == handle_len && 0 0 0
1159 0 ▶ memcmp(a->handle, handle, handle_len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
memcmp(a->handle, handle, handle_len) == 0) 0 0 0
1160 0 return a;
1161 }
1162 0 return NULL;
1163 }
1164  
1165 /* Pop the head request entry; returns NULL if queue empty. Popping
1166 advances the head, so the per-request command timer is re-armed for
1167 the new head's budget (or disarmed when nothing remains). */
1168 43 static ev_gm_req_t* pop_req(ev_gm_t *self) {
1169 43 ▶ if (ngx_queue_empty(&self->cb_queue)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) return NULL; 0 43 43
1170 43 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
1171 43 ev_gm_req_t *r = ngx_queue_data(q, ev_gm_req_t, queue);
1172 43 ngx_queue_remove(q);
1173 43 self->pending_count--;
1174 43 ▶ if (ngx_queue_empty(&self->cb_queue)) disarm_cmd_timer(self);
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) disarm_cmd_timer(self); 42 1 43
1175 1 else arm_cmd_timer(self);
1176 43 return r;
1177 }
1178  
1179 /* ---- Response/request pairing -------------------------------------
1180 * gearmand answers the request/response commands synchronously in
1181 * wire order, so a reply whose command cannot answer the head request
1182 * means a broken or hostile peer — or a FIFO already desynced. With no
1183 * request-ids in the protocol there is no resynchronization primitive;
1184 * continuing would deliver (possibly attacker-chosen) data to the
1185 * wrong callback, so a mismatch tears the connection down. NOOP and
1186 * ERROR are excepted: a correct gearmand may emit both asynchronously.
1187 */
1188  
1189 4 static const char* gm_cmd_name(uint32_t cmd) {
1190 4 switch (cmd) {
1191 1 case GM_CMD_JOB_CREATED: return "JOB_CREATED";
1192 0 case GM_CMD_NO_JOB: return "NO_JOB";
1193 0 case GM_CMD_JOB_ASSIGN: return "JOB_ASSIGN";
1194 0 case GM_CMD_WORK_STATUS: return "WORK_STATUS";
1195 0 case GM_CMD_WORK_COMPLETE: return "WORK_COMPLETE";
1196 0 case GM_CMD_WORK_FAIL: return "WORK_FAIL";
1197 3 case GM_CMD_ECHO_RES: return "ECHO_RES";
1198 0 case GM_CMD_ERROR: return "ERROR";
1199 0 case GM_CMD_STATUS_RES: return "STATUS_RES";
1200 0 case GM_CMD_ALL_YOURS: return "ALL_YOURS";
1201 0 case GM_CMD_WORK_EXCEPTION: return "WORK_EXCEPTION";
1202 0 case GM_CMD_OPTION_RES: return "OPTION_RES";
1203 0 case GM_CMD_WORK_DATA: return "WORK_DATA";
1204 0 case GM_CMD_WORK_WARNING: return "WORK_WARNING";
1205 0 case GM_CMD_JOB_ASSIGN_UNIQ: return "JOB_ASSIGN_UNIQ";
1206 0 case GM_CMD_STATUS_RES_UNIQUE: return "STATUS_RES_UNIQUE";
1207 0 case GM_CMD_NOOP: return "NOOP";
1208 0 default: return "UNKNOWN_CMD";
1209 }
1210 }
1211  
1212 4 static const char* gm_kind_name(int kind) {
1213 4 switch (kind) {
1214 1 case CB_ECHO: return "ECHO";
1215 2 case CB_SUBMIT: return "SUBMIT";
1216 0 case CB_SUBMIT_BG: return "SUBMIT_BG";
1217 0 case CB_GET_STATUS: return "GET_STATUS";
1218 0 case CB_GET_STATUS_UNIQ: return "GET_STATUS_UNIQUE";
1219 0 case CB_OPTION: return "OPTION";
1220 1 case CB_GRAB_JOB: return "GRAB_JOB";
1221 0 case CB_ADMIN: return "ADMIN";
1222 0 default: return "UNKNOWN_KIND";
1223 }
1224 }
1225  
1226 #define GM_KIND_MASK(k) (1u << ((k) - 1)) /* CB_* are 1..8 */
1227  
1228 /* Pop the head request iff its kind can legitimately be answered by
1229 `cmd`. Empty queue: return NULL (caller decides tolerance). Kind
1230 mismatch: the FIFO is desynced — tear the connection down with a
1231 protocol error and return NULL. Callers must then touch nothing
1232 further on self; process_responses re-checks magic/connected after
1233 every arm. */
1234 46 static ev_gm_req_t* pop_req_expect(pTHX_ ev_gm_t *self, uint32_t cmd,
1235 unsigned kind_mask)
1236 {
1237 46 ▶ if (ngx_queue_empty(&self->cb_queue)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) return NULL; 0 46 46
1238 46 ev_gm_req_t *head = ngx_queue_data(ngx_queue_head(&self->cb_queue),
1239 ev_gm_req_t, queue);
1240 46 ▶ if (!(kind_mask & GM_KIND_MASK(head->kind))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(kind_mask & GM_KIND_MASK(head->kind))) { 4 42 46
1241 char msg[128];
1242 4 snprintf(msg, sizeof(msg),
1243 "protocol desync: %s cannot answer %s request",
1244 gm_cmd_name(cmd), gm_kind_name(head->kind));
1245 4 handle_disconnect(aTHX_ self, msg);
1246 4 return NULL;
1247 }
1248 42 return pop_req(self);
1249 }
1250  
1251 /* Forward declare so handle_response_packet can call into worker job dispatch */
1252 static void worker_dispatch_job(pTHX_ ev_gm_t *self, ev_gm_req_t *r,
1253 int with_unique, const char *body, size_t body_len);
1254  
1255 /* Release the tombstone reference a job holds on its client. Runs
1256 exactly once when the job HV is freed (magic lives on the referent,
1257 not the RV, so re-blessing cannot shake it and Perl code cannot
1258 remove it). The struct is still allocated here by the job_refs
1259 invariant: freed only when refs hit 0. */
1260 13 static int ev_gm_job_magic_free(pTHX_ SV *sv, MAGIC *mg) {
1261 13 ev_gm_t *self = (ev_gm_t *)mg->mg_ptr;
1262 (void)sv;
1263 13 mg->mg_ptr = NULL;
1264 13 ▶ if (self) {
Branch: 1 branch
BranchTrueFalseTotal
if (self) { 13 0 13
1265 13 ▶ if (self->job_refs > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->job_refs > 0) 13 0 13
1266 13 self->job_refs--;
1267 13 (void)check_destroyed(self);
1268 }
1269 13 return 0;
1270 }
1271  
1272 static const MGVTBL ev_gm_job_magic_vtbl = {
1273 NULL, NULL, NULL, NULL, ev_gm_job_magic_free /* svt_free only */
1274 };
1275  
1276 /* Validate a job hashref and return its (live) client + handle bytes.
1277 * Croaks with op-prefixed messages on any inconsistency. The client
1278 * pointer arrives as PERL_MAGIC_ext on the job HV (attached by
1279 * worker_dispatch_job), not as a hash key: unforgeable and
1280 * un-clobberable from Perl, and the job_refs tombstone count keeps the
1281 * struct allocated so the magic-word check below never reads freed
1282 * memory. */
1283 14 static ev_gm_t* job_resolve(pTHX_ SV *job_sv, const char **h, STRLEN *hl,
1284 const char *op)
1285 {
1286 14 ▶ if (!SvROK(job_sv)) croak("%s: invalid job", op);
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(job_sv)) croak("%s: invalid job", op); 0 14 14
1287 14 SV *ref = SvRV(job_sv);
1288 14 ▶ if (SvTYPE(ref) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) != SVt_PVHV) 0 14 14
1289 0 croak("%s: stale job (not a job hash)", op);
1290 14 MAGIC *mg = mg_findext(ref, PERL_MAGIC_ext, &ev_gm_job_magic_vtbl);
1291 14 ▶ if (!mg || !mg->mg_ptr)
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr) 14 0 14
if (!mg || !mg->mg_ptr) 0 14 14
1292 0 croak("%s: stale job (no client pointer)", op);
1293 14 SV **handle_sv = hv_fetchs((HV *)ref, "handle", 0);
1294 14 ▶ if (!handle_sv) croak("%s: missing handle", op);
Branch: 1 branch
BranchTrueFalseTotal
if (!handle_sv) croak("%s: missing handle", op); 0 14 14
1295 14 ev_gm_t *self = (ev_gm_t *)mg->mg_ptr;
1296 14 ▶ if (self->magic != GM_MAGIC_ALIVE)
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) 0 14 14
1297 0 croak("%s: client destroyed", op);
1298 /* A live-but-disconnected client would silently queue this WORK_*
1299 packet into the pre-connect wait queue and flush it after a
1300 reconnect — where gearmand, which forgot the job at the drop,
1301 answers JOB_NOT_FOUND. Croak instead: the job is already dead
1302 server-side, so queuing can only produce that error. */
1303 14 ▶ if (!self->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) 0 14 14
1304 0 croak("%s: not connected", op);
1305 14 ▶ if (mg->mg_obj != self->job_session)
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_obj != self->job_session) 6 8 14
1306 6 croak("%s: stale job (previous connection)", op);
1307 8 *h = SvPV(*handle_sv, *hl);
1308 8 return self;
1309 }
1310  
1311 59 static void handle_response_packet(pTHX_ ev_gm_t *self,
1312 uint32_t cmd, const char *body, size_t body_len)
1313 {
1314 59 switch (cmd) {
1315  
1316 17 case GM_CMD_ECHO_RES: {
1317 17 ev_gm_req_t *r = pop_req_expect(aTHX_ self, cmd,
1318 GM_KIND_MASK(CB_ECHO));
1319 17 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 3 14 17
1320 14 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 14 0 14
1321 14 invoke_cb2(aTHX_ self, r->cb, newSVpvn(body, body_len), NULL);
1322 14 cleanup_req(aTHX_ r);
1323 14 break;
1324 }
1325  
1326 6 case GM_CMD_JOB_CREATED: {
1327 /* Only a SUBMIT request may sit at the head; anything else is
1328 a desync (pop_req_expect disconnects). The old silent-free
1329 else branch is gone: it dropped the user's callback without
1330 an error, and with pending_count at 0 not even the command
1331 timer could fire — a permanent silent hang. */
1332 6 ev_gm_req_t *r = pop_req_expect(aTHX_ self, cmd,
1333 GM_KIND_MASK(CB_SUBMIT) | GM_KIND_MASK(CB_SUBMIT_BG));
1334 6 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 1 5 6
1335 5 SV *handle_sv = newSVpvn(body, body_len);
1336 5 ▶ if (r->kind == CB_SUBMIT_BG) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->kind == CB_SUBMIT_BG) { 4 1 5
1337 4 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 4 0 4
1338 4 invoke_cb2(aTHX_ self, r->cb, handle_sv, NULL);
1339 else
1340 0 SvREFCNT_dec(handle_sv);
1341 4 cleanup_req(aTHX_ r);
1342 } else {
1343 /* Foreground: register active job, transfer callback bundle */
1344 ev_gm_active_t *a;
1345 1 Newxz(a, 1, ev_gm_active_t);
1346 1 Newx(a->handle, body_len, char);
1347 1 memcpy(a->handle, body, body_len);
1348 1 a->handle_len = body_len;
1349 1 a->on_complete = r->cb; r->cb = NULL;
1350 1 a->on_data = r->on_data; r->on_data = NULL;
1351 1 a->on_warning = r->on_warning; r->on_warning = NULL;
1352 1 a->on_status = r->on_status; r->on_status = NULL;
1353 1 a->on_exception = r->on_exception;r->on_exception = NULL;
1354 1 ngx_queue_insert_tail(&self->active_jobs, &a->queue);
1355 1 self->active_count_cached++;
1356 1 cleanup_req(aTHX_ r);
1357 1 SvREFCNT_dec(handle_sv);
1358 }
1359 5 break;
1360 }
1361  
1362 0 case GM_CMD_WORK_COMPLETE: {
1363 size_t hl, dl;
1364 0 const char *h = gm_arg(body, body_len, 0, 2, &hl);
1365 0 const char *d = gm_arg(body, body_len, 1, 2, &dl);
1366 0 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 0 0
1367 0 ev_gm_active_t *a = find_active_by_handle(self, h, hl);
1368 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1369 0 ngx_queue_remove(&a->queue);
1370 0 self->active_count_cached--;
1371 0 ▶ if (a->on_complete)
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_complete) 0 0 0
1372 0 ▶ invoke_cb2(aTHX_ self, a->on_complete,
Branch: 1 branch
BranchTrueFalseTotal
invoke_cb2(aTHX_ self, a->on_complete, 0 0 0
1373 0 d ? newSVpvn(d, dl) : newSVpvs(""), NULL);
1374 0 cleanup_active(aTHX_ a);
1375 0 break;
1376 }
1377  
1378 0 case GM_CMD_WORK_FAIL: {
1379 0 ev_gm_active_t *a = find_active_by_handle(self, body, body_len);
1380 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1381 0 ngx_queue_remove(&a->queue);
1382 0 self->active_count_cached--;
1383 0 ▶ if (a->on_complete)
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_complete) 0 0 0
1384 0 invoke_cb2(aTHX_ self, a->on_complete, NULL, newSVpvs("job failed"));
1385 0 cleanup_active(aTHX_ a);
1386 0 break;
1387 }
1388  
1389 0 case GM_CMD_WORK_DATA:
1390 case GM_CMD_WORK_WARNING: {
1391 size_t hl, dl;
1392 0 const char *h = gm_arg(body, body_len, 0, 2, &hl);
1393 0 const char *d = gm_arg(body, body_len, 1, 2, &dl);
1394 0 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 0 0
1395 0 ev_gm_active_t *a = find_active_by_handle(self, h, hl);
1396 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1397 0 ▶ SV *cb = (cmd == GM_CMD_WORK_DATA) ? a->on_data : a->on_warning;
Branch: 1 branch
BranchTrueFalseTotal
SV *cb = (cmd == GM_CMD_WORK_DATA) ? a->on_data : a->on_warning; 0 0 0
1398 0 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 0 0 0
1399 0 ▶ SV *arg = d ? newSVpvn(d, dl) : newSVpvn("", 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *arg = d ? newSVpvn(d, dl) : newSVpvn("", 0); 0 0 0
1400 0 invoke_handler(aTHX_ self, cb, arg, "on_data/on_warning");
1401 }
1402 0 break;
1403 }
1404  
1405 0 case GM_CMD_WORK_EXCEPTION: {
1406 /* gearmand calls fail() on the job after forwarding the
1407 exception data and does NOT emit a follow-up WORK_FAIL,
1408 so this packet is the terminal event for the active job:
1409 fire on_exception (informational) then on_complete with
1410 an "exception" error so the caller's terminal callback
1411 always runs. */
1412 size_t hl, dl;
1413 0 const char *h = gm_arg(body, body_len, 0, 2, &hl);
1414 0 const char *d = gm_arg(body, body_len, 1, 2, &dl);
1415 0 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 0 0
1416 0 ev_gm_active_t *a = find_active_by_handle(self, h, hl);
1417 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1418 0 ngx_queue_remove(&a->queue);
1419 0 self->active_count_cached--;
1420 0 ▶ if (a->on_exception) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_exception) { 0 0 0
1421 0 ▶ SV *arg = d ? newSVpvn(d, dl) : newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
SV *arg = d ? newSVpvn(d, dl) : newSVpvs(""); 0 0 0
1422 0 invoke_handler(aTHX_ self, a->on_exception, arg, "on_exception");
1423 }
1424 0 ▶ if (self->magic != GM_MAGIC_FREED && a->on_complete)
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_FREED && a->on_complete) 0 0 0
if (self->magic != GM_MAGIC_FREED && a->on_complete) 0 0 0
1425 0 invoke_cb2(aTHX_ self, a->on_complete, NULL, newSVpvs("exception"));
1426 0 cleanup_active(aTHX_ a);
1427 0 break;
1428 }
1429  
1430 0 case GM_CMD_WORK_STATUS: {
1431 size_t hl, nl, dl;
1432 0 const char *h = gm_arg(body, body_len, 0, 3, &hl);
1433 0 const char *n = gm_arg(body, body_len, 1, 3, &nl);
1434 0 const char *d = gm_arg(body, body_len, 2, 3, &dl);
1435 0 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 0 0
1436 0 ev_gm_active_t *a = find_active_by_handle(self, h, hl);
1437 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1438 0 ▶ if (a->on_status)
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_status) 0 0 0
1439 0 ▶ invoke_cb2(aTHX_ self, a->on_status,
Branch: 2 branches
BranchTrueFalseTotal
invoke_cb2(aTHX_ self, a->on_status, 0 0 0
invoke_cb2(aTHX_ self, a->on_status, 0 0 0
1440 0 n ? newSVpvn(n, nl) : newSVpvs("0"),
1441 0 d ? newSVpvn(d, dl) : newSVpvs("0"));
1442 0 break;
1443 }
1444  
1445 0 case GM_CMD_STATUS_RES:
1446 case GM_CMD_STATUS_RES_UNIQUE: {
1447 /* Both arms decode HANDLE-or-UNIQUE then KNOWN, RUNNING, NUM,
1448 DENOM, with STATUS_RES_UNIQUE adding a final CLIENT_COUNT. */
1449 0 ▶ ev_gm_req_t *r = pop_req_expect(aTHX_ self, cmd,
Branch: 1 branch
BranchTrueFalseTotal
ev_gm_req_t *r = pop_req_expect(aTHX_ self, cmd, 0 0 0
1450 cmd == GM_CMD_STATUS_RES ? GM_KIND_MASK(CB_GET_STATUS)
1451 : GM_KIND_MASK(CB_GET_STATUS_UNIQ));
1452 0 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 0 0
1453 0 ▶ if (r->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) { 0 0 0
1454 0 int with_count = (cmd == GM_CMD_STATUS_RES_UNIQUE);
1455 0 ▶ int total = with_count ? 6 : 5;
Branch: 1 branch
BranchTrueFalseTotal
int total = with_count ? 6 : 5; 0 0 0
1456 size_t l[6];
1457 const char *p[6];
1458 int i;
1459 0 ▶ for (i = 0; i < total; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < total; i++) 0 0 0
1460 0 p[i] = gm_arg(body, body_len, i, total, &l[i]);
1461 0 HV *hv = newHV();
1462 0 ▶ if (p[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (p[0]) { 0 0 0
1463 0 ▶ if (with_count) hv_stores(hv, "unique", newSVpvn(p[0], l[0]));
Branch: 1 branch
BranchTrueFalseTotal
if (with_count) hv_stores(hv, "unique", newSVpvn(p[0], l[0])); 0 0 0
1464 0 else hv_stores(hv, "handle", newSVpvn(p[0], l[0]));
1465 }
1466 0 ▶ if (p[1]) hv_stores(hv, "known", newSViv(gm_atoi_n(p[1], l[1])));
Branch: 1 branch
BranchTrueFalseTotal
if (p[1]) hv_stores(hv, "known", newSViv(gm_atoi_n(p[1], l[1]))); 0 0 0
1467 0 ▶ if (p[2]) hv_stores(hv, "running", newSViv(gm_atoi_n(p[2], l[2])));
Branch: 1 branch
BranchTrueFalseTotal
if (p[2]) hv_stores(hv, "running", newSViv(gm_atoi_n(p[2], l[2]))); 0 0 0
1468 0 ▶ if (p[3]) hv_stores(hv, "numerator", newSVpvn(p[3], l[3]));
Branch: 1 branch
BranchTrueFalseTotal
if (p[3]) hv_stores(hv, "numerator", newSVpvn(p[3], l[3])); 0 0 0
1469 0 ▶ if (p[4]) hv_stores(hv, "denominator", newSVpvn(p[4], l[4]));
Branch: 1 branch
BranchTrueFalseTotal
if (p[4]) hv_stores(hv, "denominator", newSVpvn(p[4], l[4])); 0 0 0
1470 0 ▶ if (with_count && p[5])
Branch: 2 branches
BranchTrueFalseTotal
if (with_count && p[5]) 0 0 0
if (with_count && p[5]) 0 0 0
1471 0 hv_stores(hv, "client_count", newSViv(gm_atoi_n(p[5], l[5])));
1472 0 invoke_cb2(aTHX_ self, r->cb, newRV_noinc((SV*)hv), NULL);
1473 }
1474 0 cleanup_req(aTHX_ r);
1475 0 break;
1476 }
1477  
1478 0 case GM_CMD_OPTION_RES: {
1479 0 ev_gm_req_t *r = pop_req_expect(aTHX_ self, cmd,
1480 GM_KIND_MASK(CB_OPTION));
1481 0 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 0 0
1482 0 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 0 0 0
1483 0 invoke_cb2(aTHX_ self, r->cb, newSViv(1), NULL);
1484 0 cleanup_req(aTHX_ r);
1485 0 break;
1486 }
1487  
1488 10 case GM_CMD_NO_JOB: {
1489 10 unsigned int gen = self->conn_gen;
1490 /* The grab round is over no matter what the queue looks like —
1491 clearing this FIRST is load-bearing: an early return with the
1492 flag stuck wedges the worker permanently (worker_continue
1493 gates on it). */
1494 10 self->worker_grab_inflight = 0;
1495 10 ev_gm_req_t *r = pop_req_expect(aTHX_ self, cmd,
1496 GM_KIND_MASK(CB_GRAB_JOB));
1497 10 ▶ if (!self->connected || self->conn_gen != gen) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self->connected || self->conn_gen != gen) return; 10 0 10
if (!self->connected || self->conn_gen != gen) return; 0 10 10
1498 10 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 10 0 10
1499 /* grab_job's caller registered a cb; fire it with "no job"
1500 so it knows to back off. The high-level work() loop never
1501 sets r->cb for its internal grabs, so this is a no-op
1502 there. */
1503 10 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 1 9 10
1504 1 invoke_cb2(aTHX_ self, r->cb, NULL, newSVpvs("no job"));
1505 10 cleanup_req(aTHX_ r);
1506 10 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 10 10
1507 10 ▶ if (!self->connected || self->conn_gen != gen) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self->connected || self->conn_gen != gen) return; 10 0 10
if (!self->connected || self->conn_gen != gen) return; 1 9 10
1508 }
1509 /* else: empty queue — tolerate. NO_JOB carries no information,
1510 nothing remains to misroute, and the flag is already clear. */
1511 9 ▶ if (self->worker_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_active) { 9 0 9
1512 9 worker_send_pre_sleep(aTHX_ self);
1513 9 ▶ if (self->worker_on_idle)
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_on_idle) 7 2 9
1514 7 invoke_handler(aTHX_ self, self->worker_on_idle, NULL, "on_idle");
1515 }
1516 9 break;
1517 }
1518  
1519 13 case GM_CMD_JOB_ASSIGN:
1520 case GM_CMD_JOB_ASSIGN_UNIQ: {
1521 /* First statement, same load-bearing reason as NO_JOB. */
1522 13 self->worker_grab_inflight = 0;
1523 13 ▶ if (ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) { 0 13 13
1524 /* A correct server only assigns in reply to a queued GRAB;
1525 an unsolicited assign carries real work we cannot
1526 attribute (and would strand server-side). Desync. */
1527 0 handle_disconnect(aTHX_ self,
1528 "protocol desync: unsolicited JOB_ASSIGN");
1529 0 return;
1530 }
1531 13 ev_gm_req_t *r = pop_req_expect(aTHX_ self, cmd,
1532 GM_KIND_MASK(CB_GRAB_JOB));
1533 13 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 13 13
1534 13 worker_dispatch_job(aTHX_ self, r,
1535 cmd == GM_CMD_JOB_ASSIGN_UNIQ ? 1 : 0, body, body_len);
1536 13 cleanup_req(aTHX_ r);
1537 13 break;
1538 }
1539  
1540 9 case GM_CMD_NOOP: {
1541 9 self->worker_sleeping = 0;
1542 9 worker_continue(aTHX_ self);
1543 9 break;
1544 }
1545  
1546 4 case GM_CMD_ERROR: {
1547 /* ERRCODE\0ERR_TEXT. JOB_NOT_FOUND answers a WORK_* packet,
1548 which has no queued request; even with a client request at
1549 the head it is a connection-level error. GRAB and ADMIN
1550 likewise cannot be answered by ERROR. Other errors reject
1551 the head request in wire order. */
1552 size_t cl, tl;
1553 4 const char *c = gm_arg(body, body_len, 0, 2, &cl);
1554 4 const char *t = gm_arg(body, body_len, 1, 2, &tl);
1555 SV *err;
1556 4 ▶ if (c && t)
Branch: 2 branches
BranchTrueFalseTotal
if (c && t) 4 0 4
if (c && t) 4 0 4
1557 4 err = newSVpvf("%.*s: %.*s", (int)cl, c, (int)tl, t);
1558 else
1559 0 err = newSVpvn(body, body_len);
1560 8 ev_gm_req_t *head = ngx_queue_empty(&self->cb_queue) ? NULL
1561 4 ▶ : ngx_queue_data(ngx_queue_head(&self->cb_queue),
Branch: 1 branch
BranchTrueFalseTotal
: ngx_queue_data(ngx_queue_head(&self->cb_queue), 3 1 4
1562 ev_gm_req_t, queue);
1563 4 ▶ int job_not_found = c && cl == 13 && memcmp(c, "JOB_NOT_FOUND", 13) == 0;
Branch: 3 branches
BranchTrueFalseTotal
int job_not_found = c && cl == 13 && memcmp(c, "JOB_NOT_FOUND", 13) == 0; 4 0 4
int job_not_found = c && cl == 13 && memcmp(c, "JOB_NOT_FOUND", 13) == 0; 2 2 4
int job_not_found = c && cl == 13 && memcmp(c, "JOB_NOT_FOUND", 13) == 0; 2 0 2
1564 4 ▶ if (!job_not_found && head && head->kind != CB_GRAB_JOB && head->kind != CB_ADMIN) {
Branch: 4 branches
BranchTrueFalseTotal
if (!job_not_found && head && head->kind != CB_GRAB_JOB && head->kind != CB_ADMIN) { 2 2 4
if (!job_not_found && head && head->kind != CB_GRAB_JOB && head->kind != CB_ADMIN) { 1 1 2
if (!job_not_found && head && head->kind != CB_GRAB_JOB && head->kind != CB_ADMIN) { 1 0 1
if (!job_not_found && head && head->kind != CB_GRAB_JOB && head->kind != CB_ADMIN) { 1 0 1
1565 1 pop_req(self);
1566 1 ▶ if (head->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (head->cb) { 1 0 1
1567 1 invoke_cb2(aTHX_ self, head->cb, NULL, err);
1568 } else {
1569 0 emit_error(aTHX_ self, SvPV_nolen(err));
1570 0 SvREFCNT_dec(err);
1571 }
1572 1 cleanup_req(aTHX_ head);
1573 } else {
1574 3 emit_error(aTHX_ self, SvPV_nolen(err));
1575 3 SvREFCNT_dec(err);
1576 }
1577 4 break;
1578 }
1579  
1580 0 default:
1581 0 break;
1582 }
1583 }
1584  
1585 /* Dispatch a JOB_ASSIGN[_UNIQ]: invoke registered function callback,
1586 * deliver result back to server (for sync mode). Async mode keeps a
1587 * job object alive in Perl-land until user invokes complete/fail. */
1588 13 static void worker_dispatch_job(pTHX_ ev_gm_t *self, ev_gm_req_t *r,
1589 int with_unique, const char *body, size_t body_len)
1590 {
1591 13 unsigned int gen = self->conn_gen;
1592 13 ▶ int total = with_unique ? 4 : 3;
Branch: 1 branch
BranchTrueFalseTotal
int total = with_unique ? 4 : 3; 0 13 13
1593 size_t hl, fl, ul, wl;
1594 13 const char *h = gm_arg(body, body_len, 0, total, &hl);
1595 13 const char *f = gm_arg(body, body_len, 1, total, &fl);
1596 13 ▶ const char *u = with_unique ? gm_arg(body, body_len, 2, total, &ul) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *u = with_unique ? gm_arg(body, body_len, 2, total, &ul) : NULL; 0 13 13
1597 13 ▶ const char *w = gm_arg(body, body_len, with_unique ? 3 : 2, total, &wl);
Branch: 1 branch
BranchTrueFalseTotal
const char *w = gm_arg(body, body_len, with_unique ? 3 : 2, total, &wl); 0 13 13
1598  
1599 15 ▶ if (!h || !f) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || !f) return; 13 0 13
if (!h || !f) return; 0 13 13
1600  
1601 /* Build a job hashref blessed into EV::Gearman::Job. */
1602 13 HV *job = newHV();
1603 13 hv_stores(job, "handle", newSVpvn(h, hl));
1604 13 hv_stores(job, "function", newSVpvn(f, fl));
1605 13 ▶ hv_stores(job, "unique", u ? newSVpvn(u, ul) : newSVpvn("", 0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(job, "unique", u ? newSVpvn(u, ul) : newSVpvn("", 0)); 0 13 13
1606 13 ▶ hv_stores(job, "workload", w ? newSVpvn(w, wl) : newSVpvn("", 0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(job, "workload", w ? newSVpvn(w, wl) : newSVpvn("", 0)); 13 0 13
1607 /* Tombstone reference on the client, carried as PERL_MAGIC_ext on
1608 the job HV — not a hash key, so user code, hash walkers and
1609 serializers can neither see nor clobber it, and a forged job
1610 hash has no magic to pass job_resolve's check. svt_free
1611 (ev_gm_job_magic_free) releases the reference; while any job
1612 lives, job_refs > 0 keeps this struct allocated (as an inert
1613 tombstone once DESTROY has run), so job_resolve's magic-word
1614 check never reads freed memory.
1615 Do NOT "simplify" this into storing the client's blessed SV with
1616 a bumped refcount: the old comment's double-DESTROY fear was
1617 unfounded (DESTROY fires once when the one blessed RV's refcount
1618 hits zero, no matter how many references a job holds), but a
1619 strong ref would invert ownership — the connection would stay
1620 connected and grabbing jobs, invisibly, for as long as any job
1621 is stashed, with no handle left to stop it. The RV is the sole
1622 owner; jobs are observers. */
1623 13 self->job_refs++;
1624 13 ▶ if (!self->job_session) self->job_session = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!self->job_session) self->job_session = newSV(0); 10 3 13
1625 13 sv_magicext((SV *)job, self->job_session, PERL_MAGIC_ext,
1626 &ev_gm_job_magic_vtbl, (const char *)self, 0);
1627  
1628 /* Owned reference, NOT a mortal: io_cb is a raw libev callback and
1629 never runs inside a per-callback Perl scope, so no FREETMPS would
1630 ever pop a mortal off the tmps stack — every dispatched job would
1631 stay pinned until EV::run returns (i.e. never, for a worker
1632 daemon). We release this reference on each exit path below. */
1633 13 SV *jobref = newRV_noinc((SV*)job);
1634 13 sv_bless(jobref, gv_stashpv("EV::Gearman::Job", GV_ADD));
1635  
1636 /* grab_job mode: caller supplied an explicit cb on the request,
1637 deliver the job to it instead of the registered-function path.
1638 invoke_cb2 mortalizes its arg, taking ownership of jobref. */
1639 13 ▶ if (r->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) { 0 13 13
1640 0 invoke_cb2(aTHX_ self, r->cb, jobref, NULL);
1641 0 ▶ if (self->magic == GM_MAGIC_ALIVE && self->conn_gen == gen)
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_ALIVE && self->conn_gen == gen) 0 0 0
if (self->magic == GM_MAGIC_ALIVE && self->conn_gen == gen) 0 0 0
1642 0 worker_continue(aTHX_ self);
1643 0 return;
1644 }
1645  
1646 13 ev_gm_func_t *fn = find_function(self, f, fl);
1647 13 ▶ if (!fn || !fn->cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (!fn || !fn->cb) { 13 0 13
if (!fn || !fn->cb) { 0 13 13
1648 /* A plain can_do() entry records the ability (so reconnect
1649 re-sends CAN_DO and grab_job can pull such jobs) but carries
1650 no handler; calling it would eat "Not a CODE reference"
1651 under G_EVAL and answer every job with a silent WORK_FAIL.
1652 Fail loudly (once per function) instead. */
1653 0 ▶ if (fn && !fn->no_cb_warned) {
Branch: 2 branches
BranchTrueFalseTotal
if (fn && !fn->no_cb_warned) { 0 0 0
if (fn && !fn->no_cb_warned) { 0 0 0
1654 0 fn->no_cb_warned = 1;
1655 0 warn("EV::Gearman: no handler for function '%.*s' "
1656 "(registered via can_do without a callback); "
1657 "use register_function or grab_job", (int)fl, f);
1658 }
1659 0 SvREFCNT_dec(jobref);
1660 0 send_work_event(aTHX_ self, GM_CMD_WORK_FAIL, h, hl, NULL, 0);
1661 0 worker_continue(aTHX_ self);
1662 0 return;
1663 }
1664  
1665 /* Snapshot fn->async before invoking the user callback: the
1666 callback is allowed to call cant_do() or reset_abilities(),
1667 which would free `fn`, leaving us with a stale pointer. The
1668 SV behind fn->cb stays alive during call_sv via Perl's own
1669 sub-context refcount, so reading it is safe. */
1670 13 int is_async = fn->async;
1671  
1672 13 self->callback_depth++;
1673 13 dSP;
1674 13 ENTER; SAVETMPS;
1675 13 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 13 13
1676 13 ▶ XPUSHs(jobref);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(jobref); 0 13 13
1677 13 PUTBACK;
1678 13 ▶ int count = call_sv(fn->cb, is_async ? (G_DISCARD | G_EVAL) : (G_SCALAR | G_EVAL));
Branch: 1 branch
BranchTrueFalseTotal
int count = call_sv(fn->cb, is_async ? (G_DISCARD | G_EVAL) : (G_SCALAR | G_EVAL)); 7 6 13
1679 13 SPAGAIN;
1680  
1681 13 SV *retval = NULL;
1682 13 ▶ int had_error = SvTRUE(ERRSV) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
int had_error = SvTRUE(ERRSV) ? 1 : 0; 13 0 13
1683 13 ▶ SV *err_sv = had_error ? newSVsv(ERRSV) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *err_sv = had_error ? newSVsv(ERRSV) : NULL; 0 13 13
SV *err_sv = had_error ? newSVsv(ERRSV) : NULL; 0 0 0
1684 13 ▶ if (had_error)
Branch: 1 branch
BranchTrueFalseTotal
if (had_error) 0 13 13
1685 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
1686  
1687 13 ▶ if (!is_async && count > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_async && count > 0) { 6 7 13
if (!is_async && count > 0) { 6 0 6
1688 6 retval = POPs;
1689 6 SvREFCNT_inc(retval);
1690 }
1691 13 PUTBACK;
1692 13 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 6 7 13
1693 13 self->callback_depth--;
1694 /* Release our owned jobref; if the callback stashed the job (async
1695 mode) its own reference keeps the job alive. */
1696 13 SvREFCNT_dec(jobref);
1697  
1698 13 ▶ if (self->magic == GM_MAGIC_FREED || self->conn_gen != gen) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED || self->conn_gen != gen) { 13 0 13
if (self->magic == GM_MAGIC_FREED || self->conn_gen != gen) { 2 11 13
1699 2 ▶ if (retval) SvREFCNT_dec(retval);
Branch: 1 branch
BranchTrueFalseTotal
if (retval) SvREFCNT_dec(retval); 2 0 2
1700 2 ▶ if (err_sv) SvREFCNT_dec(err_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (err_sv) SvREFCNT_dec(err_sv); 0 2 2
1701 2 return;
1702 }
1703  
1704 11 ▶ if (!is_async) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_async) { 4 7 11
1705 4 ▶ if (had_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (had_error) { 0 4 4
1706 /* WORK_EXCEPTION is terminal at the server (gearmand calls
1707 fail() on the job after forwarding the exception data),
1708 so send it INSTEAD OF WORK_FAIL when the option is on —
1709 sending both produces a JOB_NOT_FOUND error on the
1710 second packet. */
1711 0 ▶ if (self->opt_exceptions && err_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->opt_exceptions && err_sv) { 0 0 0
if (self->opt_exceptions && err_sv) { 0 0 0
1712 0 STRLEN el; const char *ep = SvPV(err_sv, el);
1713 0 send_work_event(aTHX_ self, GM_CMD_WORK_EXCEPTION, h, hl, ep, el);
1714 } else {
1715 0 send_work_event(aTHX_ self, GM_CMD_WORK_FAIL, h, hl, NULL, 0);
1716 }
1717 } else {
1718 4 STRLEN dl = 0;
1719 4 const char *dp = "";
1720 4 ▶ if (retval && SvOK(retval))
Branch: 2 branches
BranchTrueFalseTotal
if (retval && SvOK(retval)) 4 0 4
if (retval && SvOK(retval)) 4 0 4
1721 4 dp = SvPV(retval, dl);
1722 4 send_work_event(aTHX_ self, GM_CMD_WORK_COMPLETE, h, hl, dp, dl);
1723 }
1724 7 ▶ } else if (had_error && err_sv) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (had_error && err_sv) { 0 7 7
} else if (had_error && err_sv) { 0 0 0
1725 /* Async: the user keeps the job around and calls complete/fail
1726 later. We immediately grab the next job so async workers can
1727 process multiple jobs concurrently — bounded only by what the
1728 server has queued. Users who want a concurrency cap call
1729 work_stop in their callback (and work() again from
1730 complete()/fail() to resume). */
1731 0 warn("EV::Gearman: async worker callback raised: %s",
1732 SvPV_nolen(err_sv));
1733 }
1734 11 ▶ if (retval) SvREFCNT_dec(retval);
Branch: 1 branch
BranchTrueFalseTotal
if (retval) SvREFCNT_dec(retval); 4 7 11
1735 11 ▶ if (err_sv) SvREFCNT_dec(err_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (err_sv) SvREFCNT_dec(err_sv); 0 11 11
1736  
1737 11 ▶ if (self->worker_one_shot) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_one_shot) { 2 9 11
1738 2 self->worker_active = 0;
1739 2 self->worker_one_shot = 0;
1740 } else {
1741 9 worker_continue(aTHX_ self);
1742 }
1743 }
1744  
1745 /* ================================================================
1746 * Read path: parse binary or text-protocol responses
1747 *
1748 * The server may emit two stream styles:
1749 * - Binary packets with magic "\0RES" (response to binary requests)
1750 * - Text lines (response to admin commands like "status\n")
1751 *
1752 * We choose dispatch based on the head request kind: if head is
1753 * CB_ADMIN, we expect text; otherwise binary. If both styles are
1754 * intermixed we parse whichever the head expects.
1755 *
1756 * Admin replies: single-line commands ("version\n") return one
1757 * line; multi-line ("status\n", "workers\n") terminate with ".\n".
1758 * ================================================================ */
1759  
1760 /* Returns 1 if a complete admin reply was parsed and consumed,
1761 * 0 if more bytes are needed, -1 on protocol error (head request
1762 * is not CB_ADMIN — caller should disconnect). */
1763 260 static int parse_admin_response(pTHX_ ev_gm_t *self) {
1764 260 ▶ if (ngx_queue_empty(&self->cb_queue)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) return -1; 1 259 260
1765 259 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
1766 259 ev_gm_req_t *r = ngx_queue_data(q, ev_gm_req_t, queue);
1767 259 ▶ if (r->kind != CB_ADMIN) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (r->kind != CB_ADMIN) return -1; 0 259 259
1768  
1769 259 size_t consumed = 0;
1770 259 size_t result_len = 0;
1771 259 ▶ if (r->admin_terminator) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->admin_terminator) { 0 259 259
1772 /* Find ".\n" at start of a line. */
1773 0 const char *start = self->rbuf;
1774 0 const char *end = self->rbuf + self->rbuf_len;
1775 0 const char *p = start;
1776 0 const char *terminator = NULL;
1777 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
1778 0 ▶ if (p == start || *(p - 1) == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (p == start || *(p - 1) == '\n') { 0 0 0
if (p == start || *(p - 1) == '\n') { 0 0 0
1779 0 ▶ if (p[0] == '.' && (p + 1 < end) && p[1] == '\n') {
Branch: 3 branches
BranchTrueFalseTotal
if (p[0] == '.' && (p + 1 < end) && p[1] == '\n') { 0 0 0
if (p[0] == '.' && (p + 1 < end) && p[1] == '\n') { 0 0 0
if (p[0] == '.' && (p + 1 < end) && p[1] == '\n') { 0 0 0
1780 0 terminator = p;
1781 0 break;
1782 }
1783 }
1784 0 p++;
1785 }
1786 0 ▶ if (!terminator) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!terminator) return 0; 0 0 0
1787 0 consumed = (terminator - start) + 2; /* include ".\n" */
1788 0 result_len = terminator - start;
1789 } else {
1790 259 const char *nl = memchr(self->rbuf, '\n', self->rbuf_len);
1791 259 ▶ if (!nl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return 0; 256 3 259
1792 3 consumed = (nl - self->rbuf) + 1;
1793 3 result_len = nl - self->rbuf;
1794 /* gearmand ends some single-line replies with \r\n */
1795 3 ▶ if (result_len > 0 && self->rbuf[result_len - 1] == '\r') result_len--;
Branch: 2 branches
BranchTrueFalseTotal
if (result_len > 0 && self->rbuf[result_len - 1] == '\r') result_len--; 3 0 3
if (result_len > 0 && self->rbuf[result_len - 1] == '\r') result_len--; 1 2 3
1796 }
1797  
1798 3 ngx_queue_remove(&r->queue);
1799 3 self->pending_count--;
1800 3 ▶ if (ngx_queue_empty(&self->cb_queue)) disarm_cmd_timer(self);
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) disarm_cmd_timer(self); 3 0 3
1801 0 else arm_cmd_timer(self);
1802 /* gearmand's text-protocol error replies are single-line
1803 "ERR ..." — deliver them as the error argument, not as a
1804 successful result (mirrors the binary ERROR convention). The
1805 raw text is kept intact. */
1806 5 ▶ int is_err = (!r->admin_terminator && result_len >= 4 &&
Branch: 2 branches
BranchTrueFalseTotal
int is_err = (!r->admin_terminator && result_len >= 4 && 3 0 3
int is_err = (!r->admin_terminator && result_len >= 4 && 2 1 3
1807 2 ▶ memcmp(self->rbuf, "ERR ", 4) == 0);
Branch: 1 branch
BranchTrueFalseTotal
memcmp(self->rbuf, "ERR ", 4) == 0); 0 2 2
1808 /* invoke_cb2 dec's the SV when r->cb is NULL, so build it
1809 unconditionally and let the helper handle the no-cb case. */
1810 3 unsigned int gen = self->conn_gen;
1811 6 ▶ invoke_cb2(aTHX_ self, r->cb,
Branch: 2 branches
BranchTrueFalseTotal
invoke_cb2(aTHX_ self, r->cb, 0 3 3
invoke_cb2(aTHX_ self, r->cb, 3 0 3
1812 3 is_err ? NULL : newSVpvn(self->rbuf, result_len),
1813 0 is_err ? newSVpvn(self->rbuf, result_len) : NULL);
1814 3 cleanup_req(aTHX_ r);
1815  
1816 3 ▶ if (self->magic == GM_MAGIC_FREED) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return 1; 0 3 3
1817 /* Callbacks may disconnect (zeroing rbuf_len) or even reconnect;
1818 bail before the bookkeeping below underflows it. The generation
1819 check catches the disconnect()+connect() flip. */
1820 3 ▶ if (!self->connected || self->conn_gen != gen) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!self->connected || self->conn_gen != gen) return 1; 3 0 3
if (!self->connected || self->conn_gen != gen) return 1; 1 2 3
1821  
1822 2 self->rbuf_len -= consumed;
1823 2 ▶ if (self->rbuf_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len > 0) 0 2 2
1824 0 memmove(self->rbuf, self->rbuf + consumed, self->rbuf_len);
1825 2 return 1;
1826 }
1827  
1828 338 static void process_responses(pTHX_ ev_gm_t *self) {
1829 391 ▶ while (self->rbuf_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->rbuf_len > 0) { 341 50 391
1830 /* Binary response packets start with "\0RES"; the text/admin
1831 protocol never emits a leading NUL, so the first byte is
1832 enough to pick a parser. */
1833 341 int is_admin = (self->rbuf[0] != '\0');
1834  
1835 341 ▶ if (is_admin) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_admin) { 260 81 341
1836 260 unsigned int gen = self->conn_gen;
1837 260 int rv = parse_admin_response(aTHX_ self);
1838 260 ▶ if (rv == 0) { /* need more bytes */
Branch: 1 branch
BranchTrueFalseTotal
if (rv == 0) { /* need more bytes */ 256 4 260
1839 /* A broken/hostile peer can stream an unterminated
1840 admin reply forever; without a length prefix there is
1841 no other bound on the accumulation. (rv == 0 implies
1842 the head request is CB_ADMIN.) */
1843 256 ▶ if (self->rbuf_len > GM_MAX_ADMIN_RESPONSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len > GM_MAX_ADMIN_RESPONSE) { 1 255 256
1844 1 handle_disconnect(aTHX_ self, "admin response too large");
1845 1 return;
1846 }
1847 255 break;
1848 }
1849 4 ▶ if (rv < 0) { /* protocol error */
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { /* protocol error */ 1 3 4
1850 1 handle_disconnect(aTHX_ self,
1851 "unexpected admin response (queue head is not admin)");
1852 1 return;
1853 }
1854 3 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 3 3
1855 3 ▶ if (!self->connected || self->conn_gen != gen) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self->connected || self->conn_gen != gen) return; 3 0 3
if (!self->connected || self->conn_gen != gen) return; 1 2 3
1856 2 continue;
1857 }
1858  
1859 81 ▶ if (self->rbuf_len < GM_HEADER_SIZE) break;
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < GM_HEADER_SIZE) break; 0 81 81
1860 81 ▶ if (memcmp(self->rbuf, GM_MAGIC_RES, 4) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(self->rbuf, GM_MAGIC_RES, 4) != 0) { 1 80 81
1861 1 handle_disconnect(aTHX_ self, "invalid response magic");
1862 1 return;
1863 }
1864 80 uint32_t cmd = gm_read_u32(self->rbuf + 4);
1865 80 uint32_t body_len = gm_read_u32(self->rbuf + 8);
1866 80 ▶ if (body_len > GM_MAX_PACKET) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_len > GM_MAX_PACKET) { 1 79 80
1867 1 handle_disconnect(aTHX_ self, "response packet too large");
1868 1 return;
1869 }
1870 79 size_t total = (size_t)GM_HEADER_SIZE + body_len;
1871 79 ▶ if (self->rbuf_len < total) break;
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < total) break; 20 59 79
1872  
1873 /* rbuf is input-only — dispatch may grow wbuf but never rbuf,
1874 so the body pointer stays valid until the memmove below. */
1875 59 const char *body = self->rbuf + GM_HEADER_SIZE;
1876 59 unsigned int gen = self->conn_gen;
1877 59 handle_response_packet(aTHX_ self, cmd, body, body_len);
1878 59 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 59 59
1879 59 ▶ if (!self->connected || self->conn_gen != gen) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self->connected || self->conn_gen != gen) return; 54 5 59
if (!self->connected || self->conn_gen != gen) return; 3 51 54
1880  
1881 51 self->rbuf_len -= total;
1882 51 ▶ if (self->rbuf_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len > 0) 3 48 51
1883 3 memmove(self->rbuf, self->rbuf + total, self->rbuf_len);
1884 }
1885  
1886 /* Fully consumed: release an oversized read buffer (e.g. after a
1887 single large packet) instead of pinning it for the connection's
1888 lifetime. */
1889 325 ▶ if (self->rbuf_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len == 0) 50 275 325
1890 50 buf_maybe_shrink(&self->rbuf, &self->rbuf_cap);
1891 }
1892  
1893 /* ================================================================
1894 * Read/write IO
1895 * ================================================================ */
1896  
1897 344 static void on_readable(pTHX_ ev_gm_t *self) {
1898 344 buf_ensure_read(self, 4096);
1899 344 ssize_t n = read(self->fd, self->rbuf + self->rbuf_len,
1900 344 self->rbuf_cap - self->rbuf_len);
1901 344 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 338 6 344
1902 338 self->rbuf_len += n;
1903 338 process_responses(aTHX_ self);
1904 6 ▶ } else if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (n == 0) { 3 3 6
1905 3 handle_disconnect(aTHX_ self, "connection closed by server");
1906 } else {
1907 3 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
Branch: 3 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 3 0 3
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 3 0 3
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 3 3
1908 0 return;
1909 char errbuf[128];
1910 3 snprintf(errbuf, sizeof(errbuf), "read error: %s", strerror(errno));
1911 3 handle_disconnect(aTHX_ self, errbuf);
1912 }
1913 }
1914  
1915 67 static int try_write(ev_gm_t *self) {
1916 134 ▶ while (self->wbuf_off < self->wbuf_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->wbuf_off < self->wbuf_len) { 67 67 134
1917 /* MSG_NOSIGNAL where it exists (Linux, NetBSD); elsewhere
1918 SO_NOSIGPIPE was set at socket creation. Both are defensive
1919 hygiene — the first write to a dead peer normally just
1920 returns ECONNRESET — since installing a SIGPIPE handler is
1921 the embedder's business, not a library's. */
1922 #ifdef MSG_NOSIGNAL
1923 67 ssize_t n = send(self->fd, self->wbuf + self->wbuf_off,
1924 67 self->wbuf_len - self->wbuf_off, MSG_NOSIGNAL);
1925 #else
1926 ssize_t n = write(self->fd, self->wbuf + self->wbuf_off,
1927 self->wbuf_len - self->wbuf_off);
1928 #endif
1929 67 ▶ if (n > 0) self->wbuf_off += n;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) self->wbuf_off += n; 67 0 67
1930 0 ▶ else if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (n < 0) { 0 0 0
1931 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
Branch: 3 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
1932 0 return 0;
1933 0 return -1;
1934 }
1935 }
1936 67 self->wbuf_len = 0;
1937 67 self->wbuf_off = 0;
1938 67 buf_maybe_shrink(&self->wbuf, &self->wbuf_cap);
1939 67 stop_writing(self);
1940 67 return 1;
1941 }
1942  
1943 /* Attempt one non-blocking connect to the candidate at self->ai_cur.
1944 Returns 1 when the sequence stops here: the connect either completed
1945 synchronously (finish_connect_success already ran) or is in flight
1946 (EINPROGRESS; on_connect_complete finishes it). Returns 0 on
1947 immediate failure with errbuf filled; the caller advances. */
1948 64 static int connect_attempt(pTHX_ ev_gm_t *self, char *errbuf, size_t errbuf_size) {
1949 64 struct addrinfo *ai = self->ai_cur;
1950 int fd, ret;
1951  
1952 #ifdef SOCK_CLOEXEC
1953 64 fd = socket(ai->ai_family, ai->ai_socktype | SOCK_CLOEXEC, ai->ai_protocol);
1954 64 ▶ if (fd < 0 && errno == EINVAL) /* old kernels: fall back */
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 && errno == EINVAL) /* old kernels: fall back */ 0 64 64
if (fd < 0 && errno == EINVAL) /* old kernels: fall back */ 0 0 0
1955 0 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1956 #else
1957 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1958 #endif
1959 64 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 64 64
1960 0 snprintf(errbuf, errbuf_size, "socket: %s", strerror(errno));
1961 0 return 0;
1962 }
1963 64 ▶ if (gm_set_socket_flags(fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gm_set_socket_flags(fd) < 0) { 0 64 64
1964 0 close(fd);
1965 0 snprintf(errbuf, errbuf_size, "fcntl set socket flags failed");
1966 0 return 0;
1967 }
1968 64 int one = 1;
1969 64 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
1970 64 self->fd = fd;
1971 64 ret = connect(fd, ai->ai_addr, ai->ai_addrlen);
1972  
1973 /* Watcher init is the same for the immediate-success, EINPROGRESS
1974 and immediate-failure branches; only ev_io_start (via start_*)
1975 differs. Init'd-but-not-started watchers are inert, so it is
1976 safe to run this even in the failure branch which closes the fd. */
1977 64 ev_io_init(&self->rio, io_cb, self->fd, EV_READ);
1978 64 self->rio.data = (void *)self;
1979 64 ev_io_init(&self->wio, io_cb, self->fd, EV_WRITE);
1980 64 self->wio.data = (void *)self;
1981 64 ev_set_priority(&self->rio, self->priority);
1982 64 ev_set_priority(&self->wio, self->priority);
1983  
1984 64 ▶ if (ret == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) { 0 64 64
1985 0 clear_addrinfo(self);
1986 0 stop_connect_timer(self);
1987 0 self->connected = 1;
1988 0 finish_connect_success(aTHX_ self);
1989 0 return 1;
1990 }
1991 64 ▶ if (errno != EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno != EINPROGRESS) { 0 64 64
1992 0 snprintf(errbuf, errbuf_size, "connect: %s", strerror(errno));
1993 0 close(self->fd);
1994 0 self->fd = -1;
1995 0 return 0;
1996 }
1997 64 self->connecting = 1;
1998 64 start_writing(self);
1999  
2000 /* Armed only on the first async candidate: the timeout bounds the
2001 whole candidate sequence, not each individual attempt. */
2002 64 ▶ if (!self->connect_timer_active && self->connect_timeout_ms > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->connect_timer_active && self->connect_timeout_ms > 0) { 64 0 64
if (!self->connect_timer_active && self->connect_timeout_ms > 0) { 0 64 64
2003 0 ev_tstamp delay = (ev_tstamp)self->connect_timeout_ms / 1000.0;
2004 0 ev_timer_init(&self->connect_timer, connect_timeout_cb, delay, 0.0);
2005 0 self->connect_timer.data = (void *)self;
2006 0 ev_timer_start(self->loop, &self->connect_timer);
2007 0 self->connect_timer_active = 1;
2008 }
2009 64 return 1;
2010 }
2011  
2012 /* Walk candidates from self->ai_cur onward until one connects (or is
2013 in flight). prev_err is the failure text of the candidate that just
2014 failed; when the list runs out, report the most recent error and
2015 let the reconnect logic run. */
2016 88 static void connect_try_from(pTHX_ ev_gm_t *self, const char *prev_err) {
2017 char errbuf[160];
2018 88 snprintf(errbuf, sizeof(errbuf), "%s", prev_err);
2019 88 ▶ while (self->ai_cur) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->ai_cur) { 64 24 88
2020 64 ▶ if (connect_attempt(aTHX_ self, errbuf, sizeof(errbuf)))
Branch: 1 branch
BranchTrueFalseTotal
if (connect_attempt(aTHX_ self, errbuf, sizeof(errbuf))) 64 0 64
2021 64 return;
2022 0 self->ai_cur = self->ai_cur->ai_next;
2023 }
2024 24 stop_connect_timer(self);
2025 24 clear_addrinfo(self);
2026 24 report_connect_error(aTHX_ self, errbuf);
2027 }
2028  
2029 58 static void on_connect_complete(pTHX_ ev_gm_t *self) {
2030 58 int err = 0;
2031 58 socklen_t len = sizeof(err);
2032 58 ▶ if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0 || err != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0 || err != 0) { 58 0 58
if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0 || err != 0) { 25 33 58
2033 char errbuf[128];
2034 25 snprintf(errbuf, sizeof(errbuf), "connect failed: %s",
2035 25 ▶ strerror(err ? err : errno));
Branch: 1 branch
BranchTrueFalseTotal
strerror(err ? err : errno)); 0 25 25
2036 25 close(self->fd);
2037 25 self->fd = -1;
2038 25 self->connecting = 0;
2039 25 stop_writing(self);
2040 /* Fall through to the next resolved address, if any (e.g. a
2041 dual-stack 'localhost' refusing ::1 while 127.0.0.1 would
2042 answer). The connect timer keeps running; on exhaustion
2043 connect_try_from stops it and reports the last error. */
2044 25 ▶ if (self->ai_cur)
Branch: 1 branch
BranchTrueFalseTotal
if (self->ai_cur) 25 0 25
2045 25 self->ai_cur = self->ai_cur->ai_next;
2046 25 connect_try_from(aTHX_ self, errbuf);
2047 25 return;
2048 }
2049 33 self->connecting = 0;
2050 33 self->connected = 1;
2051 33 stop_writing(self);
2052 33 stop_connect_timer(self);
2053 33 clear_addrinfo(self);
2054 33 finish_connect_success(aTHX_ self);
2055 }
2056  
2057 469 static void io_cb(EV_P_ ev_io *w, int revents) {
2058 dTHX;
2059 469 ev_gm_t *self = (ev_gm_t *)w->data;
2060 (void)loop;
2061 469 ▶ if (self->magic != GM_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) return; 0 469 469
2062 469 self->callback_depth++;
2063  
2064 469 ▶ if (self->connecting) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connecting) { 58 411 469
2065 58 ▶ if (revents & EV_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) { 58 0 58
2066 58 on_connect_complete(aTHX_ self);
2067 }
2068 58 self->callback_depth--;
2069 58 check_destroyed(self);
2070 58 return;
2071 }
2072  
2073 411 ▶ if (revents & EV_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_READ) { 344 67 411
2074 344 on_readable(aTHX_ self);
2075 344 ▶ if (self->magic != GM_MAGIC_ALIVE) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) { 0 344 344
2076 0 self->callback_depth--;
2077 0 check_destroyed(self);
2078 0 return;
2079 }
2080 }
2081 411 ▶ if (revents & EV_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) { 67 344 411
2082 67 int rv = try_write(self);
2083 67 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 0 67 67
2084 char errbuf[128];
2085 0 snprintf(errbuf, sizeof(errbuf), "write error: %s", strerror(errno));
2086 0 handle_disconnect(aTHX_ self, errbuf);
2087 }
2088 }
2089 411 self->callback_depth--;
2090 411 check_destroyed(self);
2091 }
2092  
2093 /* ================================================================
2094 * start_connect
2095 * ================================================================ */
2096  
2097 87 static void start_connect(pTHX_ ev_gm_t *self) {
2098 int fd, ret;
2099  
2100 /* Backstop for every caller: starting a connect sequence while
2101 one is live would leak the old fd and re-init active watchers. */
2102 87 ▶ if (self->connected || self->connecting) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) return; 87 0 87
if (self->connected || self->connecting) return; 0 87 87
2103  
2104 87 ▶ if (!self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->path) { 63 24 87
2105 63 struct addrinfo hints, *res = NULL;
2106 char port_str[16];
2107 63 snprintf(port_str, sizeof(port_str), "%d", self->port);
2108 63 memset(&hints, 0, sizeof(hints));
2109 63 hints.ai_family = AF_UNSPEC;
2110 63 hints.ai_socktype = SOCK_STREAM;
2111  
2112 63 ret = getaddrinfo(self->host, port_str, &hints, &res);
2113 63 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 63 63
2114 char errbuf[256];
2115 0 snprintf(errbuf, sizeof(errbuf), "getaddrinfo: %s", gai_strerror(ret));
2116 0 report_connect_error(aTHX_ self, errbuf);
2117 0 return;
2118 }
2119 /* Try every resolved address in turn: a dual-stack 'localhost'
2120 typically resolves to ::1 first, which a gearmand bound to
2121 127.0.0.1 will refuse — the fallback list is the only way
2122 such a host stays reachable by name. */
2123 63 clear_addrinfo(self);
2124 63 self->ai_list = res;
2125 63 self->ai_cur = res;
2126 63 connect_try_from(aTHX_ self, "connect: no resolved addresses");
2127 63 return;
2128 }
2129  
2130 /* Unix-domain socket: a single fixed address, no fallback list. */
2131 {
2132 struct sockaddr_un addr;
2133 24 memset(&addr, 0, sizeof(addr));
2134 24 addr.sun_family = AF_UNIX;
2135 24 ▶ if (strlen(self->path) >= sizeof(addr.sun_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(self->path) >= sizeof(addr.sun_path)) { 3 21 24
2136 3 report_connect_error(aTHX_ self, "unix socket path too long");
2137 3 return;
2138 }
2139 21 strncpy(addr.sun_path, self->path, sizeof(addr.sun_path) - 1);
2140  
2141 #ifdef SOCK_CLOEXEC
2142 21 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
2143 21 ▶ if (fd < 0 && errno == EINVAL) /* old kernels: fall back */
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 && errno == EINVAL) /* old kernels: fall back */ 0 21 21
if (fd < 0 && errno == EINVAL) /* old kernels: fall back */ 0 0 0
2144 0 fd = socket(AF_UNIX, SOCK_STREAM, 0);
2145 #else
2146 fd = socket(AF_UNIX, SOCK_STREAM, 0);
2147 #endif
2148 21 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 21 21
2149 char errbuf[128];
2150 0 snprintf(errbuf, sizeof(errbuf), "socket: %s", strerror(errno));
2151 0 report_connect_error(aTHX_ self, errbuf);
2152 0 return;
2153 }
2154 21 ▶ if (gm_set_socket_flags(fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gm_set_socket_flags(fd) < 0) { 0 21 21
2155 0 close(fd);
2156 0 report_connect_error(aTHX_ self, "fcntl set socket flags failed");
2157 0 return;
2158 }
2159 21 self->fd = fd;
2160 21 ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
2161 }
2162  
2163 /* Watcher init is the same for the immediate-success and
2164 EINPROGRESS branches; only ev_io_start (via start_*) differs.
2165 Init'd-but-not-started watchers are inert, so it is safe to
2166 run this even in the failure branch which closes the fd. */
2167 21 ev_io_init(&self->rio, io_cb, self->fd, EV_READ);
2168 21 self->rio.data = (void *)self;
2169 21 ev_io_init(&self->wio, io_cb, self->fd, EV_WRITE);
2170 21 self->wio.data = (void *)self;
2171 21 ev_set_priority(&self->rio, self->priority);
2172 21 ev_set_priority(&self->wio, self->priority);
2173  
2174 21 ▶ if (ret == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) { 20 1 21
2175 20 self->connected = 1;
2176 20 finish_connect_success(aTHX_ self);
2177 20 return;
2178 }
2179 1 ▶ if (errno != EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno != EINPROGRESS) { 1 0 1
2180 char errbuf[128];
2181 1 snprintf(errbuf, sizeof(errbuf), "connect: %s", strerror(errno));
2182 1 close(self->fd);
2183 1 self->fd = -1;
2184 1 report_connect_error(aTHX_ self, errbuf);
2185 1 return;
2186 }
2187 0 self->connecting = 1;
2188 0 start_writing(self);
2189  
2190 0 ▶ if (self->connect_timeout_ms > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timeout_ms > 0) { 0 0 0
2191 0 ev_tstamp delay = (ev_tstamp)self->connect_timeout_ms / 1000.0;
2192 0 ev_timer_init(&self->connect_timer, connect_timeout_cb, delay, 0.0);
2193 0 self->connect_timer.data = (void *)self;
2194 0 ev_timer_start(self->loop, &self->connect_timer);
2195 0 self->connect_timer_active = 1;
2196 }
2197 }
2198  
2199 /* ================================================================
2200 * Helper to encode a multi-arg body: NUL-separated.
2201 * ================================================================ */
2202  
2203 /* Concatenate args with NUL separators into out (caller frees).
2204 * args: array of {ptr, len} pairs; n_args entries.
2205 * Returns total length in *out_len. NULL ptr is treated as empty. */
2206 11 static char* gm_encode_args(int n_args, const char **ptrs, const STRLEN *lens, size_t *out_len) {
2207 11 size_t total = 0;
2208 int i;
2209 47 ▶ for (i = 0; i < n_args; i++) total += (ptrs[i] ? lens[i] : 0);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n_args; i++) total += (ptrs[i] ? lens[i] : 0); 36 0 36
for (i = 0; i < n_args; i++) total += (ptrs[i] ? lens[i] : 0); 36 11 47
2210 11 ▶ if (n_args > 1) total += (n_args - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (n_args > 1) total += (n_args - 1); 11 0 11
2211 char *out;
2212 11 ▶ Newx(out, total ? total : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(out, total ? total : 1, char); 11 0 11
2213 11 char *p = out;
2214 47 ▶ for (i = 0; i < n_args; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_args; i++) { 36 11 47
2215 36 ▶ if (i > 0) { *p++ = '\0'; }
Branch: 1 branch
BranchTrueFalseTotal
if (i > 0) { *p++ = '\0'; } 25 11 36
2216 36 ▶ if (ptrs[i] && lens[i] > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptrs[i] && lens[i] > 0) { 36 0 36
if (ptrs[i] && lens[i] > 0) { 27 9 36
2217 27 memcpy(p, ptrs[i], lens[i]);
2218 27 p += lens[i];
2219 }
2220 }
2221 11 *out_len = total;
2222 11 return out;
2223 }
2224  
2225 /* ================================================================
2226 * XS interface
2227 * ================================================================ */
2228  
2229 /* Tear down a half-built object on a constructor croak path. */
2230 4 static void cleanup_partial_ctor(ev_gm_t *self) {
2231 4 Safefree(self->rbuf);
2232 4 Safefree(self->wbuf);
2233 4 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 4 0 4
2234 4 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 0 4 4
2235 4 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 0 4 4
2236 4 ▶ CLEAR_HANDLER(self->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->loop_sv); 0 4 4
2237 4 ▶ if (self->client_id) Safefree(self->client_id);
Branch: 1 branch
BranchTrueFalseTotal
if (self->client_id) Safefree(self->client_id); 0 4 4
2238 4 ▶ if (self->host) Safefree(self->host);
Branch: 1 branch
BranchTrueFalseTotal
if (self->host) Safefree(self->host); 0 4 4
2239 4 ▶ if (self->path) Safefree(self->path);
Branch: 1 branch
BranchTrueFalseTotal
if (self->path) Safefree(self->path); 0 4 4
2240 4 Safefree(self);
2241 4 }
2242  
2243 MODULE = EV::Gearman PACKAGE = EV::Gearman
2244  
2245 BOOT:
2246 {
2247 33 ▶ I_EV_API("EV::Gearman");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::Gearman"); 0 33 33
I_EV_API("EV::Gearman"); 33 0 33
I_EV_API("EV::Gearman"); 0 33 33
2248 }
2249  
2250 EV::Gearman
2251 new(char *class, ...)
2252 CODE:
2253 {
2254 PERL_UNUSED_VAR(class);
2255 71 ▶ if ((items - 1) % 2 != 0) croak("odd number of arguments");
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2 != 0) croak("odd number of arguments"); 1 70 71
2256  
2257 70 Newxz(RETVAL, 1, ev_gm_t);
2258 70 RETVAL->magic = GM_MAGIC_ALIVE;
2259 70 RETVAL->fd = -1;
2260 70 RETVAL->port = 4730;
2261 70 ngx_queue_init(&RETVAL->cb_queue);
2262 70 ngx_queue_init(&RETVAL->wait_queue);
2263 70 ngx_queue_init(&RETVAL->active_jobs);
2264 70 ngx_queue_init(&RETVAL->functions);
2265 70 Newx(RETVAL->rbuf, BUF_INIT_SIZE, char);
2266 70 RETVAL->rbuf_cap = BUF_INIT_SIZE;
2267 70 Newx(RETVAL->wbuf, BUF_INIT_SIZE, char);
2268 70 RETVAL->wbuf_cap = BUF_INIT_SIZE;
2269  
2270 /* Default error handler. eval_pv hands back the result without
2271 transferring a reference we own (it stays mortal), so take one
2272 to keep the sub alive past the current statement. */
2273 70 RETVAL->on_error = eval_pv("sub { warn \"EV::Gearman error: @_\\n\" }", TRUE);
2274 70 SvREFCNT_inc_simple_void_NN(RETVAL->on_error);
2275  
2276 70 SV *host_sv = NULL, *path_sv = NULL;
2277 70 int port = 4730;
2278 70 int do_reconnect = 0, reconnect_delay = 1000, max_reconnect_attempts = 0;
2279 70 RETVAL->loop = EV_DEFAULT;
2280  
2281 int i;
2282 284 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 217 67 284
2283 217 const char *k = SvPV_nolen(ST(i));
2284 217 SV *v = ST(i + 1);
2285  
2286 217 ▶ if (strEQ(k, "host")) host_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "host")) host_sv = v; 41 176 217
2287 176 ▶ else if (strEQ(k, "port")) port = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "port")) port = SvIV(v); 40 136 176
2288 136 ▶ else if (strEQ(k, "path")) path_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "path")) path_sv = v; 15 121 136
2289 121 ▶ else if (strEQ(k, "on_error")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "on_error")) { 53 68 121
2290 /* Junk (defined non-coderef) is ignored so the default
2291 warn handler survives; explicit undef clears it. */
2292 53 ▶ if (!SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) { 0 53 53
2293 0 ▶ CLEAR_HANDLER(RETVAL->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_error); 0 0 0
2294 53 ▶ } else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) { 52 1 53
} else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) { 52 0 52
2295 52 ▶ CLEAR_HANDLER(RETVAL->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_error); 52 0 52
2296 52 RETVAL->on_error = newSVsv(v);
2297 }
2298 }
2299 68 ▶ else if (strEQ(k, "on_connect")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "on_connect")) { 12 56 68
2300 12 ▶ if (SvOK(v) && SvROK(v)) RETVAL->on_connect = newSVsv(v);
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(v) && SvROK(v)) RETVAL->on_connect = newSVsv(v); 12 0 12
if (SvOK(v) && SvROK(v)) RETVAL->on_connect = newSVsv(v); 12 0 12
2301 }
2302 56 ▶ else if (strEQ(k, "on_disconnect")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "on_disconnect")) { 10 46 56
2303 10 ▶ if (SvOK(v) && SvROK(v)) RETVAL->on_disconnect = newSVsv(v);
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(v) && SvROK(v)) RETVAL->on_disconnect = newSVsv(v); 10 0 10
if (SvOK(v) && SvROK(v)) RETVAL->on_disconnect = newSVsv(v); 10 0 10
2304 }
2305 46 ▶ else if (strEQ(k, "connect_timeout")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "connect_timeout")) { 2 44 46
2306 2 RETVAL->connect_timeout_ms = SvIV(v);
2307 2 ▶ if (RETVAL->connect_timeout_ms < 0) RETVAL->connect_timeout_ms = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->connect_timeout_ms < 0) RETVAL->connect_timeout_ms = 0; 1 1 2
2308 }
2309 44 ▶ else if (strEQ(k, "command_timeout")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "command_timeout")) { 7 37 44
2310 7 RETVAL->command_timeout_ms = SvIV(v);
2311 7 ▶ if (RETVAL->command_timeout_ms < 0) RETVAL->command_timeout_ms = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->command_timeout_ms < 0) RETVAL->command_timeout_ms = 0; 1 6 7
2312 }
2313 37 ▶ else if (strEQ(k, "priority")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "priority")) { 3 34 37
2314 3 RETVAL->priority = SvIV(v);
2315 3 ▶ if (RETVAL->priority < -2) RETVAL->priority = -2;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->priority < -2) RETVAL->priority = -2; 1 2 3
2316 3 ▶ if (RETVAL->priority > 2) RETVAL->priority = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->priority > 2) RETVAL->priority = 2; 1 2 3
2317 }
2318 34 ▶ else if (strEQ(k, "keepalive")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "keepalive")) { 2 32 34
2319 2 RETVAL->keepalive = SvIV(v);
2320 2 ▶ if (RETVAL->keepalive < 0) RETVAL->keepalive = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->keepalive < 0) RETVAL->keepalive = 0; 1 1 2
2321 }
2322 32 ▶ else if (strEQ(k, "reconnect")) do_reconnect = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "reconnect")) do_reconnect = SvTRUE(v) ? 1 : 0; 11 21 32
2323 21 ▶ else if (strEQ(k, "reconnect_delay")) reconnect_delay = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "reconnect_delay")) reconnect_delay = SvIV(v); 11 10 21
2324 10 ▶ else if (strEQ(k, "max_reconnect_attempts")) max_reconnect_attempts = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "max_reconnect_attempts")) max_reconnect_attempts = SvIV(v); 3 7 10
2325 7 ▶ else if (strEQ(k, "exceptions")) RETVAL->opt_exceptions = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "exceptions")) RETVAL->opt_exceptions = SvTRUE(v) ? 1 : 0; 1 6 7
2326 6 ▶ else if (strEQ(k, "client_id")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "client_id")) { 2 4 6
2327 2 ▶ if (SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) { 2 0 2
2328 STRLEN cid_len;
2329 2 const char *cid = SvPV(v, cid_len);
2330 2 ▶ if (cid_len > 0 && memchr(cid, '\0', cid_len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cid_len > 0 && memchr(cid, '\0', cid_len)) { 2 0 2
if (cid_len > 0 && memchr(cid, '\0', cid_len)) { 1 1 2
2331 1 cleanup_partial_ctor(RETVAL);
2332 1 croak("EV::Gearman: client id may not contain NUL bytes");
2333 }
2334 1 RETVAL->client_id = savepv(cid);
2335 }
2336 }
2337 4 ▶ else if (strEQ(k, "grab_unique")) RETVAL->worker_grab_uniq = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "grab_unique")) RETVAL->worker_grab_uniq = SvTRUE(v) ? 1 : 0; 1 3 4
2338 3 ▶ else if (strEQ(k, "loop")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "loop")) { 1 2 3
2339 1 ▶ if (!SvROK(v) || !sv_derived_from(v, "EV::Loop")) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(v) || !sv_derived_from(v, "EV::Loop")) { 1 0 1
if (!SvROK(v) || !sv_derived_from(v, "EV::Loop")) { 0 1 1
2340 0 cleanup_partial_ctor(RETVAL);
2341 0 croak("loop => must be an EV::Loop object");
2342 }
2343 /* An EV::Loop object is a blessed scalar holding the
2344 struct ev_loop pointer in its IV slot (T_PTROBJ style);
2345 the PV slot is NULL, so SvPVX would read garbage. */
2346 1 RETVAL->loop = INT2PTR(struct ev_loop *, SvIV(SvRV(v)));
2347 /* Hold a strong ref so the loop outlives the client even
2348 when the caller drops their own reference to it. */
2349 1 ▶ CLEAR_HANDLER(RETVAL->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->loop_sv); 0 1 1
2350 1 RETVAL->loop_sv = newSVsv(v);
2351 }
2352 else {
2353 /* An unknown key is almost always a typo ("reconect") that
2354 would silently leave the intended option unset — fail
2355 loudly. */
2356 2 cleanup_partial_ctor(RETVAL);
2357 2 croak("EV::Gearman: unknown constructor option '%s'", k);
2358 }
2359 }
2360  
2361 67 ▶ if (host_sv && path_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (host_sv && path_sv) { 41 26 67
if (host_sv && path_sv) { 1 40 41
2362 1 cleanup_partial_ctor(RETVAL);
2363 1 croak("cannot specify both 'host' and 'path'");
2364 }
2365  
2366 66 RETVAL->port = port;
2367 /* Always store delay/cap, so a later $g->reconnect(1) gets the
2368 documented 1000 ms default instead of the struct's zero. */
2369 66 RETVAL->reconnect_delay_ms = reconnect_delay >= 0 ? reconnect_delay : 0;
2370 66 RETVAL->max_reconnect_attempts = max_reconnect_attempts >= 0 ? max_reconnect_attempts : 0;
2371 66 ▶ if (do_reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (do_reconnect) { 11 55 66
2372 11 RETVAL->reconnect = 1;
2373 }
2374  
2375 66 ▶ if (host_sv && SvOK(host_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (host_sv && SvOK(host_sv)) { 40 26 66
if (host_sv && SvOK(host_sv)) { 40 0 40
2376 40 RETVAL->host = savepv(SvPV_nolen(host_sv));
2377 40 start_connect(aTHX_ RETVAL);
2378 26 ▶ } else if (path_sv && SvOK(path_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (path_sv && SvOK(path_sv)) { 14 12 26
} else if (path_sv && SvOK(path_sv)) { 14 0 14
2379 14 RETVAL->path = savepv(SvPV_nolen(path_sv));
2380 14 start_connect(aTHX_ RETVAL);
2381 }
2382 }
2383 OUTPUT:
2384 RETVAL
2385  
2386 void
2387 DESTROY(EV::Gearman self)
2388 CODE:
2389 {
2390 66 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 66 66
2391  
2392 66 ▶ if (self->callback_depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->callback_depth > 0) { 0 66 66
2393 0 self->magic = GM_MAGIC_FREED;
2394 0 self->connected = 0;
2395 0 self->connecting = 0;
2396 0 stop_reading(self);
2397 0 stop_writing(self);
2398 0 stop_connect_timer(self);
2399 0 disarm_cmd_timer(self);
2400 0 stop_reconnect_timer(self);
2401 0 ▶ if (self->fd >= 0) { close(self->fd); self->fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (self->fd >= 0) { close(self->fd); self->fd = -1; } 0 0 0
2402  
2403 /* Drain queues firing user callbacks with the disconnect error.
2404 connected is cleared (so GM_CROAK_UNLESS_ALIVE makes any
2405 method called from a callback croak) and magic == FREED (so a
2406 nested DESTROY no-ops at the top of this function). cancel_*
2407 would bail on FREED, so drain inline. */
2408 0 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 0 0 0
2409 0 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
2410 0 ev_gm_req_t *r = ngx_queue_data(q, ev_gm_req_t, queue);
2411 0 ngx_queue_remove(q);
2412 0 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 0 0 0
2413 0 invoke_cb2(aTHX_ self, r->cb, NULL, newSVpvs("disconnected"));
2414 0 cleanup_req(aTHX_ r);
2415 }
2416 0 ▶ while (!ngx_queue_empty(&self->active_jobs)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->active_jobs)) { 0 0 0
2417 0 ngx_queue_t *q = ngx_queue_head(&self->active_jobs);
2418 0 ev_gm_active_t *a = ngx_queue_data(q, ev_gm_active_t, queue);
2419 0 ngx_queue_remove(q);
2420 0 self->active_count_cached--;
2421 0 ▶ if (a->on_complete)
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_complete) 0 0 0
2422 0 invoke_cb2(aTHX_ self, a->on_complete, NULL, newSVpvs("disconnected"));
2423 0 cleanup_active(aTHX_ a);
2424 }
2425 0 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 0 0 0
2426 0 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
2427 0 ev_gm_wait_t *w = ngx_queue_data(q, ev_gm_wait_t, queue);
2428 0 ngx_queue_remove(q);
2429 0 ▶ if (w->req && w->req->cb)
Branch: 2 branches
BranchTrueFalseTotal
if (w->req && w->req->cb) 0 0 0
if (w->req && w->req->cb) 0 0 0
2430 0 invoke_cb2(aTHX_ self, w->req->cb, NULL, newSVpvs("disconnected"));
2431 0 cleanup_wait(aTHX_ w);
2432 }
2433 0 ▶ DRAIN_QUEUE_SILENT(&self->functions, ev_gm_func_t, cleanup_func);
Branch: 1 branch
BranchTrueFalseTotal
DRAIN_QUEUE_SILENT(&self->functions, ev_gm_func_t, cleanup_func); 0 0 0
2434 0 self->pending_count = 0;
2435 0 self->waiting_count = 0;
2436 0 self->active_count_cached = 0;
2437 0 clear_addrinfo(self);
2438 0 ▶ CLEAR_HANDLER(self->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->loop_sv); 0 0 0
2439 0 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 0 0 0
2440 0 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 0 0 0
2441 0 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 0 0 0
2442 0 ▶ CLEAR_HANDLER(self->worker_on_idle);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->worker_on_idle); 0 0 0
2443 0 ▶ CLEAR_HANDLER(self->job_session);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->job_session); 0 0 0
2444 0 Safefree(self->host); self->host = NULL;
2445 0 Safefree(self->path); self->path = NULL;
2446 0 Safefree(self->client_id);self->client_id = NULL;
2447 0 Safefree(self->rbuf); self->rbuf = NULL;
2448 0 Safefree(self->wbuf); self->wbuf = NULL;
2449 0 return;
2450 }
2451  
2452 66 stop_connect_timer(self);
2453 66 disarm_cmd_timer(self);
2454 66 stop_reconnect_timer(self);
2455 66 cleanup_connection(aTHX_ self);
2456  
2457 66 ▶ if (!PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) { 56 10 66
2458 56 self->callback_depth++;
2459 56 cancel_pending(aTHX_ self, "disconnected");
2460 56 cancel_active(aTHX_ self, "disconnected");
2461 56 cancel_waiting(aTHX_ self, "disconnected");
2462 56 self->callback_depth--;
2463 } else {
2464 10 ▶ DRAIN_QUEUE_SILENT(&self->cb_queue, ev_gm_req_t, cleanup_req);
Branch: 1 branch
BranchTrueFalseTotal
DRAIN_QUEUE_SILENT(&self->cb_queue, ev_gm_req_t, cleanup_req); 0 10 10
2465 10 ▶ DRAIN_QUEUE_SILENT(&self->wait_queue, ev_gm_wait_t, cleanup_wait);
Branch: 1 branch
BranchTrueFalseTotal
DRAIN_QUEUE_SILENT(&self->wait_queue, ev_gm_wait_t, cleanup_wait); 0 10 10
2466 10 ▶ DRAIN_QUEUE_SILENT(&self->active_jobs, ev_gm_active_t, cleanup_active);
Branch: 1 branch
BranchTrueFalseTotal
DRAIN_QUEUE_SILENT(&self->active_jobs, ev_gm_active_t, cleanup_active); 0 10 10
2467 }
2468  
2469 80 ▶ DRAIN_QUEUE_SILENT(&self->functions, ev_gm_func_t, cleanup_func);
Branch: 1 branch
BranchTrueFalseTotal
DRAIN_QUEUE_SILENT(&self->functions, ev_gm_func_t, cleanup_func); 14 66 80
2470  
2471 66 self->magic = GM_MAGIC_FREED;
2472 66 ▶ CLEAR_HANDLER(self->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->loop_sv); 1 65 66
2473 66 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 64 2 66
2474 66 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 23 43 66
2475 66 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 10 56 66
2476 66 ▶ CLEAR_HANDLER(self->worker_on_idle);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->worker_on_idle); 5 61 66
2477 /* NULL after free: with jobs outstanding the struct survives as an
2478 inert tombstone (freed by the last job's svt_free via
2479 check_destroyed), so no dangling bytes may linger in it. */
2480 66 Safefree(self->host); self->host = NULL;
2481 66 Safefree(self->path); self->path = NULL;
2482 66 Safefree(self->client_id); self->client_id = NULL;
2483 66 Safefree(self->rbuf); self->rbuf = NULL;
2484 66 Safefree(self->wbuf); self->wbuf = NULL;
2485 /* Depth is 0 in this branch, so this frees iff no job holds a
2486 tombstone reference; otherwise the last job release does. */
2487 66 (void)check_destroyed(self);
2488 }
2489  
2490 void
2491 connect(EV::Gearman self, const char *host, int port = 4730)
2492 CODE:
2493 {
2494 2 ▶ if (self->connected || self->connecting) croak("already connected");
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) croak("already connected"); 2 0 2
if (self->connected || self->connecting) croak("already connected"); 0 2 2
2495 2 stop_reconnect_timer(self);
2496 2 Safefree(self->host); self->host = savepv(host);
2497 2 self->port = port;
2498 2 Safefree(self->path); self->path = NULL;
2499 2 self->intentional_disconnect = 0;
2500 2 start_connect(aTHX_ self);
2501 }
2502  
2503 void
2504 connect_unix(EV::Gearman self, const char *path)
2505 CODE:
2506 {
2507 9 ▶ if (self->connected || self->connecting) croak("already connected");
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) croak("already connected"); 9 0 9
if (self->connected || self->connecting) croak("already connected"); 0 9 9
2508 9 stop_reconnect_timer(self);
2509 9 Safefree(self->path); self->path = savepv(path);
2510 9 Safefree(self->host); self->host = NULL;
2511 9 self->intentional_disconnect = 0;
2512 9 start_connect(aTHX_ self);
2513 }
2514  
2515 void
2516 disconnect(EV::Gearman self)
2517 CODE:
2518 {
2519 8 self->intentional_disconnect = 1;
2520 8 self->worker_active = 0;
2521 8 stop_reconnect_timer(self);
2522 8 self->callback_depth++;
2523 8 ▶ if (self->connected || self->connecting) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) { 1 7 8
if (self->connected || self->connecting) { 0 1 1
2524 7 handle_disconnect(aTHX_ self, NULL);
2525 } else {
2526 /* No cleanup_connection here, so bump explicitly: queued
2527 entries belong to the dead session and must drain, while
2528 entries submitted on a nested session from a cancel
2529 callback must survive. */
2530 1 self->conn_gen++;
2531 1 cancel_waiting(aTHX_ self, "disconnected");
2532 }
2533 8 self->callback_depth--;
2534 8 check_destroyed(self);
2535 }
2536  
2537 int
2538 is_connected(EV::Gearman self)
2539 CODE:
2540 10 ▶ RETVAL = self->connected || self->connecting;
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = self->connected || self->connecting; 5 5 10
RETVAL = self->connected || self->connecting; 0 5 5
RETVAL = self->connected || self->connecting; 0 10 10
2541 OUTPUT:
2542 RETVAL
2543  
2544 int
2545 pending_count(EV::Gearman self)
2546 CODE:
2547 5 ▶ RETVAL = self->pending_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->pending_count; 0 5 5
2548 OUTPUT:
2549 RETVAL
2550  
2551 int
2552 waiting_count(EV::Gearman self)
2553 CODE:
2554 9 ▶ RETVAL = self->waiting_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->waiting_count; 1 8 9
2555 OUTPUT:
2556 RETVAL
2557  
2558 int
2559 active_count(EV::Gearman self)
2560 CODE:
2561 4 ▶ RETVAL = self->active_count_cached;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->active_count_cached; 0 4 4
2562 OUTPUT:
2563 RETVAL
2564  
2565 # Internal: current (read, write) buffer capacities in bytes. Used by
2566 # the test suite to assert that oversized buffers are released after a
2567 # large packet drains. Not part of the public API.
2568 void
2569 _buf_caps(EV::Gearman self)
2570 PPCODE:
2571 {
2572 1 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1 1
2573 1 mPUSHu(self->rbuf_cap);
2574 1 mPUSHu(self->wbuf_cap);
2575 }
2576  
2577 # Internal: number of live EV::Gearman::Job objects holding a tombstone
2578 # reference on this connection. Used by the test suite to assert that
2579 # dispatched-then-dropped jobs accumulate nothing. Not public API.
2580 int
2581 _job_refs(EV::Gearman self)
2582 CODE:
2583 1 ▶ RETVAL = self->job_refs;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->job_refs; 0 1 1
2584 OUTPUT:
2585 RETVAL
2586  
2587 SV *
2588 on_error(EV::Gearman self, ...)
2589 CODE:
2590 12 ▶ RETVAL = handler_accessor(aTHX_ &self->on_error, items,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = handler_accessor(aTHX_ &self->on_error, items, 4 4 8
2591 4 items > 1 ? ST(1) : &PL_sv_undef);
2592 OUTPUT:
2593 RETVAL
2594  
2595 SV *
2596 on_connect(EV::Gearman self, ...)
2597 CODE:
2598 22 ▶ RETVAL = handler_accessor(aTHX_ &self->on_connect, items,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = handler_accessor(aTHX_ &self->on_connect, items, 11 0 11
2599 11 items > 1 ? ST(1) : &PL_sv_undef);
2600 OUTPUT:
2601 RETVAL
2602  
2603 SV *
2604 on_disconnect(EV::Gearman self, ...)
2605 CODE:
2606 0 ▶ RETVAL = handler_accessor(aTHX_ &self->on_disconnect, items,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = handler_accessor(aTHX_ &self->on_disconnect, items, 0 0 0
2607 0 items > 1 ? ST(1) : &PL_sv_undef);
2608 OUTPUT:
2609 RETVAL
2610  
2611 void
2612 echo(EV::Gearman self, SV *data_sv, SV *cb_sv = &PL_sv_undef)
2613 CODE:
2614 {
2615 26 ▶ GM_CROAK_UNLESS_ALIVE(self);
Branch: 4 branches
BranchTrueFalseTotal
GM_CROAK_UNLESS_ALIVE(self); 8 18 26
GM_CROAK_UNLESS_ALIVE(self); 3 5 8
GM_CROAK_UNLESS_ALIVE(self); 3 0 3
GM_CROAK_UNLESS_ALIVE(self); 0 3 3
2616 STRLEN dlen;
2617 26 const char *data = SvPV(data_sv, dlen);
2618 26 gm_check_size(aTHX_ dlen); /* parity with submit/epoch */
2619 25 ev_gm_req_t *r = alloc_req(CB_ECHO, cb_sv);
2620 25 enqueue_packet(aTHX_ self, GM_CMD_ECHO_REQ, data, dlen, r);
2621 }
2622  
2623 void
2624 _submit_internal(EV::Gearman self, int cmd_idx, SV *func_sv, SV *workload_sv, SV *unique_sv, SV *opts_sv, SV *cb_sv)
2625 CODE:
2626 {
2627 /* cmd_idx mapping: 0=submit_job, 1=high, 2=low,
2628 3=submit_job_bg, 4=high_bg, 5=low_bg */
2629 static const uint32_t cmd_map[] = {
2630 GM_CMD_SUBMIT_JOB, GM_CMD_SUBMIT_JOB_HIGH, GM_CMD_SUBMIT_JOB_LOW,
2631 GM_CMD_SUBMIT_JOB_BG, GM_CMD_SUBMIT_JOB_HIGH_BG, GM_CMD_SUBMIT_JOB_LOW_BG
2632 };
2633 11 ▶ if (cmd_idx < 0 || cmd_idx > 5) croak("invalid submit cmd index");
Branch: 2 branches
BranchTrueFalseTotal
if (cmd_idx < 0 || cmd_idx > 5) croak("invalid submit cmd index"); 11 0 11
if (cmd_idx < 0 || cmd_idx > 5) croak("invalid submit cmd index"); 0 11 11
2634 11 ▶ GM_CROAK_UNLESS_ALIVE(self);
Branch: 4 branches
BranchTrueFalseTotal
GM_CROAK_UNLESS_ALIVE(self); 6 5 11
GM_CROAK_UNLESS_ALIVE(self); 1 5 6
GM_CROAK_UNLESS_ALIVE(self); 1 0 1
GM_CROAK_UNLESS_ALIVE(self); 0 1 1
2635 11 int is_bg = (cmd_idx >= 3);
2636  
2637 11 STRLEN flen, wlen, ulen = 0;
2638 11 ▶ const char *fname = SvPV(func_sv, flen);
Branch: 1 branch
BranchTrueFalseTotal
const char *fname = SvPV(func_sv, flen); 11 0 11
2639 11 const char *wload = SvPV(workload_sv, wlen);
2640 11 const char *uniq = "";
2641 14 ▶ if (SvOK(unique_sv)) uniq = SvPV(unique_sv, ulen);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(unique_sv)) uniq = SvPV(unique_sv, ulen); 3 8 11
2642  
2643 11 gm_check_no_nul(aTHX_ fname, flen, "function name");
2644 9 gm_check_no_nul(aTHX_ uniq, ulen, "unique key");
2645  
2646 /* Validate the eventual body size (3 args + 2 NUL separators)
2647 before allocating anything, so the croak can't leak r/body. */
2648 8 gm_check_size(aTHX_ flen + ulen + wlen + 2);
2649  
2650 7 ▶ ev_gm_req_t *r = alloc_req(is_bg ? CB_SUBMIT_BG : CB_SUBMIT, cb_sv);
Branch: 1 branch
BranchTrueFalseTotal
ev_gm_req_t *r = alloc_req(is_bg ? CB_SUBMIT_BG : CB_SUBMIT, cb_sv); 3 4 7
2651  
2652 /* Optional callback bundle for foreground (opts_sv may be hashref) */
2653 7 ▶ if (!is_bg && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!is_bg && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 4 3 7
if (!is_bg && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 0 4 4
if (!is_bg && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 0 0 0
2654 0 HV *opts = (HV *)SvRV(opts_sv);
2655 SV **v;
2656 0 ▶ if ((v = hv_fetchs(opts, "on_data", 0)) && SvROK(*v)) r->on_data = newSVsv(*v);
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "on_data", 0)) && SvROK(*v)) r->on_data = newSVsv(*v); 0 0 0
if ((v = hv_fetchs(opts, "on_data", 0)) && SvROK(*v)) r->on_data = newSVsv(*v); 0 0 0
2657 0 ▶ if ((v = hv_fetchs(opts, "on_warning", 0)) && SvROK(*v)) r->on_warning = newSVsv(*v);
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "on_warning", 0)) && SvROK(*v)) r->on_warning = newSVsv(*v); 0 0 0
if ((v = hv_fetchs(opts, "on_warning", 0)) && SvROK(*v)) r->on_warning = newSVsv(*v); 0 0 0
2658 0 ▶ if ((v = hv_fetchs(opts, "on_status", 0)) && SvROK(*v)) r->on_status = newSVsv(*v);
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "on_status", 0)) && SvROK(*v)) r->on_status = newSVsv(*v); 0 0 0
if ((v = hv_fetchs(opts, "on_status", 0)) && SvROK(*v)) r->on_status = newSVsv(*v); 0 0 0
2659 0 ▶ if ((v = hv_fetchs(opts, "on_exception", 0)) && SvROK(*v)) r->on_exception = newSVsv(*v);
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(opts, "on_exception", 0)) && SvROK(*v)) r->on_exception = newSVsv(*v); 0 0 0
if ((v = hv_fetchs(opts, "on_exception", 0)) && SvROK(*v)) r->on_exception = newSVsv(*v); 0 0 0
2660 }
2661  
2662 7 const char *ptrs[3] = { fname, uniq, wload };
2663 7 STRLEN lens[3] = { flen, ulen, wlen };
2664 size_t blen;
2665 7 char *body = gm_encode_args(3, ptrs, lens, &blen);
2666 7 enqueue_packet(aTHX_ self, cmd_map[cmd_idx], body, blen, r);
2667 7 Safefree(body);
2668 }
2669  
2670 void
2671 _submit_epoch(EV::Gearman self, SV *func_sv, SV *workload_sv, SV *unique_sv, SV *epoch_sv, SV *cb_sv)
2672 CODE:
2673 {
2674 5 ▶ GM_CROAK_UNLESS_ALIVE(self);
Branch: 4 branches
BranchTrueFalseTotal
GM_CROAK_UNLESS_ALIVE(self); 3 2 5
GM_CROAK_UNLESS_ALIVE(self); 0 3 3
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
2675 5 STRLEN flen, wlen, ulen = 0;
2676 5 ▶ const char *fname = SvPV(func_sv, flen);
Branch: 1 branch
BranchTrueFalseTotal
const char *fname = SvPV(func_sv, flen); 5 0 5
2677 5 const char *wload = SvPV(workload_sv, wlen);
2678 5 const char *uniq = "";
2679 7 ▶ if (SvOK(unique_sv)) uniq = SvPV(unique_sv, ulen);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(unique_sv)) uniq = SvPV(unique_sv, ulen); 2 3 5
2680  
2681 5 gm_check_no_nul(aTHX_ fname, flen, "function name");
2682 4 gm_check_no_nul(aTHX_ uniq, ulen, "unique key");
2683  
2684 /* Negative epochs croak instead of wrapping; genuine UVs are
2685 never negative. */
2686 3 ▶ if (!SvIsUV(epoch_sv) && SvIV(epoch_sv) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvIsUV(epoch_sv) && SvIV(epoch_sv) < 0) 3 0 3
if (!SvIsUV(epoch_sv) && SvIV(epoch_sv) < 0) 0 3 3
2687 0 croak("EV::Gearman: submit_job_epoch: epoch must be non-negative");
2688 3 UV epoch = SvUV(epoch_sv);
2689  
2690 char epoch_buf[32];
2691 3 int epoch_len = snprintf(epoch_buf, sizeof(epoch_buf), "%lu", (unsigned long)epoch);
2692  
2693 /* Validate body size (4 args + 3 NUL separators) before allocating. */
2694 3 gm_check_size(aTHX_ flen + ulen + (STRLEN)epoch_len + wlen + 3);
2695  
2696 3 ev_gm_req_t *r = alloc_req(CB_SUBMIT_BG, cb_sv);
2697  
2698 /* SUBMIT_JOB_EPOCH: FUNC\0UNIQ\0EPOCH\0DATA */
2699 3 const char *ptrs[4] = { fname, uniq, epoch_buf, wload };
2700 3 STRLEN lens[4] = { flen, ulen, (STRLEN)epoch_len, wlen };
2701 size_t blen;
2702 3 char *body = gm_encode_args(4, ptrs, lens, &blen);
2703 3 enqueue_packet(aTHX_ self, GM_CMD_SUBMIT_JOB_EPOCH, body, blen, r);
2704 3 Safefree(body);
2705 }
2706  
2707 void
2708 get_status(EV::Gearman self, SV *handle_sv, SV *cb_sv = &PL_sv_undef)
2709 CODE:
2710 {
2711 0 ▶ GM_CROAK_UNLESS_ALIVE(self);
Branch: 4 branches
BranchTrueFalseTotal
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
2712 STRLEN hlen;
2713 0 const char *h = SvPV(handle_sv, hlen);
2714 0 ev_gm_req_t *r = alloc_req(CB_GET_STATUS, cb_sv);
2715 0 enqueue_packet(aTHX_ self, GM_CMD_GET_STATUS, h, hlen, r);
2716 }
2717  
2718 void
2719 get_status_unique(EV::Gearman self, SV *unique_sv, SV *cb_sv = &PL_sv_undef)
2720 CODE:
2721 {
2722 0 ▶ GM_CROAK_UNLESS_ALIVE(self);
Branch: 4 branches
BranchTrueFalseTotal
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
2723 STRLEN ulen;
2724 0 const char *u = SvPV(unique_sv, ulen);
2725 0 ev_gm_req_t *r = alloc_req(CB_GET_STATUS_UNIQ, cb_sv);
2726 0 enqueue_packet(aTHX_ self, GM_CMD_GET_STATUS_UNIQUE, u, ulen, r);
2727 }
2728  
2729 void
2730 option(EV::Gearman self, SV *name_sv, SV *cb_sv = &PL_sv_undef)
2731 CODE:
2732 {
2733 0 ▶ GM_CROAK_UNLESS_ALIVE(self);
Branch: 4 branches
BranchTrueFalseTotal
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
2734 STRLEN nlen;
2735 0 const char *n = SvPV(name_sv, nlen);
2736 /* Track common options client-side */
2737 0 ▶ if (nlen == 10 && memcmp(n, "exceptions", 10) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (nlen == 10 && memcmp(n, "exceptions", 10) == 0) 0 0 0
if (nlen == 10 && memcmp(n, "exceptions", 10) == 0) 0 0 0
2738 0 self->opt_exceptions = 1;
2739 0 ev_gm_req_t *r = alloc_req(CB_OPTION, cb_sv);
2740 0 enqueue_packet(aTHX_ self, GM_CMD_OPTION_REQ, n, nlen, r);
2741 }
2742  
2743 void
2744 set_client_id(EV::Gearman self, SV *id_sv)
2745 CODE:
2746 {
2747 STRLEN ilen;
2748 2 const char *id = SvPV(id_sv, ilen);
2749 2 gm_check_no_nul(aTHX_ id, ilen, "client id");
2750 1 Safefree(self->client_id);
2751 1 self->client_id = savepv(id);
2752 1 ▶ if (self->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) 1 0 1
2753 1 enqueue_packet(aTHX_ self, GM_CMD_SET_CLIENT_ID, id, ilen, NULL);
2754 }
2755  
2756 void
2757 can_do(EV::Gearman self, SV *func_sv, ...)
2758 CODE:
2759 {
2760 STRLEN flen;
2761 3 const char *fname = SvPV(func_sv, flen);
2762 3 gm_check_no_nul(aTHX_ fname, flen, "function name");
2763 2 int timeout = 0;
2764 2 ▶ if (items > 2 && SvOK(ST(2))) timeout = SvIV(ST(2));
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && SvOK(ST(2))) timeout = SvIV(ST(2)); 0 2 2
if (items > 2 && SvOK(ST(2))) timeout = SvIV(ST(2)); 0 0 0
2765 2 ▶ if (timeout < 0) timeout = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (timeout < 0) timeout = 0; 0 2 2
2766  
2767 /* Without a Perl handler the caller must use grab_job to receive
2768 jobs; the entry just records the ability so reconnect re-sends
2769 the CAN_DO. */
2770 2 ev_gm_func_t *existing = find_function(self, fname, flen);
2771 2 ▶ if (!existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (!existing) { 2 0 2
2772 ev_gm_func_t *f;
2773 2 Newxz(f, 1, ev_gm_func_t);
2774 2 f->name = savepv(fname);
2775 2 f->timeout = timeout;
2776 2 ngx_queue_insert_tail(&self->functions, &f->queue);
2777 } else {
2778 0 existing->timeout = timeout;
2779 }
2780 2 send_can_do(aTHX_ self, fname, flen, timeout);
2781  
2782 /* If the worker is idle (asleep after NO_JOB), gearmand won't wake us
2783 for jobs already queued under this new ability — it only NOOPs on
2784 fresh submissions. Poll once so that backlog isn't stranded; a
2785 spurious poll is harmless (NO_JOB just re-sleeps). */
2786 2 ▶ if (self->worker_active && self->worker_sleeping) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->worker_active && self->worker_sleeping) { 1 1 2
if (self->worker_active && self->worker_sleeping) { 1 0 1
2787 1 self->worker_sleeping = 0;
2788 1 worker_continue(aTHX_ self);
2789 }
2790 }
2791  
2792 void
2793 cant_do(EV::Gearman self, SV *func_sv)
2794 CODE:
2795 {
2796 STRLEN flen;
2797 0 const char *fname = SvPV(func_sv, flen);
2798 /* Remove from registered functions */
2799 ngx_queue_t *q;
2800 0 ▶ for (q = ngx_queue_head(&self->functions); q != ngx_queue_sentinel(&self->functions); ) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = ngx_queue_head(&self->functions); q != ngx_queue_sentinel(&self->functions); ) { 0 0 0
2801 0 ev_gm_func_t *f = ngx_queue_data(q, ev_gm_func_t, queue);
2802 0 ngx_queue_t *nx = ngx_queue_next(q);
2803 0 ▶ if (strlen(f->name) == flen && memcmp(f->name, fname, flen) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(f->name) == flen && memcmp(f->name, fname, flen) == 0) { 0 0 0
if (strlen(f->name) == flen && memcmp(f->name, fname, flen) == 0) { 0 0 0
2804 0 ngx_queue_remove(q);
2805 0 cleanup_func(aTHX_ f);
2806 }
2807 0 q = nx;
2808 }
2809 0 ▶ if (self->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) 0 0 0
2810 0 enqueue_packet(aTHX_ self, GM_CMD_CANT_DO, fname, flen, NULL);
2811 }
2812  
2813 void
2814 reset_abilities(EV::Gearman self)
2815 CODE:
2816 {
2817 0 ▶ DRAIN_QUEUE_SILENT(&self->functions, ev_gm_func_t, cleanup_func);
Branch: 1 branch
BranchTrueFalseTotal
DRAIN_QUEUE_SILENT(&self->functions, ev_gm_func_t, cleanup_func); 0 0 0
2818 0 ▶ if (self->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) 0 0 0
2819 0 enqueue_packet(aTHX_ self, GM_CMD_RESET_ABILITIES, NULL, 0, NULL);
2820 }
2821  
2822 void
2823 _register_function(EV::Gearman self, SV *name_sv, SV *cb_sv, int timeout, int async)
2824 CODE:
2825 {
2826 STRLEN nlen;
2827 13 const char *name = SvPV(name_sv, nlen);
2828 13 gm_check_no_nul(aTHX_ name, nlen, "function name");
2829 12 ▶ if (timeout < 0) timeout = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (timeout < 0) timeout = 0; 0 12 12
2830 12 ▶ if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 12 0 12
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 0 12 12
2831 0 croak("register_function: callback must be a coderef");
2832  
2833 12 ev_gm_func_t *existing = find_function(self, name, nlen);
2834 12 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 0 12 12
2835 0 ▶ CLEAR_HANDLER(existing->cb);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(existing->cb); 0 0 0
2836 0 existing->cb = newSVsv(cb_sv);
2837 0 existing->async = async ? 1 : 0;
2838 0 existing->timeout = timeout;
2839 0 existing->no_cb_warned = 0;
2840 } else {
2841 ev_gm_func_t *f;
2842 12 Newxz(f, 1, ev_gm_func_t);
2843 12 f->name = savepv(name);
2844 12 f->cb = newSVsv(cb_sv);
2845 12 f->async = async ? 1 : 0;
2846 12 f->timeout = timeout;
2847 12 ngx_queue_insert_tail(&self->functions, &f->queue);
2848 }
2849  
2850 12 send_can_do(aTHX_ self, name, nlen, timeout);
2851  
2852 /* Wake an idle worker to grab the new ability's backlog (see can_do). */
2853 12 ▶ if (self->worker_active && self->worker_sleeping) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->worker_active && self->worker_sleeping) { 1 11 12
if (self->worker_active && self->worker_sleeping) { 1 0 1
2854 1 self->worker_sleeping = 0;
2855 1 worker_continue(aTHX_ self);
2856 }
2857 }
2858  
2859 void
2860 work(EV::Gearman self, ...)
2861 CODE:
2862 {
2863 /* Start (or restart) the worker GRAB loop */
2864 11 self->worker_active = 1;
2865 11 self->worker_one_shot = 0;
2866 11 ▶ if (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) {
Branch: 4 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { 5 6 11
if (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { 5 0 5
if (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { 5 0 5
if (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { 5 0 5
2867 5 ▶ CLEAR_HANDLER(self->worker_on_idle);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->worker_on_idle); 0 5 5
2868 5 self->worker_on_idle = newSVsv(ST(1));
2869 }
2870 11 worker_continue(aTHX_ self);
2871 }
2872  
2873 void
2874 work_one(EV::Gearman self, SV *cb_sv = &PL_sv_undef)
2875 CODE:
2876 {
2877 2 ▶ SV *cb = (SvOK(cb_sv) && SvROK(cb_sv)) ? cb_sv : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *cb = (SvOK(cb_sv) && SvROK(cb_sv)) ? cb_sv : NULL; 0 2 2
SV *cb = (SvOK(cb_sv) && SvROK(cb_sv)) ? cb_sv : NULL; 0 0 0
2878 2 self->worker_active = 1;
2879 2 self->worker_one_shot = 1;
2880 2 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 0 2 2
2881 0 ▶ CLEAR_HANDLER(self->worker_on_idle);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->worker_on_idle); 0 0 0
2882 0 self->worker_on_idle = newSVsv(cb);
2883 }
2884 2 worker_continue(aTHX_ self);
2885 }
2886  
2887 void
2888 work_stop(EV::Gearman self)
2889 CODE:
2890 {
2891 2 self->worker_active = 0;
2892 2 self->worker_one_shot = 0;
2893 }
2894  
2895 void
2896 grab_job(EV::Gearman self, SV *cb_sv)
2897 CODE:
2898 {
2899 1 ▶ GM_CROAK_UNLESS_ALIVE(self);
Branch: 4 branches
BranchTrueFalseTotal
GM_CROAK_UNLESS_ALIVE(self); 0 1 1
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
2900 1 ▶ if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 1 0 1
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 0 1 1
2901 0 croak("grab_job: callback required");
2902 1 self->worker_grab_inflight = 1;
2903 1 ev_gm_req_t *r = alloc_req(CB_GRAB_JOB, cb_sv);
2904 1 enqueue_packet(aTHX_ self,
2905 1 ▶ self->worker_grab_uniq ? GM_CMD_GRAB_JOB_UNIQ : GM_CMD_GRAB_JOB,
Branch: 1 branch
BranchTrueFalseTotal
self->worker_grab_uniq ? GM_CMD_GRAB_JOB_UNIQ : GM_CMD_GRAB_JOB, 0 1 1
2906 NULL, 0, r);
2907 }
2908  
2909 void
2910 all_yours(EV::Gearman self)
2911 CODE:
2912 {
2913 1 ▶ if (self->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) 1 0 1
2914 1 enqueue_packet(aTHX_ self, GM_CMD_ALL_YOURS, NULL, 0, NULL);
2915 }
2916  
2917 void
2918 admin(EV::Gearman self, SV *cmd_sv, ...)
2919 CODE:
2920 {
2921 6 ▶ GM_CROAK_UNLESS_ALIVE(self);
Branch: 4 branches
BranchTrueFalseTotal
GM_CROAK_UNLESS_ALIVE(self); 3 3 6
GM_CROAK_UNLESS_ALIVE(self); 0 3 3
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
2922 STRLEN clen;
2923 6 const char *cmd = SvPV(cmd_sv, clen);
2924 6 SV *cb = NULL;
2925 6 ▶ if (items > 2 && SvOK(ST(items-1)) && SvROK(ST(items-1)) && SvTYPE(SvRV(ST(items-1))) == SVt_PVCV)
Branch: 4 branches
BranchTrueFalseTotal
if (items > 2 && SvOK(ST(items-1)) && SvROK(ST(items-1)) && SvTYPE(SvRV(ST(items-1))) == SVt_PVCV) 6 0 6
if (items > 2 && SvOK(ST(items-1)) && SvROK(ST(items-1)) && SvTYPE(SvRV(ST(items-1))) == SVt_PVCV) 6 0 6
if (items > 2 && SvOK(ST(items-1)) && SvROK(ST(items-1)) && SvTYPE(SvRV(ST(items-1))) == SVt_PVCV) 6 0 6
if (items > 2 && SvOK(ST(items-1)) && SvROK(ST(items-1)) && SvTYPE(SvRV(ST(items-1))) == SVt_PVCV) 6 0 6
2926 6 cb = ST(items-1);
2927  
2928 /* Embedded newlines would inject a second command and desync
2929 the reply framing; a single trailing newline stays legal. */
2930 {
2931 6 size_t scan = clen;
2932 6 ▶ if (scan > 0 && cmd[scan - 1] == '\n') scan--;
Branch: 2 branches
BranchTrueFalseTotal
if (scan > 0 && cmd[scan - 1] == '\n') scan--; 6 0 6
if (scan > 0 && cmd[scan - 1] == '\n') scan--; 1 5 6
2933 6 ▶ if (scan > 0 && memchr(cmd, '\n', scan))
Branch: 2 branches
BranchTrueFalseTotal
if (scan > 0 && memchr(cmd, '\n', scan)) 6 0 6
if (scan > 0 && memchr(cmd, '\n', scan)) 1 5 6
2934 1 croak("EV::Gearman: admin: command may not contain a newline");
2935 }
2936  
2937 /* Multi-line admin commands terminate with ".\n"; single-line ones
2938 don't (gearmand 1.1.21):
2939 multi-line: status, workers, prioritystatus — arguments are
2940 ignored by the server and the reply stays
2941 multi-line, so these match on the leading word;
2942 "show jobs", "show unique jobs" — no-argument
2943 forms only (with extra arguments the server
2944 answers a single-line ERR UNKNOWN_SHOW_ARGUMENTS,
2945 so they must only match exactly);
2946 single-line: version, getpid, verbose, maxqueue F N,
2947 cancel job H, create function, drop function, and
2948 anything unrecognized (a one-line ERR). */
2949 5 int multi = 0;
2950 /* Strip trailing whitespace for matching */
2951 5 size_t cl = clen;
2952 6 ▶ while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r'))
Branch: 5 branches
BranchTrueFalseTotal
while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r')) 6 0 6
while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r')) 1 5 6
while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r')) 0 5 5
while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r')) 0 5 5
while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r')) 0 5 5
2953 1 cl--;
2954 /* leading-word match with a word boundary, so "status quo" counts
2955 but "statusquo" does not */
2956 #define GM_ADMIN_WORD(c, l, w) \
2957 (l >= sizeof(w) - 1 && memcmp(c, w, sizeof(w) - 1) == 0 && \
2958 (l == sizeof(w) - 1 || c[sizeof(w) - 1] == ' ' || c[sizeof(w) - 1] == '\t'))
2959 5 ▶ multi = GM_ADMIN_WORD(cmd, cl, "status")
Branch: 4 branches
BranchTrueFalseTotal
multi = GM_ADMIN_WORD(cmd, cl, "status") 1 4 5
multi = GM_ADMIN_WORD(cmd, cl, "status") 0 1 1
multi = GM_ADMIN_WORD(cmd, cl, "status") 0 0 0
multi = GM_ADMIN_WORD(cmd, cl, "status") 0 0 0
2960 4 ▶ || GM_ADMIN_WORD(cmd, cl, "workers")
Branch: 5 branches
BranchTrueFalseTotal
|| GM_ADMIN_WORD(cmd, cl, "workers") 4 0 4
|| GM_ADMIN_WORD(cmd, cl, "workers") 0 4 4
|| GM_ADMIN_WORD(cmd, cl, "workers") 0 0 0
|| GM_ADMIN_WORD(cmd, cl, "workers") 0 0 0
|| GM_ADMIN_WORD(cmd, cl, "workers") 0 0 0
2961 4 ▶ || GM_ADMIN_WORD(cmd, cl, "prioritystatus")
Branch: 5 branches
BranchTrueFalseTotal
|| GM_ADMIN_WORD(cmd, cl, "prioritystatus") 1 3 4
|| GM_ADMIN_WORD(cmd, cl, "prioritystatus") 0 1 1
|| GM_ADMIN_WORD(cmd, cl, "prioritystatus") 0 0 0
|| GM_ADMIN_WORD(cmd, cl, "prioritystatus") 0 0 0
|| GM_ADMIN_WORD(cmd, cl, "prioritystatus") 0 0 0
2962 4 ▶ || (cl == 9 && memcmp(cmd, "show jobs", 9) == 0)
Branch: 2 branches
BranchTrueFalseTotal
|| (cl == 9 && memcmp(cmd, "show jobs", 9) == 0) 0 4 4
|| (cl == 9 && memcmp(cmd, "show jobs", 9) == 0) 0 0 0
2963 10 ▶ || (cl == 16 && memcmp(cmd, "show unique jobs", 16) == 0);
Branch: 3 branches
BranchTrueFalseTotal
|| (cl == 16 && memcmp(cmd, "show unique jobs", 16) == 0); 5 0 5
|| (cl == 16 && memcmp(cmd, "show unique jobs", 16) == 0); 0 4 4
|| (cl == 16 && memcmp(cmd, "show unique jobs", 16) == 0); 0 0 0
2964 #undef GM_ADMIN_WORD
2965  
2966 /* Build full text command (must end in \n) */
2967 char *txt;
2968 size_t txt_len;
2969 5 ▶ int has_nl = (clen > 0 && cmd[clen-1] == '\n');
Branch: 2 branches
BranchTrueFalseTotal
int has_nl = (clen > 0 && cmd[clen-1] == '\n'); 5 0 5
int has_nl = (clen > 0 && cmd[clen-1] == '\n'); 1 4 5
2970 5 txt_len = clen + (has_nl ? 0 : 1);
2971 5 Newx(txt, txt_len, char);
2972 5 memcpy(txt, cmd, clen);
2973 5 ▶ if (!has_nl) txt[txt_len - 1] = '\n';
Branch: 1 branch
BranchTrueFalseTotal
if (!has_nl) txt[txt_len - 1] = '\n'; 4 1 5
2974  
2975 5 ev_gm_req_t *r = alloc_req(CB_ADMIN, cb);
2976 5 r->admin_terminator = multi;
2977  
2978 /* submit_bytes takes ownership of txt and Safefrees it. */
2979 5 submit_bytes(aTHX_ self, txt, txt_len, r);
2980 }
2981  
2982 int
2983 connect_timeout(EV::Gearman self, ...)
2984 CODE:
2985 {
2986 1 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 1 1
2987 0 self->connect_timeout_ms = SvIV(ST(1));
2988 0 ▶ if (self->connect_timeout_ms < 0) self->connect_timeout_ms = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timeout_ms < 0) self->connect_timeout_ms = 0; 0 0 0
2989 }
2990 1 ▶ RETVAL = self->connect_timeout_ms;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->connect_timeout_ms; 0 1 1
2991 }
2992 OUTPUT:
2993 RETVAL
2994  
2995 int
2996 command_timeout(EV::Gearman self, ...)
2997 CODE:
2998 {
2999 1 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 1 1
3000 0 self->command_timeout_ms = SvIV(ST(1));
3001 0 ▶ if (self->command_timeout_ms < 0) self->command_timeout_ms = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->command_timeout_ms < 0) self->command_timeout_ms = 0; 0 0 0
3002 0 ▶ if (self->command_timeout_ms == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->command_timeout_ms == 0) 0 0 0
3003 0 disarm_cmd_timer(self);
3004 0 ▶ else if (!ngx_queue_empty(&self->cb_queue))
Branch: 1 branch
BranchTrueFalseTotal
else if (!ngx_queue_empty(&self->cb_queue)) 0 0 0
3005 0 arm_cmd_timer(self);
3006 }
3007 1 ▶ RETVAL = self->command_timeout_ms;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->command_timeout_ms; 0 1 1
3008 }
3009 OUTPUT:
3010 RETVAL
3011  
3012 void
3013 reconnect(EV::Gearman self, ...)
3014 CODE:
3015 {
3016 /* Optional args: only overwrite delay / max_attempts when the
3017 caller supplies them, so reconnect(1) re-enables without
3018 resetting the values configured at construction time. */
3019 6 ▶ if (items < 2) croak("reconnect: enable arg required");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("reconnect: enable arg required"); 1 5 6
3020 5 self->reconnect = SvTRUE(ST(1)) ? 1 : 0;
3021 5 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 5 5
3022 0 int d = SvIV(ST(2));
3023 0 self->reconnect_delay_ms = d >= 0 ? d : 0;
3024 }
3025 5 ▶ if (items > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) { 0 5 5
3026 0 int m = SvIV(ST(3));
3027 0 self->max_reconnect_attempts = m >= 0 ? m : 0;
3028 }
3029 5 ▶ if (!self->reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->reconnect) { 5 0 5
3030 5 self->reconnect_attempts = 0;
3031 5 stop_reconnect_timer(self);
3032 }
3033 }
3034  
3035 int
3036 reconnect_enabled(EV::Gearman self)
3037 CODE:
3038 0 ▶ RETVAL = self->reconnect;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->reconnect; 0 0 0
3039 OUTPUT:
3040 RETVAL
3041  
3042 int
3043 priority(EV::Gearman self, ...)
3044 CODE:
3045 {
3046 2 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 2 2
3047 0 self->priority = SvIV(ST(1));
3048 0 ▶ if (self->priority < -2) self->priority = -2;
Branch: 1 branch
BranchTrueFalseTotal
if (self->priority < -2) self->priority = -2; 0 0 0
3049 0 ▶ if (self->priority > 2) self->priority = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (self->priority > 2) self->priority = 2; 0 0 0
3050 0 ▶ if (self->reading) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reading) { 0 0 0
3051 0 ev_io_stop(self->loop, &self->rio);
3052 0 ev_set_priority(&self->rio, self->priority);
3053 0 ev_io_start(self->loop, &self->rio);
3054 } else {
3055 0 ev_set_priority(&self->rio, self->priority);
3056 }
3057 0 ▶ if (self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->writing) { 0 0 0
3058 0 ev_io_stop(self->loop, &self->wio);
3059 0 ev_set_priority(&self->wio, self->priority);
3060 0 ev_io_start(self->loop, &self->wio);
3061 } else {
3062 0 ev_set_priority(&self->wio, self->priority);
3063 }
3064 }
3065 2 ▶ RETVAL = self->priority;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->priority; 0 2 2
3066 }
3067 OUTPUT:
3068 RETVAL
3069  
3070 int
3071 keepalive(EV::Gearman self, ...)
3072 CODE:
3073 {
3074 1 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 1 1
3075 0 self->keepalive = SvIV(ST(1));
3076 0 ▶ if (self->keepalive < 0) self->keepalive = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->keepalive < 0) self->keepalive = 0; 0 0 0
3077 0 ▶ if (self->connected && self->fd >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected && self->fd >= 0) 0 0 0
if (self->connected && self->fd >= 0) 0 0 0
3078 0 apply_keepalive(self);
3079 }
3080 1 ▶ RETVAL = self->keepalive;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->keepalive; 0 1 1
3081 }
3082 OUTPUT:
3083 RETVAL
3084  
3085 # ===== Job object methods (called from Perl via $job->complete etc.) =====
3086  
3087 MODULE = EV::Gearman PACKAGE = EV::Gearman::Job
3088  
3089 void
3090 _send_event(SV *job_sv, int kind, SV *data_sv = &PL_sv_undef)
3091 CODE:
3092 {
3093 /* kind: 0=complete, 1=fail, 2=exception, 3=data, 4=warning */
3094 STRLEN hl;
3095 const char *h;
3096 12 ev_gm_t *self = job_resolve(aTHX_ job_sv, &h, &hl, "_send_event");
3097  
3098 7 STRLEN dl = 0;
3099 13 ▶ const char *dp = SvOK(data_sv) ? SvPV(data_sv, dl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *dp = SvOK(data_sv) ? SvPV(data_sv, dl) : NULL; 6 1 7
3100  
3101 static const uint32_t cmds[] = {
3102 GM_CMD_WORK_COMPLETE, GM_CMD_WORK_FAIL, GM_CMD_WORK_EXCEPTION,
3103 GM_CMD_WORK_DATA, GM_CMD_WORK_WARNING,
3104 };
3105 7 ▶ if (kind < 0 || kind >= (int)(sizeof(cmds)/sizeof(cmds[0])))
Branch: 2 branches
BranchTrueFalseTotal
if (kind < 0 || kind >= (int)(sizeof(cmds)/sizeof(cmds[0]))) 7 0 7
if (kind < 0 || kind >= (int)(sizeof(cmds)/sizeof(cmds[0]))) 0 7 7
3106 0 croak("_send_event: unknown kind %d", kind);
3107  
3108 /* WORK_FAIL takes no data; the rest take handle\0data even if data
3109 is empty (the trailing NUL is the separator, not a terminator). */
3110 13 ▶ send_work_event(aTHX_ self, cmds[kind], h, hl,
Branch: 1 branch
BranchTrueFalseTotal
send_work_event(aTHX_ self, cmds[kind], h, hl, 6 1 7
3111 6 ▶ kind == 1 ? NULL : (dp ? dp : ""), dl);
Branch: 1 branch
BranchTrueFalseTotal
kind == 1 ? NULL : (dp ? dp : ""), dl); 6 0 6
3112 }
3113  
3114 void
3115 _send_status(SV *job_sv, SV *num_sv, SV *denom_sv)
3116 CODE:
3117 {
3118 STRLEN hl;
3119 const char *h;
3120 2 ev_gm_t *self = job_resolve(aTHX_ job_sv, &h, &hl, "_send_status");
3121  
3122 1 STRLEN nl = 0, dl = 0;
3123 1 ▶ const char *n = SvOK(num_sv) ? SvPV(num_sv, nl) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *n = SvOK(num_sv) ? SvPV(num_sv, nl) : ""; 0 1 1
3124 1 ▶ const char *d = SvOK(denom_sv) ? SvPV(denom_sv, dl) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *d = SvOK(denom_sv) ? SvPV(denom_sv, dl) : ""; 0 1 1
3125 1 const char *ptrs[3] = { h, n, d };
3126 1 STRLEN lens[3] = { hl, nl, dl };
3127 size_t blen;
3128 1 char *body = gm_encode_args(3, ptrs, lens, &blen);
3129 1 enqueue_packet(aTHX_ self, GM_CMD_WORK_STATUS, body, blen, NULL);
3130 1 Safefree(body);
3131 }
« blib/lib/EV/Gearman/Job.pm ↑ Summary