Gearman.xs

filter: Statementstmt 37.7% 483 / 1280 Branchbran 20.5% 215 / 1046 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 30.0% 698 / 2326 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/EV/Gearman/Job.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5  
6 #include "EVAPI.h"
7 #include "ngx-queue.h"
8  
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
13 #include <netdb.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <arpa/inet.h>
21  
22 /* ================================================================
23 * Gearman binary protocol constants
24 *
25 * Wire format (all integers big-endian):
26 * Bytes 0-3 : magic - "\0REQ" for client->server, "\0RES" for server->client
27 * Bytes 4-7 : command (uint32 BE)
28 * Bytes 8-11: data length (uint32 BE)
29 * Bytes 12+ : data, with NUL-separated args; final arg has no terminator
30 * ================================================================ */
31  
32 #define GM_MAGIC_REQ "\0REQ"
33 #define GM_MAGIC_RES "\0RES"
34 #define GM_HEADER_SIZE 12
35  
36 #define GM_MAGIC_ALIVE 0xCA7C00FE
37 #define GM_MAGIC_FREED 0xDEAD0FF0
38  
39 /* Commands (request: client/worker -> server) */
40 #define GM_CMD_CAN_DO 1
41 #define GM_CMD_CANT_DO 2
42 #define GM_CMD_RESET_ABILITIES 3
43 #define GM_CMD_PRE_SLEEP 4
44 #define GM_CMD_NOOP 6
45 #define GM_CMD_SUBMIT_JOB 7
46 #define GM_CMD_JOB_CREATED 8
47 #define GM_CMD_GRAB_JOB 9
48 #define GM_CMD_NO_JOB 10
49 #define GM_CMD_JOB_ASSIGN 11
50 #define GM_CMD_WORK_STATUS 12
51 #define GM_CMD_WORK_COMPLETE 13
52 #define GM_CMD_WORK_FAIL 14
53 #define GM_CMD_GET_STATUS 15
54 #define GM_CMD_ECHO_REQ 16
55 #define GM_CMD_ECHO_RES 17
56 #define GM_CMD_SUBMIT_JOB_BG 18
57 #define GM_CMD_ERROR 19
58 #define GM_CMD_STATUS_RES 20
59 #define GM_CMD_SUBMIT_JOB_HIGH 21
60 #define GM_CMD_SET_CLIENT_ID 22
61 #define GM_CMD_CAN_DO_TIMEOUT 23
62 #define GM_CMD_ALL_YOURS 24
63 #define GM_CMD_WORK_EXCEPTION 25
64 #define GM_CMD_OPTION_REQ 26
65 #define GM_CMD_OPTION_RES 27
66 #define GM_CMD_WORK_DATA 28
67 #define GM_CMD_WORK_WARNING 29
68 #define GM_CMD_GRAB_JOB_UNIQ 30
69 #define GM_CMD_JOB_ASSIGN_UNIQ 31
70 #define GM_CMD_SUBMIT_JOB_HIGH_BG 32
71 #define GM_CMD_SUBMIT_JOB_LOW 33
72 #define GM_CMD_SUBMIT_JOB_LOW_BG 34
73 #define GM_CMD_SUBMIT_JOB_SCHED 35
74 #define GM_CMD_SUBMIT_JOB_EPOCH 36
75 #define GM_CMD_GET_STATUS_UNIQUE 41
76 #define GM_CMD_STATUS_RES_UNIQUE 42
77  
78 /* Internal request types (CB_*) — describe what we're waiting for */
79 #define CB_ECHO 1 /* expects ECHO_RES */
80 #define CB_SUBMIT 2 /* expects JOB_CREATED + work events */
81 #define CB_SUBMIT_BG 3 /* expects JOB_CREATED only */
82 #define CB_GET_STATUS 4 /* expects STATUS_RES */
83 #define CB_GET_STATUS_UNIQ 5 /* expects STATUS_RES_UNIQUE */
84 #define CB_OPTION 6 /* expects OPTION_RES */
85 #define CB_GRAB_JOB 7 /* expects JOB_ASSIGN | JOB_ASSIGN_UNIQ | NO_JOB */
86 #define CB_ADMIN 8 /* expects text-protocol response */
87  
88 #define BUF_INIT_SIZE 16384
89 #define GM_MAX_PACKET (256u * 1024u * 1024u) /* 256 MB sanity bound */
90 /* Once a buffer has fully drained, release it back to BUF_INIT_SIZE if
91 it grew past this — a single large packet otherwise pins its
92 high-water-mark allocation for the life of the connection. */
93 #define BUF_SHRINK_THRESHOLD (1u * 1024u * 1024u)
94  
95 #define CLEAR_HANDLER(field) \
96 do { if (NULL != (field)) { SvREFCNT_dec(field); (field) = NULL; } } while(0)
97  
98 /* Silently drain an ngx_queue, freeing each entry via its cleanup
99 function. Used during global destruction and DESTROY where invoking
100 Perl callbacks would be unsafe / pointless. */
101 #define DRAIN_QUEUE_SILENT(qhead, type, cleanup) \
102 while (!ngx_queue_empty(qhead)) { \
103 ngx_queue_t *_q = ngx_queue_head(qhead); \
104 type *_e = ngx_queue_data(_q, type, queue); \
105 ngx_queue_remove(_q); \
106 cleanup(aTHX_ _e); \
107 }
108  
109 #define GM_CROAK_UNLESS_ALIVE(self) \
110 do { \
111 if (!(self)->connected && !(self)->connecting && \
112 !((self)->reconnect && (self)->reconnect_timer_active)) \
113 croak("not connected"); \
114 } while(0)
115  
116 /* ================================================================
117 * Type declarations
118 * ================================================================ */
119  
120 typedef struct ev_gm_s ev_gm_t;
121 typedef struct ev_gm_req_s ev_gm_req_t;
122 typedef struct ev_gm_wait_s ev_gm_wait_t;
123 typedef struct ev_gm_func_s ev_gm_func_t;
124 typedef struct ev_gm_active_s ev_gm_active_t;
125  
126 typedef ev_gm_t* EV__Gearman;
127 typedef struct ev_loop* EV__Loop;
128  
129 /* Request awaiting a response (FIFO). For SUBMIT_JOB, holds the
130 * callback bundle that gets transferred to active_jobs once the
131 * JOB_CREATED handle is known. */
132 struct ev_gm_req_s {
133 ngx_queue_t queue;
134 int kind; /* CB_* */
135 SV *cb; /* primary callback (handle for BG, result for FG) */
136 SV *on_data;
137 SV *on_warning;
138 SV *on_status;
139 SV *on_exception;
140 int admin_terminator; /* 0 = single-line; 1 = ".\n" terminator (status/workers) */
141 };
142  
143 /* Pending packet awaiting connect / drain */
144 struct ev_gm_wait_s {
145 ngx_queue_t queue;
146 char *packet;
147 size_t packet_len;
148 /* if non-NULL, becomes a request entry on send */
149 ev_gm_req_t *req;
150 };
151  
152 /* Active foreground job (after JOB_CREATED) */
153 struct ev_gm_active_s {
154 ngx_queue_t queue;
155 char *handle;
156 STRLEN handle_len;
157 SV *on_complete;
158 SV *on_data;
159 SV *on_warning;
160 SV *on_status;
161 SV *on_exception;
162 };
163  
164 /* Registered worker function */
165 struct ev_gm_func_s {
166 ngx_queue_t queue;
167 char *name;
168 SV *cb;
169 int async; /* if 1, callback returns nothing; user calls $job->complete */
170 int timeout; /* seconds; 0 = no timeout */
171 };
172  
173 struct ev_gm_s {
174 unsigned int magic;
175 struct ev_loop *loop;
176 int fd;
177 int connected;
178 int connecting;
179  
180 /* IO watchers */
181 ev_io rio, wio;
182 int reading, writing;
183  
184 /* Buffers */
185 char *rbuf;
186 size_t rbuf_len, rbuf_cap;
187 char *wbuf;
188 size_t wbuf_len, wbuf_off, wbuf_cap;
189  
190 /* User-level callbacks */
191 SV *on_error;
192 SV *on_connect;
193 SV *on_disconnect;
194  
195 /* FIFO of requests awaiting a binary response */
196 ngx_queue_t cb_queue;
197 int pending_count;
198  
199 /* Pending packets awaiting transmission (e.g. before connect) */
200 ngx_queue_t wait_queue;
201 int waiting_count;
202  
203 /* Active jobs (foreground) by handle */
204 ngx_queue_t active_jobs;
205 int active_count_cached;
206  
207 /* Worker state */
208 ngx_queue_t functions;
209 int worker_active; /* in worker loop */
210 int worker_sleeping; /* PRE_SLEEP issued, awaiting NOOP */
211 int worker_grab_inflight;/* GRAB_JOB[_UNIQ] in flight */
212 int worker_grab_uniq; /* prefer GRAB_JOB_UNIQ */
213 SV *worker_on_idle; /* fires when no jobs available */
214 int worker_one_shot; /* if 1, exit worker loop after first job */
215 char *client_id; /* for SET_CLIENT_ID */
216  
217 /* Connection target */
218 char *host;
219 int port;
220 char *path;
221  
222 /* Reconnect */
223 int reconnect;
224 int reconnect_delay_ms;
225 int max_reconnect_attempts;
226 int reconnect_attempts;
227 ev_timer reconnect_timer;
228 int reconnect_timer_active;
229 int intentional_disconnect;
230  
231 /* Timeouts */
232 int connect_timeout_ms;
233 ev_timer connect_timer;
234 int connect_timer_active;
235 int command_timeout_ms;
236 ev_timer cmd_timer;
237 int cmd_timer_active;
238  
239 /* Safety */
240 int callback_depth;
241 int in_cb_cleanup;
242 int in_wait_cleanup;
243 int in_active_cleanup;
244  
245 /* Options */
246 int priority;
247 int keepalive;
248  
249 /* Options sent on connect */
250 int opt_exceptions;
251 };
252  
253 /* ================================================================
254 * Shared error strings
255 * ================================================================ */
256  
257 static SV *err_disconnected = NULL;
258  
259 /* ================================================================
260 * Forward declarations
261 * ================================================================ */
262  
263 static void io_cb(EV_P_ ev_io *w, int revents);
264 static void reconnect_timer_cb(EV_P_ ev_timer *w, int revents);
265 static void connect_timeout_cb(EV_P_ ev_timer *w, int revents);
266 static void cmd_timeout_cb(EV_P_ ev_timer *w, int revents);
267 static void start_reading(ev_gm_t *self);
268 static void stop_reading(ev_gm_t *self);
269 static void start_writing(ev_gm_t *self);
270 static void stop_writing(ev_gm_t *self);
271 static void start_connect(pTHX_ ev_gm_t *self);
272 static void cleanup_connection(pTHX_ ev_gm_t *self);
273 static void emit_error(pTHX_ ev_gm_t *self, const char *msg);
274 static void handle_disconnect(pTHX_ ev_gm_t *self, const char *reason);
275 static void schedule_reconnect(pTHX_ ev_gm_t *self);
276 static void apply_keepalive(ev_gm_t *self);
277 static void report_connect_error(pTHX_ ev_gm_t *self, const char *errbuf);
278 static void finish_connect_success(pTHX_ ev_gm_t *self);
279 static void stop_connect_timer(ev_gm_t *self);
280 static void stop_reconnect_timer(ev_gm_t *self);
281 static int check_destroyed(ev_gm_t *self);
282 static void cancel_pending(pTHX_ ev_gm_t *self, SV *err_sv);
283 static void cancel_waiting(pTHX_ ev_gm_t *self, SV *err_sv);
284 static void cancel_active(pTHX_ ev_gm_t *self, SV *err_sv);
285 static void send_pending_waits(pTHX_ ev_gm_t *self);
286 static void arm_cmd_timer(ev_gm_t *self);
287 static void disarm_cmd_timer(ev_gm_t *self);
288 static void enqueue_packet(pTHX_ ev_gm_t *self,
289 uint32_t cmd, const char *data, size_t data_len, ev_gm_req_t *req);
290 static void worker_continue(pTHX_ ev_gm_t *self);
291 static void worker_send_grab(pTHX_ ev_gm_t *self);
292 static void worker_send_pre_sleep(pTHX_ ev_gm_t *self);
293 static ev_gm_func_t* find_function(ev_gm_t *self, const char *name, STRLEN len);
294  
295 /* ================================================================
296 * Big-endian helpers (no unaligned access)
297 * ================================================================ */
298  
299 2 static void gm_write_u32(char *buf, uint32_t val) {
300 2 val = htonl(val);
301 2 memcpy(buf, &val, 4);
302 2 }
303  
304 2 static uint32_t gm_read_u32(const char *buf) {
305 uint32_t val;
306 2 memcpy(&val, buf, 4);
307 2 return ntohl(val);
308 }
309  
310 /* atoi/strtol on a length-bounded ASCII run that may not be NUL-terminated
311 (gm_arg's last field returns a pointer with no trailing NUL since the
312 separator-NUL only lives between fields). Copies up to 31 bytes onto
313 the stack first. */
314 0 static IV gm_atoi_n(const char *p, size_t len) {
315 char buf[32];
316 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
317 0 memcpy(buf, p, len);
318 0 buf[len] = '\0';
319 0 return (IV)atoi(buf);
320 }
321  
322 /* ================================================================
323 * Buffer management
324 * ================================================================ */
325  
326 1 static void buf_ensure_write(ev_gm_t *self, size_t needed) {
327 /* Compact first to reclaim already-sent prefix. */
328 1 ▶ if (self->wbuf_off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_off > 0) { 0 1 1
329 0 size_t live = self->wbuf_len - self->wbuf_off;
330 0 ▶ if (live > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (live > 0) 0 0 0
331 0 memmove(self->wbuf, self->wbuf + self->wbuf_off, live);
332 0 self->wbuf_len = live;
333 0 self->wbuf_off = 0;
334 }
335 1 size_t total = self->wbuf_len + needed;
336 1 ▶ if (self->wbuf_cap >= total) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->wbuf_cap >= total) return; 1 0 1
337 0 ▶ size_t new_cap = self->wbuf_cap ? self->wbuf_cap : BUF_INIT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = self->wbuf_cap ? self->wbuf_cap : BUF_INIT_SIZE; 0 0 0
338 0 ▶ while (new_cap < total) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < total) new_cap *= 2; 0 0 0
339 0 Renew(self->wbuf, new_cap, char);
340 0 self->wbuf_cap = new_cap;
341 }
342  
343 4 static void buf_ensure_read(ev_gm_t *self, size_t needed) {
344 4 size_t total = self->rbuf_len + needed;
345 4 ▶ if (self->rbuf_cap >= total) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_cap >= total) return; 4 0 4
346 0 ▶ size_t new_cap = self->rbuf_cap ? self->rbuf_cap : BUF_INIT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = self->rbuf_cap ? self->rbuf_cap : BUF_INIT_SIZE; 0 0 0
347 0 ▶ while (new_cap < total) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < total) new_cap *= 2; 0 0 0
348 0 Renew(self->rbuf, new_cap, char);
349 0 self->rbuf_cap = new_cap;
350 }
351  
352 1 static void buf_append_write(ev_gm_t *self, const char *data, size_t len) {
353 1 buf_ensure_write(self, len);
354 1 memcpy(self->wbuf + self->wbuf_len, data, len);
355 1 self->wbuf_len += len;
356 1 }
357  
358 /* Release an oversized, fully-drained buffer back to the initial size.
359 Caller guarantees the buffer holds no live bytes. */
360 1 static void buf_maybe_shrink(char **buf, size_t *cap) {
361 1 ▶ if (*cap > BUF_SHRINK_THRESHOLD) {
Branch: 1 branch
BranchTrueFalseTotal
if (*cap > BUF_SHRINK_THRESHOLD) { 0 1 1
362 0 Renew(*buf, BUF_INIT_SIZE, char);
363 0 *cap = BUF_INIT_SIZE;
364 }
365 1 }
366  
367 /* ================================================================
368 * Watcher helpers
369 * ================================================================ */
370  
371 5 static void start_reading(ev_gm_t *self) {
372 5 ▶ if (!self->reading && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->reading && self->fd >= 0) { 5 0 5
if (!self->reading && self->fd >= 0) { 5 0 5
373 5 ev_io_start(self->loop, &self->rio);
374 5 self->reading = 1;
375 }
376 5 }
377  
378 17 static void stop_reading(ev_gm_t *self) {
379 17 ▶ if (self->reading) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reading) { 5 12 17
380 5 ev_io_stop(self->loop, &self->rio);
381 5 self->reading = 0;
382 }
383 17 }
384  
385 10 static void start_writing(ev_gm_t *self) {
386 10 ▶ if (!self->writing && self->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!self->writing && self->fd >= 0) { 10 0 10
if (!self->writing && self->fd >= 0) { 10 0 10
387 10 ev_io_start(self->loop, &self->wio);
388 10 self->writing = 1;
389 }
390 10 }
391  
392 26 static void stop_writing(ev_gm_t *self) {
393 26 ▶ if (self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->writing) { 10 16 26
394 10 ev_io_stop(self->loop, &self->wio);
395 10 self->writing = 0;
396 }
397 26 }
398  
399 39 static int check_destroyed(ev_gm_t *self) {
400 39 ▶ if (self->magic == GM_MAGIC_FREED && self->callback_depth == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED && self->callback_depth == 0) { 0 39 39
if (self->magic == GM_MAGIC_FREED && self->callback_depth == 0) { 0 0 0
401 0 Safefree(self);
402 0 return 1;
403 }
404 39 return 0;
405 }
406  
407 /* ================================================================
408 * Callback invocation helpers
409 * ================================================================ */
410  
411 /* Invoke Perl callback with up to two args (any may be NULL = undef).
412 * Mortal SV transfer: caller passes refcnt=1 SVs, we mortalize them. */
413 1 static void invoke_cb2(pTHX_ ev_gm_t *self, SV *cb, SV *a, SV *b) {
414 1 ▶ if (!cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cb) { 0 1 1
415 0 ▶ if (a) SvREFCNT_dec(a);
Branch: 1 branch
BranchTrueFalseTotal
if (a) SvREFCNT_dec(a); 0 0 0
416 0 ▶ if (b) SvREFCNT_dec(b);
Branch: 1 branch
BranchTrueFalseTotal
if (b) SvREFCNT_dec(b); 0 0 0
417 0 return;
418 }
419 1 self->callback_depth++;
420 1 dSP;
421 1 ENTER;
422 1 SAVETMPS;
423 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
424 1 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1 1
425 1 ▶ if (a) mPUSHs(a); else PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if (a) mPUSHs(a); else PUSHs(&PL_sv_undef); 0 1 1
426 1 ▶ if (b) mPUSHs(b); else PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if (b) mPUSHs(b); else PUSHs(&PL_sv_undef); 1 0 1
427 1 PUTBACK;
428 1 call_sv(cb, G_DISCARD | G_EVAL);
429 1 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 1 0 1
if (SvTRUE(ERRSV)) { 0 1 1
430 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
431 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
432 }
433 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 0 1
434 1 LEAVE;
435 1 self->callback_depth--;
436 }
437  
438 /* Invoke a handler with at most one mortal arg; eat exceptions. */
439 20 static void invoke_handler(pTHX_ ev_gm_t *self, SV *cb, SV *arg, const char *label) {
440 20 ▶ if (!cb) { if (arg) SvREFCNT_dec(arg); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (!cb) { if (arg) SvREFCNT_dec(arg); return; } 9 11 20
if (!cb) { if (arg) SvREFCNT_dec(arg); return; } 0 9 9
441 11 self->callback_depth++;
442 11 dSP;
443 11 ENTER;
444 11 SAVETMPS;
445 11 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 11 11
446 11 ▶ if (arg) XPUSHs(sv_2mortal(arg));
Branch: 2 branches
BranchTrueFalseTotal
if (arg) XPUSHs(sv_2mortal(arg)); 10 1 11
if (arg) XPUSHs(sv_2mortal(arg)); 0 10 10
447 11 PUTBACK;
448 11 call_sv(cb, G_DISCARD | G_EVAL);
449 11 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 11 0 11
if (SvTRUE(ERRSV)) { 0 11 11
450 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
451 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
452 }
453 11 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 10 1 11
454 11 LEAVE;
455 11 self->callback_depth--;
456 }
457  
458 10 static void emit_error(pTHX_ ev_gm_t *self, const char *msg) {
459 10 invoke_handler(aTHX_ self, self->on_error, newSVpv(msg, 0), "on_error");
460 10 }
461  
462 5 static void emit_connect(pTHX_ ev_gm_t *self) {
463 5 invoke_handler(aTHX_ self, self->on_connect, NULL, "on_connect");
464 5 }
465  
466 5 static void emit_disconnect(pTHX_ ev_gm_t *self) {
467 5 invoke_handler(aTHX_ self, self->on_disconnect, NULL, "on_disconnect");
468 5 }
469  
470 5 static void apply_keepalive(ev_gm_t *self) {
471 5 ▶ if (self->keepalive <= 0 || self->path) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->keepalive <= 0 || self->path) return; 0 5 5
if (self->keepalive <= 0 || self->path) return; 0 0 0
472 0 int one = 1;
473 0 setsockopt(self->fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
474 #ifdef TCP_KEEPIDLE
475 0 setsockopt(self->fd, IPPROTO_TCP, TCP_KEEPIDLE,
476 0 &self->keepalive, sizeof(self->keepalive));
477 #endif
478 }
479  
480 /* Set O_NONBLOCK and FD_CLOEXEC on an already-open socket fd. The
481 CLOEXEC step prevents the fd leaking into child processes spawned
482 via fork+exec while the connection is up. Returns 0 on success,
483 -1 on failure (caller should close and error out). */
484 9 static int gm_set_socket_flags(int fd) {
485 9 int fl = fcntl(fd, F_GETFL);
486 9 ▶ 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; 9 0 9
if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) return -1; 0 9 9
487 #ifdef FD_CLOEXEC
488 9 fl = fcntl(fd, F_GETFD);
489 9 ▶ 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); 9 0 9
490 #endif
491 9 return 0;
492 }
493  
494 /* Shared get/set body for on_error / on_connect / on_disconnect. */
495 1 static SV* handler_accessor(pTHX_ SV **slot, int items, SV *new_cb) {
496 1 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 1 0 1
497 1 ▶ if (*slot) { SvREFCNT_dec(*slot); *slot = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (*slot) { SvREFCNT_dec(*slot); *slot = NULL; } 0 1 1
498 1 ▶ if (SvOK(new_cb) && SvROK(new_cb) && SvTYPE(SvRV(new_cb)) == SVt_PVCV)
Branch: 3 branches
BranchTrueFalseTotal
if (SvOK(new_cb) && SvROK(new_cb) && SvTYPE(SvRV(new_cb)) == SVt_PVCV) 1 0 1
if (SvOK(new_cb) && SvROK(new_cb) && SvTYPE(SvRV(new_cb)) == SVt_PVCV) 1 0 1
if (SvOK(new_cb) && SvROK(new_cb) && SvTYPE(SvRV(new_cb)) == SVt_PVCV) 1 0 1
499 1 *slot = newSVsv(new_cb);
500 }
501 1 ▶ return *slot ? newSVsv(*slot) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return *slot ? newSVsv(*slot) : &PL_sv_undef; 1 0 1
502 }
503  
504 /* ================================================================
505 * Allocators / cleaners
506 * ================================================================ */
507  
508 /* Allocate a request entry with kind set; if cb_sv is a valid coderef,
509 bumps its refcount into r->cb. Pass NULL (or a non-coderef) for no cb. */
510 1 static ev_gm_req_t* alloc_req(int kind, SV *cb_sv) {
511 ev_gm_req_t *r;
512 1 Newxz(r, 1, ev_gm_req_t);
513 1 r->kind = kind;
514 1 ▶ if (cb_sv && SvOK(cb_sv) && SvROK(cb_sv))
Branch: 3 branches
BranchTrueFalseTotal
if (cb_sv && SvOK(cb_sv) && SvROK(cb_sv)) 1 0 1
if (cb_sv && SvOK(cb_sv) && SvROK(cb_sv)) 1 0 1
if (cb_sv && SvOK(cb_sv) && SvROK(cb_sv)) 1 0 1
515 1 r->cb = newSVsv(cb_sv);
516 1 return r;
517 }
518  
519 1 static void cleanup_req(pTHX_ ev_gm_req_t *r) {
520 1 ▶ CLEAR_HANDLER(r->cb);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->cb); 1 0 1
521 1 ▶ CLEAR_HANDLER(r->on_data);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->on_data); 0 1 1
522 1 ▶ CLEAR_HANDLER(r->on_warning);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->on_warning); 0 1 1
523 1 ▶ CLEAR_HANDLER(r->on_status);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->on_status); 0 1 1
524 1 ▶ CLEAR_HANDLER(r->on_exception);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(r->on_exception); 0 1 1
525 1 Safefree(r);
526 1 }
527  
528 0 static void cleanup_wait(pTHX_ ev_gm_wait_t *w) {
529 0 Safefree(w->packet);
530 0 ▶ if (w->req) cleanup_req(aTHX_ w->req);
Branch: 1 branch
BranchTrueFalseTotal
if (w->req) cleanup_req(aTHX_ w->req); 0 0 0
531 0 Safefree(w);
532 0 }
533  
534 0 static void cleanup_active(pTHX_ ev_gm_active_t *a) {
535 0 Safefree(a->handle);
536 0 ▶ CLEAR_HANDLER(a->on_complete);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_complete); 0 0 0
537 0 ▶ CLEAR_HANDLER(a->on_data);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_data); 0 0 0
538 0 ▶ CLEAR_HANDLER(a->on_warning);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_warning); 0 0 0
539 0 ▶ CLEAR_HANDLER(a->on_status);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_status); 0 0 0
540 0 ▶ CLEAR_HANDLER(a->on_exception);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(a->on_exception); 0 0 0
541 0 Safefree(a);
542 0 }
543  
544 0 static void cleanup_func(pTHX_ ev_gm_func_t *f) {
545 0 Safefree(f->name);
546 0 ▶ CLEAR_HANDLER(f->cb);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(f->cb); 0 0 0
547 0 Safefree(f);
548 0 }
549  
550 /* ================================================================
551 * Cancel queues
552 * ================================================================ */
553  
554 /* Drain a queue, firing each entry's user callback with err_sv before
555 freeing it. Bails if a callback triggers DESTROY (magic flips to
556 FREED). The flag prevents reentrancy if a callback also calls
557 disconnect() while we are mid-loop. */
558 #define CANCEL_QUEUE(qhead, type, cleanup, counter, flag, cb_expr) do { \
559 if (self->flag) break; \
560 self->flag = 1; \
561 while (!ngx_queue_empty(qhead)) { \
562 ngx_queue_t *_q = ngx_queue_head(qhead); \
563 type *_e = ngx_queue_data(_q, type, queue); \
564 ngx_queue_remove(_q); \
565 self->counter--; \
566 if (cb_expr) \
567 invoke_cb2(aTHX_ self, cb_expr, NULL, newSVsv(err_sv)); \
568 if (self->magic == GM_MAGIC_FREED) { \
569 cleanup(aTHX_ _e); \
570 self->flag = 0; \
571 return; \
572 } \
573 cleanup(aTHX_ _e); \
574 } \
575 self->flag = 0; \
576 } while (0)
577  
578 16 static void cancel_pending(pTHX_ ev_gm_t *self, SV *err_sv) {
579 17 ▶ CANCEL_QUEUE(&self->cb_queue, ev_gm_req_t, cleanup_req,
Branch: 4 branches
BranchTrueFalseTotal
CANCEL_QUEUE(&self->cb_queue, ev_gm_req_t, cleanup_req, 0 16 16
CANCEL_QUEUE(&self->cb_queue, ev_gm_req_t, cleanup_req, 1 0 1
CANCEL_QUEUE(&self->cb_queue, ev_gm_req_t, cleanup_req, 0 1 1
CANCEL_QUEUE(&self->cb_queue, ev_gm_req_t, cleanup_req, 1 16 17
580 pending_count, in_cb_cleanup, _e->cb);
581 16 self->pending_count = 0;
582 }
583  
584 16 static void cancel_waiting(pTHX_ ev_gm_t *self, SV *err_sv) {
585 16 ▶ CANCEL_QUEUE(&self->wait_queue, ev_gm_wait_t, cleanup_wait,
Branch: 6 branches
BranchTrueFalseTotal
CANCEL_QUEUE(&self->wait_queue, ev_gm_wait_t, cleanup_wait, 0 16 16
CANCEL_QUEUE(&self->wait_queue, ev_gm_wait_t, cleanup_wait, 0 0 0
CANCEL_QUEUE(&self->wait_queue, ev_gm_wait_t, cleanup_wait, 0 0 0
CANCEL_QUEUE(&self->wait_queue, ev_gm_wait_t, cleanup_wait, 0 0 0
CANCEL_QUEUE(&self->wait_queue, ev_gm_wait_t, cleanup_wait, 0 0 0
CANCEL_QUEUE(&self->wait_queue, ev_gm_wait_t, cleanup_wait, 0 16 16
586 waiting_count, in_wait_cleanup,
587 (_e->req ? _e->req->cb : NULL));
588 16 self->waiting_count = 0;
589 }
590  
591 16 static void cancel_active(pTHX_ ev_gm_t *self, SV *err_sv) {
592 16 ▶ 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 16 16
CANCEL_QUEUE(&self->active_jobs, ev_gm_active_t, cleanup_active, 0 0 0
CANCEL_QUEUE(&self->active_jobs, ev_gm_active_t, cleanup_active, 0 0 0
CANCEL_QUEUE(&self->active_jobs, ev_gm_active_t, cleanup_active, 0 16 16
593 active_count_cached, in_active_cleanup, _e->on_complete);
594 16 self->active_count_cached = 0;
595 }
596  
597 /* ================================================================
598 * Connection cleanup / disconnect
599 * ================================================================ */
600  
601 17 static void cleanup_connection(pTHX_ ev_gm_t *self) {
602 17 stop_reading(self);
603 17 stop_writing(self);
604 17 stop_connect_timer(self);
605 17 disarm_cmd_timer(self);
606  
607 17 ▶ if (self->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->fd >= 0) { 6 11 17
608 6 close(self->fd);
609 6 self->fd = -1;
610 }
611 17 self->connected = 0;
612 17 self->connecting = 0;
613 17 self->rbuf_len = 0;
614 17 self->wbuf_len = 0;
615 17 self->wbuf_off = 0;
616  
617 /* Worker state resets: a reconnect must re-issue CAN_DO and re-start
618 the GRAB loop from scratch. Registered functions are kept. */
619 17 self->worker_sleeping = 0;
620 17 self->worker_grab_inflight = 0;
621 17 }
622  
623 5 static void handle_disconnect(pTHX_ ev_gm_t *self, const char *reason) {
624 5 int was_connected = self->connected;
625 5 cleanup_connection(aTHX_ self);
626  
627 5 cancel_pending(aTHX_ self, err_disconnected);
628 5 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 5 5
629  
630 5 cancel_active(aTHX_ self, err_disconnected);
631 5 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 5 5
632  
633 5 cancel_waiting(aTHX_ self, err_disconnected);
634 5 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 5 5
635  
636 5 ▶ if (was_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if (was_connected) { 5 0 5
637 5 emit_disconnect(aTHX_ self);
638 5 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 5 5
639 }
640  
641 5 ▶ if (reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (reason) { 5 0 5
642 5 emit_error(aTHX_ self, reason);
643 5 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 5 5
644 }
645  
646 5 ▶ if (!self->intentional_disconnect && self->reconnect)
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->reconnect) 5 0 5
if (!self->intentional_disconnect && self->reconnect) 0 5 5
647 0 schedule_reconnect(aTHX_ self);
648 }
649  
650 3 static void schedule_reconnect(pTHX_ ev_gm_t *self) {
651 3 ▶ if (self->reconnect_timer_active) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timer_active) return; 0 3 3
652 3 ▶ if (self->max_reconnect_attempts > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (self->max_reconnect_attempts > 0 && 3 0 3
653 3 ▶ self->reconnect_attempts >= self->max_reconnect_attempts) {
Branch: 1 branch
BranchTrueFalseTotal
self->reconnect_attempts >= self->max_reconnect_attempts) { 1 2 3
654 1 emit_error(aTHX_ self, "max reconnect attempts reached");
655 1 return;
656 }
657 2 self->reconnect_attempts++;
658 2 ev_tstamp delay = (ev_tstamp)self->reconnect_delay_ms / 1000.0;
659 2 ▶ if (delay < 0) delay = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (delay < 0) delay = 0; 0 2 2
660 2 ev_timer_init(&self->reconnect_timer, reconnect_timer_cb, delay, 0.0);
661 2 self->reconnect_timer.data = (void *)self;
662 2 ev_timer_start(self->loop, &self->reconnect_timer);
663 2 self->reconnect_timer_active = 1;
664 }
665  
666 37 static void stop_connect_timer(ev_gm_t *self) {
667 37 ▶ if (self->connect_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timer_active) { 0 37 37
668 0 ev_timer_stop(self->loop, &self->connect_timer);
669 0 self->connect_timer_active = 0;
670 }
671 37 }
672  
673 14 static void stop_reconnect_timer(ev_gm_t *self) {
674 14 ▶ if (self->reconnect_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_timer_active) { 0 14 14
675 0 ev_timer_stop(self->loop, &self->reconnect_timer);
676 0 self->reconnect_timer_active = 0;
677 }
678 14 }
679  
680 1 static void arm_cmd_timer(ev_gm_t *self) {
681 1 ▶ if (self->command_timeout_ms <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->command_timeout_ms <= 0) return; 0 1 1
682 1 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 1 1
683 1 ev_tstamp timeout = (ev_tstamp)self->command_timeout_ms / 1000.0;
684 1 ▶ if (self->cmd_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cmd_timer_active) { 0 1 1
685 0 self->cmd_timer.repeat = timeout;
686 0 ev_timer_again(self->loop, &self->cmd_timer);
687 } else {
688 1 ev_timer_init(&self->cmd_timer, cmd_timeout_cb, timeout, timeout);
689 1 self->cmd_timer.data = (void *)self;
690 1 ev_timer_start(self->loop, &self->cmd_timer);
691 1 self->cmd_timer_active = 1;
692 }
693 }
694  
695 30 static void disarm_cmd_timer(ev_gm_t *self) {
696 30 ▶ if (self->cmd_timer_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->cmd_timer_active) { 1 29 30
697 1 ev_timer_stop(self->loop, &self->cmd_timer);
698 1 self->cmd_timer_active = 0;
699 }
700 30 }
701  
702 1 static void cmd_timeout_cb(EV_P_ ev_timer *w, int revents) {
703 1 ev_gm_t *self = (ev_gm_t *)w->data;
704 (void)loop; (void)revents;
705 1 ▶ if (self->magic != GM_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) return; 0 1 1
706 1 ▶ if (ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) { 0 1 1
707 0 disarm_cmd_timer(self);
708 0 return;
709 }
710 1 disarm_cmd_timer(self);
711 1 self->callback_depth++;
712 1 handle_disconnect(aTHX_ self, "command timeout");
713 1 self->callback_depth--;
714 1 check_destroyed(self);
715 }
716  
717 0 static void connect_timeout_cb(EV_P_ ev_timer *w, int revents) {
718 0 ev_gm_t *self = (ev_gm_t *)w->data;
719 (void)loop; (void)revents;
720 0 ▶ if (self->magic != GM_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) return; 0 0 0
721 0 self->connect_timer_active = 0;
722 0 self->callback_depth++;
723 0 handle_disconnect(aTHX_ self, "connect timeout");
724 0 self->callback_depth--;
725 0 check_destroyed(self);
726 }
727  
728 2 static void reconnect_timer_cb(EV_P_ ev_timer *w, int revents) {
729 2 ev_gm_t *self = (ev_gm_t *)w->data;
730 (void)loop; (void)revents;
731 2 ▶ if (self->magic != GM_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) return; 0 2 2
732 2 self->reconnect_timer_active = 0;
733 2 self->callback_depth++;
734 2 start_connect(aTHX_ self);
735 2 self->callback_depth--;
736 2 check_destroyed(self);
737 }
738  
739 /* ================================================================
740 * Packet build/send
741 * ================================================================ */
742  
743 /* Pack a binary packet into a fresh buffer. Caller frees with Safefree. */
744 1 static char* gm_pack(uint32_t cmd, const char *data, size_t data_len, size_t *out_len) {
745 1 size_t total = GM_HEADER_SIZE + data_len;
746 char *p;
747 1 Newx(p, total, char);
748 1 memcpy(p, GM_MAGIC_REQ, 4);
749 1 gm_write_u32(p + 4, cmd);
750 1 gm_write_u32(p + 8, (uint32_t)data_len);
751 1 ▶ if (data_len > 0) memcpy(p + 12, data, data_len);
Branch: 1 branch
BranchTrueFalseTotal
if (data_len > 0) memcpy(p + 12, data, data_len); 1 0 1
752 1 *out_len = total;
753 1 return p;
754 }
755  
756 /* Hand `pkt` (heap-allocated, plen bytes) to the write path. If
757 * already connected, append to the write buffer and Safefree pkt;
758 * otherwise transfer ownership to a new wait entry. If req is
759 * non-NULL its ownership transfers too, into cb_queue or the wait
760 * entry respectively. */
761 1 static void submit_bytes(pTHX_ ev_gm_t *self,
762 char *pkt, size_t plen, ev_gm_req_t *req)
763 {
764 1 ▶ if (self->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) { 1 0 1
765 1 buf_append_write(self, pkt, plen);
766 1 Safefree(pkt);
767 1 ▶ if (req) {
Branch: 1 branch
BranchTrueFalseTotal
if (req) { 1 0 1
768 1 ngx_queue_insert_tail(&self->cb_queue, &req->queue);
769 1 self->pending_count++;
770 1 arm_cmd_timer(self);
771 }
772 1 start_writing(self);
773 } else {
774 ev_gm_wait_t *w;
775 0 Newxz(w, 1, ev_gm_wait_t);
776 0 w->packet = pkt;
777 0 w->packet_len = plen;
778 0 w->req = req;
779 0 ngx_queue_insert_tail(&self->wait_queue, &w->queue);
780 0 self->waiting_count++;
781 }
782 1 }
783  
784 1 static void enqueue_packet(pTHX_ ev_gm_t *self,
785 uint32_t cmd, const char *data, size_t data_len, ev_gm_req_t *req)
786 {
787 size_t plen;
788 1 char *pkt = gm_pack(cmd, data, data_len, &plen);
789 1 submit_bytes(aTHX_ self, pkt, plen, req);
790 1 }
791  
792 /* The wire length field is 32-bit and the peer caps reads at
793 GM_MAX_PACKET; reject anything larger before it would truncate and
794 desync the protocol. Callers validate at the point of entry (before
795 allocating request state) so a croak here never leaks. */
796 1 static void gm_check_size(pTHX_ size_t len) {
797 1 ▶ if (len > GM_MAX_PACKET)
Branch: 1 branch
BranchTrueFalseTotal
if (len > GM_MAX_PACKET) 1 0 1
798 1 croak("EV::Gearman: payload too large (%zu bytes, max %u)",
799 len, (unsigned)GM_MAX_PACKET);
800 0 }
801  
802 5 static void send_pending_waits(pTHX_ ev_gm_t *self) {
803 5 ▶ while (!ngx_queue_empty(&self->wait_queue) && self->connected) {
Branch: 2 branches
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue) && self->connected) { 0 5 5
while (!ngx_queue_empty(&self->wait_queue) && self->connected) { 0 0 0
804 0 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
805 0 ev_gm_wait_t *w = ngx_queue_data(q, ev_gm_wait_t, queue);
806 0 ngx_queue_remove(q);
807 0 self->waiting_count--;
808  
809 0 buf_append_write(self, w->packet, w->packet_len);
810  
811 0 ▶ if (w->req) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->req) { 0 0 0
812 0 ngx_queue_insert_tail(&self->cb_queue, &w->req->queue);
813 0 self->pending_count++;
814 0 w->req = NULL;
815 0 arm_cmd_timer(self);
816 }
817  
818 0 Safefree(w->packet); w->packet = NULL;
819 0 Safefree(w);
820  
821 0 start_writing(self);
822 }
823 5 }
824  
825 /* ================================================================
826 * Worker helpers (state machine for GRAB / NOOP / PRE_SLEEP)
827 * ================================================================ */
828  
829 0 static ev_gm_func_t* find_function(ev_gm_t *self, const char *name, STRLEN len) {
830 ngx_queue_t *q;
831 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
832 0 q = ngx_queue_next(q))
833 {
834 0 ev_gm_func_t *f = ngx_queue_data(q, ev_gm_func_t, queue);
835 0 ▶ 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) 0 0 0
if (strlen(f->name) == len && memcmp(f->name, name, len) == 0) 0 0 0
836 0 return f;
837 }
838 0 return NULL;
839 }
840  
841 0 static void worker_send_grab(pTHX_ ev_gm_t *self) {
842 0 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 0 0
843 0 ▶ if (self->worker_grab_inflight) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_grab_inflight) return; 0 0 0
844 0 ▶ if (ngx_queue_empty(&self->functions)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->functions)) return; 0 0 0
845 0 self->worker_grab_inflight = 1;
846 0 ev_gm_req_t *r = alloc_req(CB_GRAB_JOB, NULL);
847 0 enqueue_packet(aTHX_ self,
848 0 ▶ 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 0 0
849 NULL, 0, r);
850 }
851  
852 0 static void worker_send_pre_sleep(pTHX_ ev_gm_t *self) {
853 0 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 0 0
854 0 ▶ if (self->worker_sleeping) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_sleeping) return; 0 0 0
855 0 self->worker_sleeping = 1;
856 0 enqueue_packet(aTHX_ self, GM_CMD_PRE_SLEEP, NULL, 0, NULL);
857 }
858  
859 /* Drive the worker loop forward: if active and not busy, GRAB. */
860 5 static void worker_continue(pTHX_ ev_gm_t *self) {
861 5 ▶ if (!self->worker_active || !self->connected) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!self->worker_active || !self->connected) return; 0 5 5
if (!self->worker_active || !self->connected) return; 0 0 0
862 0 ▶ if (self->worker_grab_inflight || self->worker_sleeping) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->worker_grab_inflight || self->worker_sleeping) return; 0 0 0
if (self->worker_grab_inflight || self->worker_sleeping) return; 0 0 0
863 0 ▶ if (ngx_queue_empty(&self->functions)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->functions)) return; 0 0 0
864 0 worker_send_grab(aTHX_ self);
865 }
866  
867 /* ================================================================
868 * Connect-success path: re-register worker functions, drain queue
869 * ================================================================ */
870  
871 /* Send a WORK_* packet whose body is "handle" + optional "\0data".
872 For WORK_FAIL pass dp=NULL; otherwise pass payload bytes. */
873 0 static void send_work_event(pTHX_ ev_gm_t *self, uint32_t cmd,
874 const char *h, STRLEN hl, const char *dp, STRLEN dl)
875 {
876 /* No size croak here: this runs on the worker completion / event
877 path (worker_dispatch_job, $job->complete), where a longjmp would
878 unwind the EV loop mid-iteration. A >4 GiB result is not a real
879 concern (the worker would OOM building it first). */
880 0 ▶ size_t plen = dp ? hl + 1 + dl : hl;
Branch: 1 branch
BranchTrueFalseTotal
size_t plen = dp ? hl + 1 + dl : hl; 0 0 0
881 char *body;
882 0 ▶ Newx(body, plen ? plen : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(body, plen ? plen : 1, char); 0 0 0
883 0 memcpy(body, h, hl);
884 0 ▶ if (dp) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp) { 0 0 0
885 0 body[hl] = '\0';
886 0 ▶ if (dl) memcpy(body + hl + 1, dp, dl);
Branch: 1 branch
BranchTrueFalseTotal
if (dl) memcpy(body + hl + 1, dp, dl); 0 0 0
887 }
888 0 enqueue_packet(aTHX_ self, cmd, body, plen, NULL);
889 0 Safefree(body);
890 0 }
891  
892 /* Send CAN_DO[_TIMEOUT] for one function name. Builds the
893 "name\0timeout" body on the heap to avoid the 512-byte stack-buffer
894 truncation that the older inline pattern was prone to. */
895 0 static void send_can_do(pTHX_ ev_gm_t *self,
896 const char *name, STRLEN nlen, int timeout)
897 {
898 0 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 0 0
899 0 ▶ if (timeout > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (timeout > 0) { 0 0 0
900 char tbuf[16];
901 0 int tlen = snprintf(tbuf, sizeof(tbuf), "%d", timeout);
902 0 ▶ if (tlen <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (tlen <= 0) return; 0 0 0
903 0 size_t plen = nlen + 1 + (size_t)tlen;
904 char *body;
905 0 Newx(body, plen, char);
906 0 memcpy(body, name, nlen);
907 0 body[nlen] = '\0';
908 0 memcpy(body + nlen + 1, tbuf, tlen);
909 0 enqueue_packet(aTHX_ self, GM_CMD_CAN_DO_TIMEOUT, body, plen, NULL);
910 0 Safefree(body);
911 } else {
912 0 enqueue_packet(aTHX_ self, GM_CMD_CAN_DO, name, nlen, NULL);
913 }
914 }
915  
916 5 static void register_all_functions(pTHX_ ev_gm_t *self) {
917 5 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 5 5
918 ngx_queue_t *q;
919 5 ▶ 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 5 5
920 0 q = ngx_queue_next(q))
921 {
922 0 ev_gm_func_t *f = ngx_queue_data(q, ev_gm_func_t, queue);
923 0 send_can_do(aTHX_ self, f->name, strlen(f->name), f->timeout);
924 }
925 }
926  
927 5 static void send_options_and_id(pTHX_ ev_gm_t *self) {
928 5 ▶ if (self->client_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->client_id) { 0 5 5
929 0 enqueue_packet(aTHX_ self, GM_CMD_SET_CLIENT_ID,
930 0 self->client_id, strlen(self->client_id), NULL);
931 }
932 5 ▶ if (self->opt_exceptions) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->opt_exceptions) { 0 5 5
933 0 const char *opt = "exceptions";
934 0 ev_gm_req_t *r = alloc_req(CB_OPTION, NULL);
935 0 enqueue_packet(aTHX_ self, GM_CMD_OPTION_REQ, opt, strlen(opt), r);
936 }
937 5 }
938  
939 3 static void report_connect_error(pTHX_ ev_gm_t *self, const char *errbuf) {
940 3 self->callback_depth++;
941 3 emit_error(aTHX_ self, errbuf);
942 3 self->callback_depth--;
943 3 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 3 3
944 3 ▶ if (!self->intentional_disconnect && self->reconnect)
Branch: 2 branches
BranchTrueFalseTotal
if (!self->intentional_disconnect && self->reconnect) 3 0 3
if (!self->intentional_disconnect && self->reconnect) 3 0 3
945 3 schedule_reconnect(aTHX_ self);
946 }
947  
948 5 static void finish_connect_success(pTHX_ ev_gm_t *self) {
949 5 self->reconnect_attempts = 0;
950  
951 5 start_reading(self);
952 5 apply_keepalive(self);
953  
954 5 send_options_and_id(aTHX_ self);
955 5 register_all_functions(aTHX_ self);
956  
957 5 emit_connect(aTHX_ self);
958 5 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 5 5
959  
960 5 send_pending_waits(aTHX_ self);
961 5 ▶ if (check_destroyed(self)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (check_destroyed(self)) return; 0 5 5
962  
963 5 worker_continue(aTHX_ self);
964 }
965  
966 /* ================================================================
967 * Response routing
968 * ================================================================ */
969  
970 /* Return pointer to first byte of the i-th NUL-separated argument
971 * (zero-indexed) and its length in *out_len. Returns NULL if not
972 * present. The final argument is whatever remains after the last
973 * NUL — it may itself contain NUL bytes for binary data. */
974 0 static const char* gm_arg(const char *body, size_t body_len, int idx,
975 int total, size_t *out_len)
976 {
977 /* Walk through, splitting by NUL. The (total-1) NULs separate
978 total fields; the final field has no trailing NUL. */
979 0 const char *p = body;
980 0 const char *end = body + body_len;
981 0 int cur = 0;
982 0 ▶ while (cur < idx) {
Branch: 1 branch
BranchTrueFalseTotal
while (cur < idx) { 0 0 0
983 0 const char *nul = memchr(p, '\0', end - p);
984 0 ▶ if (!nul) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!nul) return NULL; 0 0 0
985 0 p = nul + 1;
986 0 cur++;
987 }
988 /* p is start of arg `idx`; find its length */
989 0 ▶ if (idx == total - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx == total - 1) { 0 0 0
990 /* Last arg runs to end */
991 0 *out_len = end - p;
992 } else {
993 0 const char *nul = memchr(p, '\0', end - p);
994 0 ▶ if (!nul) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!nul) return NULL; 0 0 0
995 0 *out_len = nul - p;
996 }
997 0 return p;
998 }
999  
1000 0 static ev_gm_active_t* find_active_by_handle(ev_gm_t *self,
1001 const char *handle, STRLEN handle_len)
1002 {
1003 ngx_queue_t *q;
1004 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
1005 0 q = ngx_queue_next(q))
1006 {
1007 0 ev_gm_active_t *a = ngx_queue_data(q, ev_gm_active_t, queue);
1008 0 ▶ if (a->handle_len == handle_len &&
Branch: 1 branch
BranchTrueFalseTotal
if (a->handle_len == handle_len && 0 0 0
1009 0 ▶ memcmp(a->handle, handle, handle_len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
memcmp(a->handle, handle, handle_len) == 0) 0 0 0
1010 0 return a;
1011 }
1012 0 return NULL;
1013 }
1014  
1015 /* Pop the head request entry; returns NULL if queue empty. */
1016 0 static ev_gm_req_t* pop_req(ev_gm_t *self) {
1017 0 ▶ if (ngx_queue_empty(&self->cb_queue)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) return NULL; 0 0 0
1018 0 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
1019 0 ev_gm_req_t *r = ngx_queue_data(q, ev_gm_req_t, queue);
1020 0 ngx_queue_remove(q);
1021 0 self->pending_count--;
1022 0 return r;
1023 }
1024  
1025 /* Forward declare so handle_response_packet can call into worker job dispatch */
1026 static void worker_dispatch_job(pTHX_ ev_gm_t *self, ev_gm_req_t *r,
1027 int with_unique, const char *body, size_t body_len);
1028  
1029 /* Validate a job hashref and return its (live) client + handle bytes.
1030 * Croaks with op-prefixed messages on any inconsistency. */
1031 0 static ev_gm_t* job_resolve(pTHX_ SV *job_sv, const char **h, STRLEN *hl,
1032 const char *op)
1033 {
1034 0 ▶ if (!SvROK(job_sv)) croak("%s: invalid job", op);
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(job_sv)) croak("%s: invalid job", op); 0 0 0
1035 0 HV *job = (HV *)SvRV(job_sv);
1036 0 SV **ptr_sv = hv_fetchs(job, "_client_ptr", 0);
1037 0 ▶ if (!ptr_sv || !SvIOK(*ptr_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!ptr_sv || !SvIOK(*ptr_sv)) 0 0 0
if (!ptr_sv || !SvIOK(*ptr_sv)) 0 0 0
1038 0 croak("%s: stale job (no client pointer)", op);
1039 0 SV **handle_sv = hv_fetchs(job, "handle", 0);
1040 0 ▶ if (!handle_sv) croak("%s: missing handle", op);
Branch: 1 branch
BranchTrueFalseTotal
if (!handle_sv) croak("%s: missing handle", op); 0 0 0
1041 0 ev_gm_t *self = INT2PTR(ev_gm_t *, SvIV(*ptr_sv));
1042 0 ▶ if (!self || self->magic != GM_MAGIC_ALIVE)
Branch: 2 branches
BranchTrueFalseTotal
if (!self || self->magic != GM_MAGIC_ALIVE) 0 0 0
if (!self || self->magic != GM_MAGIC_ALIVE) 0 0 0
1043 0 croak("%s: client destroyed", op);
1044 0 *h = SvPV(*handle_sv, *hl);
1045 0 return self;
1046 }
1047  
1048 0 static void handle_response_packet(pTHX_ ev_gm_t *self,
1049 uint32_t cmd, const char *body, size_t body_len)
1050 {
1051 0 switch (cmd) {
1052  
1053 0 case GM_CMD_ECHO_RES: {
1054 0 ev_gm_req_t *r = pop_req(self);
1055 0 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 0 0
1056 0 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 0 0 0
1057 0 invoke_cb2(aTHX_ self, r->cb, newSVpvn(body, body_len), NULL);
1058 0 cleanup_req(aTHX_ r);
1059 0 break;
1060 }
1061  
1062 0 case GM_CMD_JOB_CREATED: {
1063 /* Match against next request that expects JOB_CREATED */
1064 0 ev_gm_req_t *r = pop_req(self);
1065 0 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 0 0
1066 0 SV *handle_sv = newSVpvn(body, body_len);
1067 0 ▶ if (r->kind == CB_SUBMIT_BG) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->kind == CB_SUBMIT_BG) { 0 0 0
1068 0 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 0 0 0
1069 0 invoke_cb2(aTHX_ self, r->cb, handle_sv, NULL);
1070 else
1071 0 SvREFCNT_dec(handle_sv);
1072 0 cleanup_req(aTHX_ r);
1073 0 ▶ } else if (r->kind == CB_SUBMIT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (r->kind == CB_SUBMIT) { 0 0 0
1074 /* Foreground: register active job, transfer callback bundle */
1075 ev_gm_active_t *a;
1076 0 Newxz(a, 1, ev_gm_active_t);
1077 0 Newx(a->handle, body_len, char);
1078 0 memcpy(a->handle, body, body_len);
1079 0 a->handle_len = body_len;
1080 0 a->on_complete = r->cb; r->cb = NULL;
1081 0 a->on_data = r->on_data; r->on_data = NULL;
1082 0 a->on_warning = r->on_warning; r->on_warning = NULL;
1083 0 a->on_status = r->on_status; r->on_status = NULL;
1084 0 a->on_exception = r->on_exception;r->on_exception = NULL;
1085 0 ngx_queue_insert_tail(&self->active_jobs, &a->queue);
1086 0 self->active_count_cached++;
1087 0 cleanup_req(aTHX_ r);
1088 0 SvREFCNT_dec(handle_sv);
1089 } else {
1090 0 SvREFCNT_dec(handle_sv);
1091 0 cleanup_req(aTHX_ r);
1092 }
1093 0 break;
1094 }
1095  
1096 0 case GM_CMD_WORK_COMPLETE: {
1097 size_t hl, dl;
1098 0 const char *h = gm_arg(body, body_len, 0, 2, &hl);
1099 0 const char *d = gm_arg(body, body_len, 1, 2, &dl);
1100 0 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 0 0
1101 0 ev_gm_active_t *a = find_active_by_handle(self, h, hl);
1102 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1103 0 ngx_queue_remove(&a->queue);
1104 0 self->active_count_cached--;
1105 0 ▶ if (a->on_complete)
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_complete) 0 0 0
1106 0 ▶ invoke_cb2(aTHX_ self, a->on_complete,
Branch: 1 branch
BranchTrueFalseTotal
invoke_cb2(aTHX_ self, a->on_complete, 0 0 0
1107 0 d ? newSVpvn(d, dl) : newSVpvs(""), NULL);
1108 0 cleanup_active(aTHX_ a);
1109 0 break;
1110 }
1111  
1112 0 case GM_CMD_WORK_FAIL: {
1113 0 ev_gm_active_t *a = find_active_by_handle(self, body, body_len);
1114 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1115 0 ngx_queue_remove(&a->queue);
1116 0 self->active_count_cached--;
1117 0 ▶ if (a->on_complete)
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_complete) 0 0 0
1118 0 invoke_cb2(aTHX_ self, a->on_complete, NULL, newSVpvs("job failed"));
1119 0 cleanup_active(aTHX_ a);
1120 0 break;
1121 }
1122  
1123 0 case GM_CMD_WORK_DATA:
1124 case GM_CMD_WORK_WARNING: {
1125 size_t hl, dl;
1126 0 const char *h = gm_arg(body, body_len, 0, 2, &hl);
1127 0 const char *d = gm_arg(body, body_len, 1, 2, &dl);
1128 0 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 0 0
1129 0 ev_gm_active_t *a = find_active_by_handle(self, h, hl);
1130 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1131 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
1132 0 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 0 0 0
1133 0 ▶ SV *arg = d ? newSVpvn(d, dl) : newSVpvn("", 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *arg = d ? newSVpvn(d, dl) : newSVpvn("", 0); 0 0 0
1134 0 invoke_handler(aTHX_ self, cb, arg, "on_data/on_warning");
1135 }
1136 0 break;
1137 }
1138  
1139 0 case GM_CMD_WORK_EXCEPTION: {
1140 /* gearmand calls fail() on the job after forwarding the
1141 exception data and does NOT emit a follow-up WORK_FAIL,
1142 so this packet is the terminal event for the active job:
1143 fire on_exception (informational) then on_complete with
1144 an "exception" error so the caller's terminal callback
1145 always runs. */
1146 size_t hl, dl;
1147 0 const char *h = gm_arg(body, body_len, 0, 2, &hl);
1148 0 const char *d = gm_arg(body, body_len, 1, 2, &dl);
1149 0 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 0 0
1150 0 ev_gm_active_t *a = find_active_by_handle(self, h, hl);
1151 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1152 0 ngx_queue_remove(&a->queue);
1153 0 self->active_count_cached--;
1154 0 ▶ if (a->on_exception) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_exception) { 0 0 0
1155 0 ▶ SV *arg = d ? newSVpvn(d, dl) : newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
SV *arg = d ? newSVpvn(d, dl) : newSVpvs(""); 0 0 0
1156 0 invoke_handler(aTHX_ self, a->on_exception, arg, "on_exception");
1157 }
1158 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
1159 0 invoke_cb2(aTHX_ self, a->on_complete, NULL, newSVpvs("exception"));
1160 0 cleanup_active(aTHX_ a);
1161 0 break;
1162 }
1163  
1164 0 case GM_CMD_WORK_STATUS: {
1165 size_t hl, nl, dl;
1166 0 const char *h = gm_arg(body, body_len, 0, 3, &hl);
1167 0 const char *n = gm_arg(body, body_len, 1, 3, &nl);
1168 0 const char *d = gm_arg(body, body_len, 2, 3, &dl);
1169 0 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 0 0
1170 0 ev_gm_active_t *a = find_active_by_handle(self, h, hl);
1171 0 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 0 0
1172 0 ▶ if (a->on_status)
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_status) 0 0 0
1173 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
1174 0 n ? newSVpvn(n, nl) : newSVpvs("0"),
1175 0 d ? newSVpvn(d, dl) : newSVpvs("0"));
1176 0 break;
1177 }
1178  
1179 0 case GM_CMD_STATUS_RES:
1180 case GM_CMD_STATUS_RES_UNIQUE: {
1181 /* Both arms decode HANDLE-or-UNIQUE then KNOWN, RUNNING, NUM,
1182 DENOM, with STATUS_RES_UNIQUE adding a final CLIENT_COUNT. */
1183 0 ev_gm_req_t *r = pop_req(self);
1184 0 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 0 0
1185 0 ▶ if (r->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) { 0 0 0
1186 0 int with_count = (cmd == GM_CMD_STATUS_RES_UNIQUE);
1187 0 ▶ int total = with_count ? 6 : 5;
Branch: 1 branch
BranchTrueFalseTotal
int total = with_count ? 6 : 5; 0 0 0
1188 size_t l[6];
1189 const char *p[6];
1190 int i;
1191 0 ▶ for (i = 0; i < total; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < total; i++) 0 0 0
1192 0 p[i] = gm_arg(body, body_len, i, total, &l[i]);
1193 0 HV *hv = newHV();
1194 0 ▶ if (p[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (p[0]) { 0 0 0
1195 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
1196 0 else hv_stores(hv, "handle", newSVpvn(p[0], l[0]));
1197 }
1198 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
1199 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
1200 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
1201 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
1202 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
1203 0 hv_stores(hv, "client_count", newSViv(gm_atoi_n(p[5], l[5])));
1204 0 invoke_cb2(aTHX_ self, r->cb, newRV_noinc((SV*)hv), NULL);
1205 }
1206 0 cleanup_req(aTHX_ r);
1207 0 break;
1208 }
1209  
1210 0 case GM_CMD_OPTION_RES: {
1211 0 ev_gm_req_t *r = pop_req(self);
1212 0 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 0 0
1213 0 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 0 0 0
1214 0 invoke_cb2(aTHX_ self, r->cb, newSViv(1), NULL);
1215 0 cleanup_req(aTHX_ r);
1216 0 break;
1217 }
1218  
1219 0 case GM_CMD_NO_JOB: {
1220 0 ev_gm_req_t *r = pop_req(self);
1221 0 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 0 0
1222 0 self->worker_grab_inflight = 0;
1223 /* grab_job's caller registered a cb; fire it with "no job" so
1224 it knows to back off. The high-level work() loop never sets
1225 r->cb for its internal grabs, so this is a no-op there. */
1226 0 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 0 0 0
1227 0 invoke_cb2(aTHX_ self, r->cb, NULL, newSVpvs("no job"));
1228 0 cleanup_req(aTHX_ r);
1229 0 ▶ if (self->worker_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_active) { 0 0 0
1230 0 worker_send_pre_sleep(aTHX_ self);
1231 0 ▶ if (self->worker_on_idle)
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_on_idle) 0 0 0
1232 0 invoke_handler(aTHX_ self, self->worker_on_idle, NULL, "on_idle");
1233 }
1234 0 break;
1235 }
1236  
1237 0 case GM_CMD_JOB_ASSIGN:
1238 case GM_CMD_JOB_ASSIGN_UNIQ: {
1239 0 ev_gm_req_t *r = pop_req(self);
1240 0 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 0 0
1241 0 self->worker_grab_inflight = 0;
1242 0 worker_dispatch_job(aTHX_ self, r,
1243 cmd == GM_CMD_JOB_ASSIGN_UNIQ ? 1 : 0, body, body_len);
1244 0 cleanup_req(aTHX_ r);
1245 0 break;
1246 }
1247  
1248 0 case GM_CMD_NOOP: {
1249 0 self->worker_sleeping = 0;
1250 0 worker_continue(aTHX_ self);
1251 0 break;
1252 }
1253  
1254 0 case GM_CMD_ERROR: {
1255 /* ERRCODE\0ERR_TEXT — fail head request */
1256 size_t cl, tl;
1257 0 const char *c = gm_arg(body, body_len, 0, 2, &cl);
1258 0 const char *t = gm_arg(body, body_len, 1, 2, &tl);
1259 SV *err;
1260 0 ▶ if (c && t)
Branch: 2 branches
BranchTrueFalseTotal
if (c && t) 0 0 0
if (c && t) 0 0 0
1261 0 err = newSVpvf("%.*s: %.*s", (int)cl, c, (int)tl, t);
1262 else
1263 0 err = newSVpvn(body, body_len);
1264 0 ev_gm_req_t *r = pop_req(self);
1265 0 ▶ if (r && r->cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (r && r->cb) { 0 0 0
if (r && r->cb) { 0 0 0
1266 0 invoke_cb2(aTHX_ self, r->cb, NULL, err);
1267 } else {
1268 0 emit_error(aTHX_ self, SvPV_nolen(err));
1269 0 SvREFCNT_dec(err);
1270 }
1271 0 ▶ if (r) cleanup_req(aTHX_ r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) cleanup_req(aTHX_ r); 0 0 0
1272 0 break;
1273 }
1274  
1275 0 default:
1276 0 break;
1277 }
1278 }
1279  
1280 /* Dispatch a JOB_ASSIGN[_UNIQ]: invoke registered function callback,
1281 * deliver result back to server (for sync mode). Async mode keeps a
1282 * job object alive in Perl-land until user invokes complete/fail. */
1283 0 static void worker_dispatch_job(pTHX_ ev_gm_t *self, ev_gm_req_t *r,
1284 int with_unique, const char *body, size_t body_len)
1285 {
1286 0 ▶ int total = with_unique ? 4 : 3;
Branch: 1 branch
BranchTrueFalseTotal
int total = with_unique ? 4 : 3; 0 0 0
1287 size_t hl, fl, ul, wl;
1288 0 const char *h = gm_arg(body, body_len, 0, total, &hl);
1289 0 const char *f = gm_arg(body, body_len, 1, total, &fl);
1290 0 ▶ 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 0 0
1291 0 ▶ 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 0 0
1292  
1293 0 ▶ if (!h || !f) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || !f) return; 0 0 0
if (!h || !f) return; 0 0 0
1294  
1295 /* Build a job hashref blessed into EV::Gearman::Job. */
1296 0 HV *job = newHV();
1297 0 hv_stores(job, "handle", newSVpvn(h, hl));
1298 0 hv_stores(job, "function", newSVpvn(f, fl));
1299 0 ▶ 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 0 0
1300 0 ▶ hv_stores(job, "workload", w ? newSVpvn(w, wl) : newSVpvn("", 0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(job, "workload", w ? newSVpvn(w, wl) : newSVpvn("", 0)); 0 0 0
1301 /* Raw client pointer; we deliberately do NOT bump the client's
1302 refcount because $g is the unique T_PTROBJ owner — creating a
1303 second blessed RV would let DESTROY fire twice. Job methods
1304 check self->magic to detect a destroyed connection. */
1305 0 hv_stores(job, "_client_ptr", newSViv(PTR2IV(self)));
1306  
1307 0 SV *jobref = sv_2mortal(newRV_noinc((SV*)job));
1308 0 sv_bless(jobref, gv_stashpv("EV::Gearman::Job", GV_ADD));
1309  
1310 /* grab_job mode: caller supplied an explicit cb on the request,
1311 deliver the job to it instead of the registered-function path. */
1312 0 ▶ if (r->cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) { 0 0 0
1313 0 SvREFCNT_inc(jobref); /* invoke_cb2 mortalizes its arg */
1314 0 invoke_cb2(aTHX_ self, r->cb, jobref, NULL);
1315 0 worker_continue(aTHX_ self);
1316 0 return;
1317 }
1318  
1319 0 ev_gm_func_t *fn = find_function(self, f, fl);
1320 0 ▶ if (!fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fn) { 0 0 0
1321 0 send_work_event(aTHX_ self, GM_CMD_WORK_FAIL, h, hl, NULL, 0);
1322 0 worker_continue(aTHX_ self);
1323 0 return;
1324 }
1325  
1326 /* Snapshot fn->async before invoking the user callback: the
1327 callback is allowed to call cant_do() or reset_abilities(),
1328 which would free `fn`, leaving us with a stale pointer. The
1329 SV behind fn->cb stays alive during call_sv via Perl's own
1330 sub-context refcount, so reading it is safe. */
1331 0 int is_async = fn->async;
1332  
1333 0 self->callback_depth++;
1334 0 dSP;
1335 0 ENTER; SAVETMPS;
1336 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1337 0 ▶ XPUSHs(jobref);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(jobref); 0 0 0
1338 0 PUTBACK;
1339 0 ▶ 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)); 0 0 0
1340 0 SPAGAIN;
1341  
1342 0 SV *retval = NULL;
1343 0 ▶ int had_error = SvTRUE(ERRSV) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
int had_error = SvTRUE(ERRSV) ? 1 : 0; 0 0 0
1344 0 ▶ SV *err_sv = had_error ? newSVsv(ERRSV) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *err_sv = had_error ? newSVsv(ERRSV) : NULL; 0 0 0
SV *err_sv = had_error ? newSVsv(ERRSV) : NULL; 0 0 0
1345 0 ▶ if (had_error)
Branch: 1 branch
BranchTrueFalseTotal
if (had_error) 0 0 0
1346 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
1347  
1348 0 ▶ if (!is_async && count > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_async && count > 0) { 0 0 0
if (!is_async && count > 0) { 0 0 0
1349 0 retval = POPs;
1350 0 SvREFCNT_inc(retval);
1351 }
1352 0 PUTBACK;
1353 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
1354 0 self->callback_depth--;
1355  
1356 0 ▶ if (self->magic == GM_MAGIC_FREED) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) { 0 0 0
1357 0 ▶ if (retval) SvREFCNT_dec(retval);
Branch: 1 branch
BranchTrueFalseTotal
if (retval) SvREFCNT_dec(retval); 0 0 0
1358 0 ▶ if (err_sv) SvREFCNT_dec(err_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (err_sv) SvREFCNT_dec(err_sv); 0 0 0
1359 0 return;
1360 }
1361  
1362 0 ▶ if (!is_async) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_async) { 0 0 0
1363 0 ▶ if (had_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (had_error) { 0 0 0
1364 /* WORK_EXCEPTION is terminal at the server (gearmand calls
1365 fail() on the job after forwarding the exception data),
1366 so send it INSTEAD OF WORK_FAIL when the option is on —
1367 sending both produces a JOB_NOT_FOUND error on the
1368 second packet. */
1369 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
1370 0 STRLEN el; const char *ep = SvPV(err_sv, el);
1371 0 send_work_event(aTHX_ self, GM_CMD_WORK_EXCEPTION, h, hl, ep, el);
1372 } else {
1373 0 send_work_event(aTHX_ self, GM_CMD_WORK_FAIL, h, hl, NULL, 0);
1374 }
1375 } else {
1376 0 STRLEN dl = 0;
1377 0 const char *dp = "";
1378 0 ▶ if (retval && SvOK(retval))
Branch: 2 branches
BranchTrueFalseTotal
if (retval && SvOK(retval)) 0 0 0
if (retval && SvOK(retval)) 0 0 0
1379 0 dp = SvPV(retval, dl);
1380 0 send_work_event(aTHX_ self, GM_CMD_WORK_COMPLETE, h, hl, dp, dl);
1381 }
1382 0 ▶ } else if (had_error && err_sv) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (had_error && err_sv) { 0 0 0
} else if (had_error && err_sv) { 0 0 0
1383 /* Async: the user keeps the job around and calls complete/fail
1384 later. We immediately grab the next job so async workers can
1385 process multiple jobs concurrently — bounded only by what the
1386 server has queued. Users who want a concurrency cap call
1387 work_stop in their callback (and work() again from
1388 complete()/fail() to resume). */
1389 0 warn("EV::Gearman: async worker callback raised: %s",
1390 SvPV_nolen(err_sv));
1391 }
1392 0 ▶ if (retval) SvREFCNT_dec(retval);
Branch: 1 branch
BranchTrueFalseTotal
if (retval) SvREFCNT_dec(retval); 0 0 0
1393 0 ▶ if (err_sv) SvREFCNT_dec(err_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (err_sv) SvREFCNT_dec(err_sv); 0 0 0
1394  
1395 0 ▶ if (self->worker_one_shot) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->worker_one_shot) { 0 0 0
1396 0 self->worker_active = 0;
1397 0 self->worker_one_shot = 0;
1398 } else {
1399 0 worker_continue(aTHX_ self);
1400 }
1401 }
1402  
1403 /* ================================================================
1404 * Read path: parse binary or text-protocol responses
1405 *
1406 * The server may emit two stream styles:
1407 * - Binary packets with magic "\0RES" (response to binary requests)
1408 * - Text lines (response to admin commands like "status\n")
1409 *
1410 * We choose dispatch based on the head request kind: if head is
1411 * CB_ADMIN, we expect text; otherwise binary. If both styles are
1412 * intermixed we parse whichever the head expects.
1413 *
1414 * Admin replies: single-line commands ("version\n") return one
1415 * line; multi-line ("status\n", "workers\n") terminate with ".\n".
1416 * ================================================================ */
1417  
1418 /* Returns 1 if a complete admin reply was parsed and consumed,
1419 * 0 if more bytes are needed, -1 on protocol error (head request
1420 * is not CB_ADMIN — caller should disconnect). */
1421 1 static int parse_admin_response(pTHX_ ev_gm_t *self) {
1422 1 ▶ if (ngx_queue_empty(&self->cb_queue)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) return -1; 1 0 1
1423 0 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
1424 0 ev_gm_req_t *r = ngx_queue_data(q, ev_gm_req_t, queue);
1425 0 ▶ if (r->kind != CB_ADMIN) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (r->kind != CB_ADMIN) return -1; 0 0 0
1426  
1427 0 size_t consumed = 0;
1428 0 size_t result_len = 0;
1429 0 ▶ if (r->admin_terminator) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->admin_terminator) { 0 0 0
1430 /* Find ".\n" at start of a line. */
1431 0 const char *start = self->rbuf;
1432 0 const char *end = self->rbuf + self->rbuf_len;
1433 0 const char *p = start;
1434 0 const char *terminator = NULL;
1435 0 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 0 0 0
1436 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
1437 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
1438 0 terminator = p;
1439 0 break;
1440 }
1441 }
1442 0 p++;
1443 }
1444 0 ▶ if (!terminator) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!terminator) return 0; 0 0 0
1445 0 consumed = (terminator - start) + 2; /* include ".\n" */
1446 0 result_len = terminator - start;
1447 } else {
1448 0 const char *nl = memchr(self->rbuf, '\n', self->rbuf_len);
1449 0 ▶ if (!nl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return 0; 0 0 0
1450 0 consumed = (nl - self->rbuf) + 1;
1451 0 result_len = nl - self->rbuf;
1452 }
1453  
1454 0 ngx_queue_remove(&r->queue);
1455 0 self->pending_count--;
1456 /* invoke_cb2 dec's the SV when r->cb is NULL, so build it
1457 unconditionally and let the helper handle the no-cb case. */
1458 0 invoke_cb2(aTHX_ self, r->cb, newSVpvn(self->rbuf, result_len), NULL);
1459 0 cleanup_req(aTHX_ r);
1460  
1461 0 ▶ if (self->magic == GM_MAGIC_FREED) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return 1; 0 0 0
1462 /* The callback may have called disconnect(), which zeroes rbuf_len
1463 via cleanup_connection. Bail before the bookkeeping below would
1464 underflow it (mirrors the binary path in process_responses). */
1465 0 ▶ if (!self->connected) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return 1; 0 0 0
1466  
1467 0 self->rbuf_len -= consumed;
1468 0 ▶ if (self->rbuf_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len > 0) 0 0 0
1469 0 memmove(self->rbuf, self->rbuf + consumed, self->rbuf_len);
1470 0 return 1;
1471 }
1472  
1473 3 static void process_responses(pTHX_ ev_gm_t *self) {
1474 3 int processed = 0;
1475 3 ▶ while (self->rbuf_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->rbuf_len > 0) { 3 0 3
1476 /* Binary response packets start with "\0RES"; the text/admin
1477 protocol never emits a leading NUL, so the first byte is
1478 enough to pick a parser. */
1479 3 int is_admin = (self->rbuf[0] != '\0');
1480  
1481 3 ▶ if (is_admin) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_admin) { 1 2 3
1482 1 int rv = parse_admin_response(aTHX_ self);
1483 1 ▶ if (rv == 0) break; /* need more bytes */
Branch: 1 branch
BranchTrueFalseTotal
if (rv == 0) break; /* need more bytes */ 0 1 1
1484 1 ▶ if (rv < 0) { /* protocol error */
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { /* protocol error */ 1 0 1
1485 1 handle_disconnect(aTHX_ self,
1486 "unexpected admin response (queue head is not admin)");
1487 1 return;
1488 }
1489 0 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 0 0
1490 0 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 0 0
1491 0 processed++;
1492 0 continue;
1493 }
1494  
1495 2 ▶ if (self->rbuf_len < GM_HEADER_SIZE) break;
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < GM_HEADER_SIZE) break; 0 2 2
1496 2 ▶ if (memcmp(self->rbuf, GM_MAGIC_RES, 4) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(self->rbuf, GM_MAGIC_RES, 4) != 0) { 1 1 2
1497 1 handle_disconnect(aTHX_ self, "invalid response magic");
1498 1 return;
1499 }
1500 1 uint32_t cmd = gm_read_u32(self->rbuf + 4);
1501 1 uint32_t body_len = gm_read_u32(self->rbuf + 8);
1502 1 ▶ if (body_len > GM_MAX_PACKET) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_len > GM_MAX_PACKET) { 1 0 1
1503 1 handle_disconnect(aTHX_ self, "response packet too large");
1504 1 return;
1505 }
1506 0 size_t total = (size_t)GM_HEADER_SIZE + body_len;
1507 0 ▶ if (self->rbuf_len < total) break;
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len < total) break; 0 0 0
1508  
1509 /* rbuf is input-only — dispatch may grow wbuf but never rbuf,
1510 so the body pointer stays valid until the memmove below. */
1511 0 const char *body = self->rbuf + GM_HEADER_SIZE;
1512 0 handle_response_packet(aTHX_ self, cmd, body, body_len);
1513 0 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 0 0
1514 0 ▶ if (!self->connected) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!self->connected) return; 0 0 0
1515  
1516 0 self->rbuf_len -= total;
1517 0 ▶ if (self->rbuf_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len > 0) 0 0 0
1518 0 memmove(self->rbuf, self->rbuf + total, self->rbuf_len);
1519 0 processed++;
1520 }
1521  
1522 0 ▶ if (processed > 0 && self->cmd_timer_active) {
Branch: 2 branches
BranchTrueFalseTotal
if (processed > 0 && self->cmd_timer_active) { 0 0 0
if (processed > 0 && self->cmd_timer_active) { 0 0 0
1523 0 ▶ if (ngx_queue_empty(&self->cb_queue))
Branch: 1 branch
BranchTrueFalseTotal
if (ngx_queue_empty(&self->cb_queue)) 0 0 0
1524 0 disarm_cmd_timer(self);
1525 else
1526 0 arm_cmd_timer(self);
1527 }
1528  
1529 /* Fully consumed: release an oversized read buffer (e.g. after a
1530 single large packet) instead of pinning it for the connection's
1531 lifetime. */
1532 0 ▶ if (self->rbuf_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->rbuf_len == 0) 0 0 0
1533 0 buf_maybe_shrink(&self->rbuf, &self->rbuf_cap);
1534 }
1535  
1536 /* ================================================================
1537 * Read/write IO
1538 * ================================================================ */
1539  
1540 4 static void on_readable(pTHX_ ev_gm_t *self) {
1541 4 buf_ensure_read(self, 4096);
1542 4 ssize_t n = read(self->fd, self->rbuf + self->rbuf_len,
1543 4 self->rbuf_cap - self->rbuf_len);
1544 4 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 3 1 4
1545 3 self->rbuf_len += n;
1546 3 process_responses(aTHX_ self);
1547 1 ▶ } else if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (n == 0) { 1 0 1
1548 1 handle_disconnect(aTHX_ self, "connection closed by server");
1549 } else {
1550 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
1551 0 return;
1552 char errbuf[128];
1553 0 snprintf(errbuf, sizeof(errbuf), "read error: %s", strerror(errno));
1554 0 handle_disconnect(aTHX_ self, errbuf);
1555 }
1556 }
1557  
1558 1 static int try_write(ev_gm_t *self) {
1559 2 ▶ while (self->wbuf_off < self->wbuf_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (self->wbuf_off < self->wbuf_len) { 1 1 2
1560 1 ssize_t n = write(self->fd, self->wbuf + self->wbuf_off,
1561 1 self->wbuf_len - self->wbuf_off);
1562 1 ▶ if (n > 0) self->wbuf_off += n;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) self->wbuf_off += n; 1 0 1
1563 0 ▶ else if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (n < 0) { 0 0 0
1564 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
1565 0 return 0;
1566 0 return -1;
1567 }
1568 }
1569 1 self->wbuf_len = 0;
1570 1 self->wbuf_off = 0;
1571 1 buf_maybe_shrink(&self->wbuf, &self->wbuf_cap);
1572 1 stop_writing(self);
1573 1 return 1;
1574 }
1575  
1576 8 static void on_connect_complete(pTHX_ ev_gm_t *self) {
1577 8 int err = 0;
1578 8 socklen_t len = sizeof(err);
1579 8 ▶ 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) { 8 0 8
if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0 || err != 0) { 3 5 8
1580 char errbuf[128];
1581 3 snprintf(errbuf, sizeof(errbuf), "connect failed: %s",
1582 3 ▶ strerror(err ? err : errno));
Branch: 1 branch
BranchTrueFalseTotal
strerror(err ? err : errno)); 0 3 3
1583 3 close(self->fd);
1584 3 self->fd = -1;
1585 3 self->connecting = 0;
1586 3 stop_writing(self);
1587 3 stop_connect_timer(self);
1588 3 report_connect_error(aTHX_ self, errbuf);
1589 3 return;
1590 }
1591 5 self->connecting = 0;
1592 5 self->connected = 1;
1593 5 stop_writing(self);
1594 5 stop_connect_timer(self);
1595 5 finish_connect_success(aTHX_ self);
1596 }
1597  
1598 13 static void io_cb(EV_P_ ev_io *w, int revents) {
1599 13 ev_gm_t *self = (ev_gm_t *)w->data;
1600 (void)loop;
1601 13 ▶ if (self->magic != GM_MAGIC_ALIVE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) return; 0 13 13
1602 13 self->callback_depth++;
1603  
1604 13 ▶ if (self->connecting) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connecting) { 8 5 13
1605 8 ▶ if (revents & EV_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) { 8 0 8
1606 8 on_connect_complete(aTHX_ self);
1607 }
1608 8 self->callback_depth--;
1609 8 check_destroyed(self);
1610 8 return;
1611 }
1612  
1613 5 ▶ if (revents & EV_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_READ) { 4 1 5
1614 4 on_readable(aTHX_ self);
1615 4 ▶ if (self->magic != GM_MAGIC_ALIVE) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != GM_MAGIC_ALIVE) { 0 4 4
1616 0 self->callback_depth--;
1617 0 check_destroyed(self);
1618 0 return;
1619 }
1620 }
1621 5 ▶ if (revents & EV_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_WRITE) { 1 4 5
1622 1 int rv = try_write(self);
1623 1 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 0 1 1
1624 char errbuf[128];
1625 0 snprintf(errbuf, sizeof(errbuf), "write error: %s", strerror(errno));
1626 0 handle_disconnect(aTHX_ self, errbuf);
1627 }
1628 }
1629 5 self->callback_depth--;
1630 5 check_destroyed(self);
1631 }
1632  
1633 /* ================================================================
1634 * start_connect
1635 * ================================================================ */
1636  
1637 10 static void start_connect(pTHX_ ev_gm_t *self) {
1638 int fd, ret;
1639  
1640 10 ▶ if (self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->path) { 1 9 10
1641 struct sockaddr_un addr;
1642 1 memset(&addr, 0, sizeof(addr));
1643 1 addr.sun_family = AF_UNIX;
1644 1 ▶ if (strlen(self->path) >= sizeof(addr.sun_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(self->path) >= sizeof(addr.sun_path)) { 1 0 1
1645 1 emit_error(aTHX_ self, "unix socket path too long");
1646 1 return;
1647 }
1648 0 strncpy(addr.sun_path, self->path, sizeof(addr.sun_path) - 1);
1649  
1650 #ifdef SOCK_CLOEXEC
1651 0 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
1652 0 ▶ if (fd < 0 && errno == EINVAL) /* old kernels: fall back */
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 && errno == EINVAL) /* old kernels: fall back */ 0 0 0
if (fd < 0 && errno == EINVAL) /* old kernels: fall back */ 0 0 0
1653 0 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1654 #else
1655 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1656 #endif
1657 0 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 0 0
1658 char errbuf[128];
1659 0 snprintf(errbuf, sizeof(errbuf), "socket: %s", strerror(errno));
1660 0 emit_error(aTHX_ self, errbuf);
1661 0 return;
1662 }
1663 0 ▶ if (gm_set_socket_flags(fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gm_set_socket_flags(fd) < 0) { 0 0 0
1664 0 close(fd);
1665 0 emit_error(aTHX_ self, "fcntl set socket flags failed");
1666 0 return;
1667 }
1668 0 self->fd = fd;
1669 0 ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
1670 } else {
1671 9 struct addrinfo hints, *res = NULL;
1672 char port_str[16];
1673 9 snprintf(port_str, sizeof(port_str), "%d", self->port);
1674 9 memset(&hints, 0, sizeof(hints));
1675 9 hints.ai_family = AF_UNSPEC;
1676 9 hints.ai_socktype = SOCK_STREAM;
1677  
1678 9 ret = getaddrinfo(self->host, port_str, &hints, &res);
1679 9 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 9 9
1680 char errbuf[256];
1681 0 snprintf(errbuf, sizeof(errbuf), "getaddrinfo: %s", gai_strerror(ret));
1682 0 report_connect_error(aTHX_ self, errbuf);
1683 0 return;
1684 }
1685 #ifdef SOCK_CLOEXEC
1686 9 fd = socket(res->ai_family, res->ai_socktype | SOCK_CLOEXEC, res->ai_protocol);
1687 9 ▶ if (fd < 0 && errno == EINVAL)
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 && errno == EINVAL) 0 9 9
if (fd < 0 && errno == EINVAL) 0 0 0
1688 0 fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1689 #else
1690 fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1691 #endif
1692 9 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 9 9
1693 0 freeaddrinfo(res);
1694 char errbuf[128];
1695 0 snprintf(errbuf, sizeof(errbuf), "socket: %s", strerror(errno));
1696 0 report_connect_error(aTHX_ self, errbuf);
1697 0 return;
1698 }
1699 9 ▶ if (gm_set_socket_flags(fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gm_set_socket_flags(fd) < 0) { 0 9 9
1700 0 freeaddrinfo(res);
1701 0 close(fd);
1702 0 emit_error(aTHX_ self, "fcntl set socket flags failed");
1703 0 return;
1704 }
1705 9 int one = 1;
1706 9 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
1707 9 self->fd = fd;
1708 9 ret = connect(fd, res->ai_addr, res->ai_addrlen);
1709 9 freeaddrinfo(res);
1710 }
1711  
1712 /* Watcher init is the same for the immediate-success and
1713 EINPROGRESS branches; only ev_io_start (via start_*) differs.
1714 Init'd-but-not-started watchers are inert, so it is safe to
1715 run this even in the failure branch which closes the fd. */
1716 9 ev_io_init(&self->rio, io_cb, self->fd, EV_READ);
1717 9 self->rio.data = (void *)self;
1718 9 ev_io_init(&self->wio, io_cb, self->fd, EV_WRITE);
1719 9 self->wio.data = (void *)self;
1720 9 ev_set_priority(&self->rio, self->priority);
1721 9 ev_set_priority(&self->wio, self->priority);
1722  
1723 9 ▶ if (ret == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) { 0 9 9
1724 0 self->connected = 1;
1725 0 finish_connect_success(aTHX_ self);
1726 0 return;
1727 }
1728 9 ▶ if (errno != EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno != EINPROGRESS) { 0 9 9
1729 char errbuf[128];
1730 0 snprintf(errbuf, sizeof(errbuf), "connect: %s", strerror(errno));
1731 0 close(self->fd);
1732 0 self->fd = -1;
1733 0 report_connect_error(aTHX_ self, errbuf);
1734 0 return;
1735 }
1736 9 self->connecting = 1;
1737 9 start_writing(self);
1738  
1739 9 ▶ if (self->connect_timeout_ms > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_timeout_ms > 0) { 0 9 9
1740 0 ev_tstamp delay = (ev_tstamp)self->connect_timeout_ms / 1000.0;
1741 0 ev_timer_init(&self->connect_timer, connect_timeout_cb, delay, 0.0);
1742 0 self->connect_timer.data = (void *)self;
1743 0 ev_timer_start(self->loop, &self->connect_timer);
1744 0 self->connect_timer_active = 1;
1745 }
1746 }
1747  
1748 /* ================================================================
1749 * Helper to encode a multi-arg body: NUL-separated.
1750 * ================================================================ */
1751  
1752 /* Concatenate args with NUL separators into out (caller frees).
1753 * args: array of {ptr, len} pairs; n_args entries.
1754 * Returns total length in *out_len. NULL ptr is treated as empty. */
1755 0 static char* gm_encode_args(int n_args, const char **ptrs, const STRLEN *lens, size_t *out_len) {
1756 0 size_t total = 0;
1757 int i;
1758 0 ▶ 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); 0 0 0
for (i = 0; i < n_args; i++) total += (ptrs[i] ? lens[i] : 0); 0 0 0
1759 0 ▶ if (n_args > 1) total += (n_args - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (n_args > 1) total += (n_args - 1); 0 0 0
1760 char *out;
1761 0 ▶ Newx(out, total ? total : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(out, total ? total : 1, char); 0 0 0
1762 0 char *p = out;
1763 0 ▶ for (i = 0; i < n_args; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_args; i++) { 0 0 0
1764 0 ▶ if (i > 0) { *p++ = '\0'; }
Branch: 1 branch
BranchTrueFalseTotal
if (i > 0) { *p++ = '\0'; } 0 0 0
1765 0 ▶ if (ptrs[i] && lens[i] > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptrs[i] && lens[i] > 0) { 0 0 0
if (ptrs[i] && lens[i] > 0) { 0 0 0
1766 0 memcpy(p, ptrs[i], lens[i]);
1767 0 p += lens[i];
1768 }
1769 }
1770 0 *out_len = total;
1771 0 return out;
1772 }
1773  
1774 /* ================================================================
1775 * XS interface
1776 * ================================================================ */
1777  
1778 MODULE = EV::Gearman PACKAGE = EV::Gearman
1779  
1780 BOOT:
1781 {
1782 20 ▶ I_EV_API("EV::Gearman");
Branch: 3 branches
BranchTrueFalseTotal
I_EV_API("EV::Gearman"); 0 20 20
I_EV_API("EV::Gearman"); 20 0 20
I_EV_API("EV::Gearman"); 0 20 20
1783 20 err_disconnected = newSVpvs("disconnected");
1784 20 SvREADONLY_on(err_disconnected);
1785 }
1786  
1787 EV::Gearman
1788 new(char *class, ...)
1789 CODE:
1790 {
1791 PERL_UNUSED_VAR(class);
1792 14 ▶ 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 13 14
1793  
1794 13 Newxz(RETVAL, 1, ev_gm_t);
1795 13 RETVAL->magic = GM_MAGIC_ALIVE;
1796 13 RETVAL->fd = -1;
1797 13 RETVAL->port = 4730;
1798 13 ngx_queue_init(&RETVAL->cb_queue);
1799 13 ngx_queue_init(&RETVAL->wait_queue);
1800 13 ngx_queue_init(&RETVAL->active_jobs);
1801 13 ngx_queue_init(&RETVAL->functions);
1802 13 Newx(RETVAL->rbuf, BUF_INIT_SIZE, char);
1803 13 RETVAL->rbuf_cap = BUF_INIT_SIZE;
1804 13 Newx(RETVAL->wbuf, BUF_INIT_SIZE, char);
1805 13 RETVAL->wbuf_cap = BUF_INIT_SIZE;
1806  
1807 /* Default error handler. eval_pv hands back the result without
1808 transferring a reference we own (it stays mortal), so take one
1809 to keep the sub alive past the current statement. */
1810 13 RETVAL->on_error = eval_pv("sub { warn \"EV::Gearman error: @_\\n\" }", TRUE);
1811 13 SvREFCNT_inc_simple_void_NN(RETVAL->on_error);
1812  
1813 13 SV *host_sv = NULL, *path_sv = NULL;
1814 13 int port = 4730;
1815 13 int do_reconnect = 0, reconnect_delay = 1000, max_reconnect_attempts = 0;
1816 13 RETVAL->loop = EV_DEFAULT;
1817  
1818 int i;
1819 40 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 27 13 40
1820 27 const char *k = SvPV_nolen(ST(i));
1821 27 SV *v = ST(i + 1);
1822  
1823 27 ▶ if (strEQ(k, "host")) host_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "host")) host_sv = v; 8 19 27
1824 19 ▶ else if (strEQ(k, "port")) port = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "port")) port = SvIV(v); 7 12 19
1825 12 ▶ else if (strEQ(k, "path")) path_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "path")) path_sv = v; 1 11 12
1826 11 ▶ else if (strEQ(k, "on_error")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "on_error")) { 7 4 11
1827 7 ▶ CLEAR_HANDLER(RETVAL->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_error); 7 0 7
1828 7 ▶ if (SvOK(v) && SvROK(v)) RETVAL->on_error = newSVsv(v);
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(v) && SvROK(v)) RETVAL->on_error = newSVsv(v); 7 0 7
if (SvOK(v) && SvROK(v)) RETVAL->on_error = newSVsv(v); 7 0 7
1829 }
1830 4 ▶ else if (strEQ(k, "on_connect")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "on_connect")) { 0 4 4
1831 0 ▶ if (SvOK(v) && SvROK(v)) RETVAL->on_connect = newSVsv(v);
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(v) && SvROK(v)) RETVAL->on_connect = newSVsv(v); 0 0 0
if (SvOK(v) && SvROK(v)) RETVAL->on_connect = newSVsv(v); 0 0 0
1832 }
1833 4 ▶ else if (strEQ(k, "on_disconnect")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "on_disconnect")) { 0 4 4
1834 0 ▶ if (SvOK(v) && SvROK(v)) RETVAL->on_disconnect = newSVsv(v);
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(v) && SvROK(v)) RETVAL->on_disconnect = newSVsv(v); 0 0 0
if (SvOK(v) && SvROK(v)) RETVAL->on_disconnect = newSVsv(v); 0 0 0
1835 }
1836 4 ▶ else if (strEQ(k, "connect_timeout")) RETVAL->connect_timeout_ms = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "connect_timeout")) RETVAL->connect_timeout_ms = SvIV(v); 0 4 4
1837 4 ▶ else if (strEQ(k, "command_timeout")) RETVAL->command_timeout_ms = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "command_timeout")) RETVAL->command_timeout_ms = SvIV(v); 1 3 4
1838 3 ▶ else if (strEQ(k, "priority")) RETVAL->priority = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "priority")) RETVAL->priority = SvIV(v); 0 3 3
1839 3 ▶ else if (strEQ(k, "keepalive")) RETVAL->keepalive = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "keepalive")) RETVAL->keepalive = SvIV(v); 0 3 3
1840 3 ▶ else if (strEQ(k, "reconnect")) do_reconnect = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "reconnect")) do_reconnect = SvTRUE(v) ? 1 : 0; 1 2 3
1841 2 ▶ else if (strEQ(k, "reconnect_delay")) reconnect_delay = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "reconnect_delay")) reconnect_delay = SvIV(v); 1 1 2
1842 1 ▶ else if (strEQ(k, "max_reconnect_attempts")) max_reconnect_attempts = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "max_reconnect_attempts")) max_reconnect_attempts = SvIV(v); 1 0 1
1843 0 ▶ 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; 0 0 0
1844 0 ▶ else if (strEQ(k, "client_id")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "client_id")) { 0 0 0
1845 0 ▶ if (SvOK(v)) RETVAL->client_id = savepv(SvPV_nolen(v));
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) RETVAL->client_id = savepv(SvPV_nolen(v)); 0 0 0
1846 }
1847 0 ▶ 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; 0 0 0
1848 0 ▶ else if (strEQ(k, "loop")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "loop")) { 0 0 0
1849 0 RETVAL->loop = (struct ev_loop *)SvPVX(SvRV(v));
1850 }
1851 }
1852  
1853 13 ▶ if (host_sv && path_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (host_sv && path_sv) { 8 5 13
if (host_sv && path_sv) { 1 7 8
1854 1 Safefree(RETVAL->rbuf);
1855 1 Safefree(RETVAL->wbuf);
1856 1 ▶ CLEAR_HANDLER(RETVAL->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_error); 1 0 1
1857 1 ▶ CLEAR_HANDLER(RETVAL->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_connect); 0 1 1
1858 1 ▶ CLEAR_HANDLER(RETVAL->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(RETVAL->on_disconnect); 0 1 1
1859 1 ▶ if (RETVAL->client_id) Safefree(RETVAL->client_id);
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL->client_id) Safefree(RETVAL->client_id); 0 1 1
1860 1 Safefree(RETVAL);
1861 1 croak("cannot specify both 'host' and 'path'");
1862 }
1863  
1864 12 RETVAL->port = port;
1865 12 ▶ if (do_reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (do_reconnect) { 1 11 12
1866 1 RETVAL->reconnect = 1;
1867 1 RETVAL->reconnect_delay_ms = reconnect_delay >= 0 ? reconnect_delay : 0;
1868 1 RETVAL->max_reconnect_attempts = max_reconnect_attempts >= 0 ? max_reconnect_attempts : 0;
1869 }
1870  
1871 12 ▶ if (host_sv && SvOK(host_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (host_sv && SvOK(host_sv)) { 7 5 12
if (host_sv && SvOK(host_sv)) { 7 0 7
1872 7 RETVAL->host = savepv(SvPV_nolen(host_sv));
1873 7 start_connect(aTHX_ RETVAL);
1874 5 ▶ } else if (path_sv && SvOK(path_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (path_sv && SvOK(path_sv)) { 0 5 5
} else if (path_sv && SvOK(path_sv)) { 0 0 0
1875 0 RETVAL->path = savepv(SvPV_nolen(path_sv));
1876 0 start_connect(aTHX_ RETVAL);
1877 }
1878 }
1879 OUTPUT:
1880 RETVAL
1881  
1882 void
1883 DESTROY(EV::Gearman self)
1884 CODE:
1885 {
1886 12 ▶ if (self->magic == GM_MAGIC_FREED) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic == GM_MAGIC_FREED) return; 0 12 12
1887  
1888 12 ▶ if (self->callback_depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->callback_depth > 0) { 0 12 12
1889 0 self->magic = GM_MAGIC_FREED;
1890 0 self->connected = 0;
1891 0 self->connecting = 0;
1892 0 stop_reading(self);
1893 0 stop_writing(self);
1894 0 stop_connect_timer(self);
1895 0 disarm_cmd_timer(self);
1896 0 stop_reconnect_timer(self);
1897 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
1898  
1899 /* Drain queues firing user callbacks with the disconnect error.
1900 connected is cleared (so GM_CROAK_UNLESS_ALIVE makes any
1901 method called from a callback croak) and magic == FREED (so a
1902 nested DESTROY no-ops at the top of this function). cancel_*
1903 would bail on FREED, so drain inline. */
1904 0 ▶ while (!ngx_queue_empty(&self->cb_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->cb_queue)) { 0 0 0
1905 0 ngx_queue_t *q = ngx_queue_head(&self->cb_queue);
1906 0 ev_gm_req_t *r = ngx_queue_data(q, ev_gm_req_t, queue);
1907 0 ngx_queue_remove(q);
1908 0 ▶ if (r->cb)
Branch: 1 branch
BranchTrueFalseTotal
if (r->cb) 0 0 0
1909 0 invoke_cb2(aTHX_ self, r->cb, NULL, newSVsv(err_disconnected));
1910 0 cleanup_req(aTHX_ r);
1911 }
1912 0 ▶ while (!ngx_queue_empty(&self->active_jobs)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->active_jobs)) { 0 0 0
1913 0 ngx_queue_t *q = ngx_queue_head(&self->active_jobs);
1914 0 ev_gm_active_t *a = ngx_queue_data(q, ev_gm_active_t, queue);
1915 0 ngx_queue_remove(q);
1916 0 self->active_count_cached--;
1917 0 ▶ if (a->on_complete)
Branch: 1 branch
BranchTrueFalseTotal
if (a->on_complete) 0 0 0
1918 0 invoke_cb2(aTHX_ self, a->on_complete, NULL, newSVsv(err_disconnected));
1919 0 cleanup_active(aTHX_ a);
1920 }
1921 0 ▶ while (!ngx_queue_empty(&self->wait_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!ngx_queue_empty(&self->wait_queue)) { 0 0 0
1922 0 ngx_queue_t *q = ngx_queue_head(&self->wait_queue);
1923 0 ev_gm_wait_t *w = ngx_queue_data(q, ev_gm_wait_t, queue);
1924 0 ngx_queue_remove(q);
1925 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
1926 0 invoke_cb2(aTHX_ self, w->req->cb, NULL, newSVsv(err_disconnected));
1927 0 cleanup_wait(aTHX_ w);
1928 }
1929 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
1930 0 self->pending_count = 0;
1931 0 self->waiting_count = 0;
1932 0 self->active_count_cached = 0;
1933 0 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 0 0 0
1934 0 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 0 0 0
1935 0 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 0 0 0
1936 0 ▶ CLEAR_HANDLER(self->worker_on_idle);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->worker_on_idle); 0 0 0
1937 0 Safefree(self->host); self->host = NULL;
1938 0 Safefree(self->path); self->path = NULL;
1939 0 Safefree(self->client_id);self->client_id = NULL;
1940 0 Safefree(self->rbuf); self->rbuf = NULL;
1941 0 Safefree(self->wbuf); self->wbuf = NULL;
1942 0 return;
1943 }
1944  
1945 12 stop_connect_timer(self);
1946 12 disarm_cmd_timer(self);
1947 12 stop_reconnect_timer(self);
1948 12 cleanup_connection(aTHX_ self);
1949  
1950 12 ▶ if (!PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) { 11 1 12
1951 11 self->callback_depth++;
1952 11 cancel_pending(aTHX_ self, err_disconnected);
1953 11 cancel_active(aTHX_ self, err_disconnected);
1954 11 cancel_waiting(aTHX_ self, err_disconnected);
1955 11 self->callback_depth--;
1956 } else {
1957 1 ▶ 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 1 1
1958 1 ▶ 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 1 1
1959 1 ▶ 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 1 1
1960 }
1961  
1962 12 ▶ 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 12 12
1963  
1964 12 self->magic = GM_MAGIC_FREED;
1965 12 ▶ CLEAR_HANDLER(self->on_error);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_error); 12 0 12
1966 12 ▶ CLEAR_HANDLER(self->on_connect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_connect); 1 11 12
1967 12 ▶ CLEAR_HANDLER(self->on_disconnect);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->on_disconnect); 0 12 12
1968 12 ▶ CLEAR_HANDLER(self->worker_on_idle);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->worker_on_idle); 0 12 12
1969 12 Safefree(self->host);
1970 12 Safefree(self->path);
1971 12 Safefree(self->client_id);
1972 12 Safefree(self->rbuf);
1973 12 Safefree(self->wbuf);
1974 12 Safefree(self);
1975 }
1976  
1977 void
1978 connect(EV::Gearman self, const char *host, int port = 4730)
1979 CODE:
1980 {
1981 0 ▶ if (self->connected || self->connecting) croak("already connected");
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) croak("already connected"); 0 0 0
if (self->connected || self->connecting) croak("already connected"); 0 0 0
1982 0 stop_reconnect_timer(self);
1983 0 Safefree(self->host); self->host = savepv(host);
1984 0 self->port = port;
1985 0 Safefree(self->path); self->path = NULL;
1986 0 self->intentional_disconnect = 0;
1987 0 start_connect(aTHX_ self);
1988 }
1989  
1990 void
1991 connect_unix(EV::Gearman self, const char *path)
1992 CODE:
1993 {
1994 1 ▶ if (self->connected || self->connecting) croak("already connected");
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) croak("already connected"); 1 0 1
if (self->connected || self->connecting) croak("already connected"); 0 1 1
1995 1 stop_reconnect_timer(self);
1996 1 Safefree(self->path); self->path = savepv(path);
1997 1 Safefree(self->host); self->host = NULL;
1998 1 self->intentional_disconnect = 0;
1999 1 start_connect(aTHX_ self);
2000 }
2001  
2002 void
2003 disconnect(EV::Gearman self)
2004 CODE:
2005 {
2006 0 self->intentional_disconnect = 1;
2007 0 self->worker_active = 0;
2008 0 stop_reconnect_timer(self);
2009 0 self->callback_depth++;
2010 0 ▶ if (self->connected || self->connecting) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->connected || self->connecting) { 0 0 0
if (self->connected || self->connecting) { 0 0 0
2011 0 handle_disconnect(aTHX_ self, NULL);
2012 } else {
2013 0 cancel_waiting(aTHX_ self, err_disconnected);
2014 }
2015 0 self->callback_depth--;
2016 0 check_destroyed(self);
2017 }
2018  
2019 int
2020 is_connected(EV::Gearman self)
2021 CODE:
2022 2 ▶ RETVAL = self->connected || self->connecting;
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = self->connected || self->connecting; 2 0 2
RETVAL = self->connected || self->connecting; 0 2 2
RETVAL = self->connected || self->connecting; 0 2 2
2023 OUTPUT:
2024 RETVAL
2025  
2026 int
2027 pending_count(EV::Gearman self)
2028 CODE:
2029 0 ▶ RETVAL = self->pending_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->pending_count; 0 0 0
2030 OUTPUT:
2031 RETVAL
2032  
2033 int
2034 waiting_count(EV::Gearman self)
2035 CODE:
2036 0 ▶ RETVAL = self->waiting_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->waiting_count; 0 0 0
2037 OUTPUT:
2038 RETVAL
2039  
2040 int
2041 active_count(EV::Gearman self)
2042 CODE:
2043 0 ▶ RETVAL = self->active_count_cached;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->active_count_cached; 0 0 0
2044 OUTPUT:
2045 RETVAL
2046  
2047 # Internal: current (read, write) buffer capacities in bytes. Used by
2048 # the test suite to assert that oversized buffers are released after a
2049 # large packet drains. Not part of the public API.
2050 void
2051 _buf_caps(EV::Gearman self)
2052 PPCODE:
2053 {
2054 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
2055 0 mPUSHu(self->rbuf_cap);
2056 0 mPUSHu(self->wbuf_cap);
2057 }
2058  
2059 SV *
2060 on_error(EV::Gearman self, ...)
2061 CODE:
2062 0 ▶ RETVAL = handler_accessor(aTHX_ &self->on_error, items,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = handler_accessor(aTHX_ &self->on_error, items, 0 0 0
2063 0 items > 1 ? ST(1) : &PL_sv_undef);
2064 OUTPUT:
2065 RETVAL
2066  
2067 SV *
2068 on_connect(EV::Gearman self, ...)
2069 CODE:
2070 2 ▶ RETVAL = handler_accessor(aTHX_ &self->on_connect, items,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = handler_accessor(aTHX_ &self->on_connect, items, 1 0 1
2071 1 items > 1 ? ST(1) : &PL_sv_undef);
2072 OUTPUT:
2073 RETVAL
2074  
2075 SV *
2076 on_disconnect(EV::Gearman self, ...)
2077 CODE:
2078 0 ▶ RETVAL = handler_accessor(aTHX_ &self->on_disconnect, items,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = handler_accessor(aTHX_ &self->on_disconnect, items, 0 0 0
2079 0 items > 1 ? ST(1) : &PL_sv_undef);
2080 OUTPUT:
2081 RETVAL
2082  
2083 void
2084 echo(EV::Gearman self, SV *data_sv, SV *cb_sv = &PL_sv_undef)
2085 CODE:
2086 {
2087 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
2088 STRLEN dlen;
2089 1 const char *data = SvPV(data_sv, dlen);
2090 1 ev_gm_req_t *r = alloc_req(CB_ECHO, cb_sv);
2091 1 enqueue_packet(aTHX_ self, GM_CMD_ECHO_REQ, data, dlen, r);
2092 }
2093  
2094 void
2095 _submit_internal(EV::Gearman self, int cmd_idx, SV *func_sv, SV *workload_sv, SV *unique_sv, SV *opts_sv, SV *cb_sv)
2096 CODE:
2097 {
2098 /* cmd_idx mapping: 0=submit_job, 1=high, 2=low,
2099 3=submit_job_bg, 4=high_bg, 5=low_bg */
2100 static const uint32_t cmd_map[] = {
2101 GM_CMD_SUBMIT_JOB, GM_CMD_SUBMIT_JOB_HIGH, GM_CMD_SUBMIT_JOB_LOW,
2102 GM_CMD_SUBMIT_JOB_BG, GM_CMD_SUBMIT_JOB_HIGH_BG, GM_CMD_SUBMIT_JOB_LOW_BG
2103 };
2104 1 ▶ 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"); 1 0 1
if (cmd_idx < 0 || cmd_idx > 5) croak("invalid submit cmd index"); 0 1 1
2105 1 ▶ GM_CROAK_UNLESS_ALIVE(self);
Branch: 4 branches
BranchTrueFalseTotal
GM_CROAK_UNLESS_ALIVE(self); 1 0 1
GM_CROAK_UNLESS_ALIVE(self); 0 1 1
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
GM_CROAK_UNLESS_ALIVE(self); 0 0 0
2106 1 int is_bg = (cmd_idx >= 3);
2107  
2108 1 STRLEN flen, wlen, ulen = 0;
2109 1 ▶ const char *fname = SvPV(func_sv, flen);
Branch: 1 branch
BranchTrueFalseTotal
const char *fname = SvPV(func_sv, flen); 1 0 1
2110 1 const char *wload = SvPV(workload_sv, wlen);
2111 1 const char *uniq = "";
2112 1 ▶ if (SvOK(unique_sv)) uniq = SvPV(unique_sv, ulen);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(unique_sv)) uniq = SvPV(unique_sv, ulen); 0 1 1
2113  
2114 /* Validate the eventual body size (3 args + 2 NUL separators)
2115 before allocating anything, so the croak can't leak r/body. */
2116 1 gm_check_size(aTHX_ flen + ulen + wlen + 2);
2117  
2118 0 ▶ 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); 0 0 0
2119  
2120 /* Optional callback bundle for foreground (opts_sv may be hashref) */
2121 0 ▶ 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) { 0 0 0
if (!is_bg && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 0 0 0
if (!is_bg && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) { 0 0 0
2122 0 HV *opts = (HV *)SvRV(opts_sv);
2123 SV **v;
2124 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
2125 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
2126 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
2127 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
2128 }
2129  
2130 0 const char *ptrs[3] = { fname, uniq, wload };
2131 0 STRLEN lens[3] = { flen, ulen, wlen };
2132 size_t blen;
2133 0 char *body = gm_encode_args(3, ptrs, lens, &blen);
2134 0 enqueue_packet(aTHX_ self, cmd_map[cmd_idx], body, blen, r);
2135 0 Safefree(body);
2136 }
2137  
2138 void
2139 _submit_epoch(EV::Gearman self, SV *func_sv, SV *workload_sv, SV *unique_sv, UV epoch, SV *cb_sv)
2140 CODE:
2141 {
2142 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
2143 0 STRLEN flen, wlen, ulen = 0;
2144 0 ▶ const char *fname = SvPV(func_sv, flen);
Branch: 1 branch
BranchTrueFalseTotal
const char *fname = SvPV(func_sv, flen); 0 0 0
2145 0 const char *wload = SvPV(workload_sv, wlen);
2146 0 const char *uniq = "";
2147 0 ▶ if (SvOK(unique_sv)) uniq = SvPV(unique_sv, ulen);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(unique_sv)) uniq = SvPV(unique_sv, ulen); 0 0 0
2148  
2149 char epoch_buf[32];
2150 0 int epoch_len = snprintf(epoch_buf, sizeof(epoch_buf), "%lu", (unsigned long)epoch);
2151  
2152 /* Validate body size (4 args + 3 NUL separators) before allocating. */
2153 0 gm_check_size(aTHX_ flen + ulen + (STRLEN)epoch_len + wlen + 3);
2154  
2155 0 ev_gm_req_t *r = alloc_req(CB_SUBMIT_BG, cb_sv);
2156  
2157 /* SUBMIT_JOB_EPOCH: FUNC\0UNIQ\0EPOCH\0DATA */
2158 0 const char *ptrs[4] = { fname, uniq, epoch_buf, wload };
2159 0 STRLEN lens[4] = { flen, ulen, (STRLEN)epoch_len, wlen };
2160 size_t blen;
2161 0 char *body = gm_encode_args(4, ptrs, lens, &blen);
2162 0 enqueue_packet(aTHX_ self, GM_CMD_SUBMIT_JOB_EPOCH, body, blen, r);
2163 0 Safefree(body);
2164 }
2165  
2166 void
2167 get_status(EV::Gearman self, SV *handle_sv, SV *cb_sv = &PL_sv_undef)
2168 CODE:
2169 {
2170 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
2171 STRLEN hlen;
2172 0 const char *h = SvPV(handle_sv, hlen);
2173 0 ev_gm_req_t *r = alloc_req(CB_GET_STATUS, cb_sv);
2174 0 enqueue_packet(aTHX_ self, GM_CMD_GET_STATUS, h, hlen, r);
2175 }
2176  
2177 void
2178 get_status_unique(EV::Gearman self, SV *unique_sv, SV *cb_sv = &PL_sv_undef)
2179 CODE:
2180 {
2181 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
2182 STRLEN ulen;
2183 0 const char *u = SvPV(unique_sv, ulen);
2184 0 ev_gm_req_t *r = alloc_req(CB_GET_STATUS_UNIQ, cb_sv);
2185 0 enqueue_packet(aTHX_ self, GM_CMD_GET_STATUS_UNIQUE, u, ulen, r);
2186 }
2187  
2188 void
2189 option(EV::Gearman self, SV *name_sv, SV *cb_sv = &PL_sv_undef)
2190 CODE:
2191 {
2192 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
2193 STRLEN nlen;
2194 0 const char *n = SvPV(name_sv, nlen);
2195 /* Track common options client-side */
2196 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
2197 0 self->opt_exceptions = 1;
2198 0 ev_gm_req_t *r = alloc_req(CB_OPTION, cb_sv);
2199 0 enqueue_packet(aTHX_ self, GM_CMD_OPTION_REQ, n, nlen, r);
2200 }
2201  
2202 void
2203 set_client_id(EV::Gearman self, SV *id_sv)
2204 CODE:
2205 {
2206 STRLEN ilen;
2207 0 const char *id = SvPV(id_sv, ilen);
2208 0 Safefree(self->client_id);
2209 0 self->client_id = savepv(id);
2210 0 ▶ if (self->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) 0 0 0
2211 0 enqueue_packet(aTHX_ self, GM_CMD_SET_CLIENT_ID, id, ilen, NULL);
2212 }
2213  
2214 void
2215 can_do(EV::Gearman self, SV *func_sv, ...)
2216 CODE:
2217 {
2218 STRLEN flen;
2219 0 const char *fname = SvPV(func_sv, flen);
2220 0 int timeout = 0;
2221 0 ▶ 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 0 0
if (items > 2 && SvOK(ST(2))) timeout = SvIV(ST(2)); 0 0 0
2222  
2223 /* Without a Perl handler the caller must use grab_job to receive
2224 jobs; the entry just records the ability so reconnect re-sends
2225 the CAN_DO. */
2226 0 ev_gm_func_t *existing = find_function(self, fname, flen);
2227 0 ▶ if (!existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (!existing) { 0 0 0
2228 ev_gm_func_t *f;
2229 0 Newxz(f, 1, ev_gm_func_t);
2230 0 f->name = savepv(fname);
2231 0 f->timeout = timeout;
2232 0 ngx_queue_insert_tail(&self->functions, &f->queue);
2233 } else {
2234 0 existing->timeout = timeout;
2235 }
2236 0 send_can_do(aTHX_ self, fname, flen, timeout);
2237 }
2238  
2239 void
2240 cant_do(EV::Gearman self, SV *func_sv)
2241 CODE:
2242 {
2243 STRLEN flen;
2244 0 const char *fname = SvPV(func_sv, flen);
2245 /* Remove from registered functions */
2246 ngx_queue_t *q;
2247 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
2248 0 ev_gm_func_t *f = ngx_queue_data(q, ev_gm_func_t, queue);
2249 0 ngx_queue_t *nx = ngx_queue_next(q);
2250 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
2251 0 ngx_queue_remove(q);
2252 0 cleanup_func(aTHX_ f);
2253 }
2254 0 q = nx;
2255 }
2256 0 ▶ if (self->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) 0 0 0
2257 0 enqueue_packet(aTHX_ self, GM_CMD_CANT_DO, fname, flen, NULL);
2258 }
2259  
2260 void
2261 reset_abilities(EV::Gearman self)
2262 CODE:
2263 {
2264 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
2265 0 ▶ if (self->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) 0 0 0
2266 0 enqueue_packet(aTHX_ self, GM_CMD_RESET_ABILITIES, NULL, 0, NULL);
2267 }
2268  
2269 void
2270 _register_function(EV::Gearman self, SV *name_sv, SV *cb_sv, int timeout, int async)
2271 CODE:
2272 {
2273 STRLEN nlen;
2274 0 const char *name = SvPV(name_sv, nlen);
2275 0 ▶ if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 0 0 0
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 0 0 0
2276 0 croak("register_function: callback must be a coderef");
2277  
2278 0 ev_gm_func_t *existing = find_function(self, name, nlen);
2279 0 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 0 0 0
2280 0 ▶ CLEAR_HANDLER(existing->cb);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(existing->cb); 0 0 0
2281 0 existing->cb = newSVsv(cb_sv);
2282 0 existing->async = async ? 1 : 0;
2283 0 existing->timeout = timeout;
2284 } else {
2285 ev_gm_func_t *f;
2286 0 Newxz(f, 1, ev_gm_func_t);
2287 0 f->name = savepv(name);
2288 0 f->cb = newSVsv(cb_sv);
2289 0 f->async = async ? 1 : 0;
2290 0 f->timeout = timeout;
2291 0 ngx_queue_insert_tail(&self->functions, &f->queue);
2292 }
2293  
2294 0 send_can_do(aTHX_ self, name, nlen, timeout);
2295 }
2296  
2297 void
2298 work(EV::Gearman self, ...)
2299 CODE:
2300 {
2301 /* Start (or restart) the worker GRAB loop */
2302 0 self->worker_active = 1;
2303 0 self->worker_one_shot = 0;
2304 0 ▶ 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) { 0 0 0
if (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { 0 0 0
if (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { 0 0 0
if (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVCV) { 0 0 0
2305 0 ▶ CLEAR_HANDLER(self->worker_on_idle);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->worker_on_idle); 0 0 0
2306 0 self->worker_on_idle = newSVsv(ST(1));
2307 }
2308 0 worker_continue(aTHX_ self);
2309 }
2310  
2311 void
2312 work_one(EV::Gearman self, SV *cb_sv = &PL_sv_undef)
2313 CODE:
2314 {
2315 0 ▶ SV *cb = (SvOK(cb_sv) && SvROK(cb_sv)) ? cb_sv : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *cb = (SvOK(cb_sv) && SvROK(cb_sv)) ? cb_sv : NULL; 0 0 0
SV *cb = (SvOK(cb_sv) && SvROK(cb_sv)) ? cb_sv : NULL; 0 0 0
2316 0 self->worker_active = 1;
2317 0 self->worker_one_shot = 1;
2318 0 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 0 0 0
2319 0 ▶ CLEAR_HANDLER(self->worker_on_idle);
Branch: 1 branch
BranchTrueFalseTotal
CLEAR_HANDLER(self->worker_on_idle); 0 0 0
2320 0 self->worker_on_idle = newSVsv(cb);
2321 }
2322 0 worker_continue(aTHX_ self);
2323 }
2324  
2325 void
2326 work_stop(EV::Gearman self)
2327 CODE:
2328 {
2329 0 self->worker_active = 0;
2330 0 self->worker_one_shot = 0;
2331 }
2332  
2333 void
2334 grab_job(EV::Gearman self, SV *cb_sv)
2335 CODE:
2336 {
2337 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
2338 0 ▶ if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 0 0 0
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 0 0 0
2339 0 croak("grab_job: callback required");
2340 0 self->worker_grab_inflight = 1;
2341 0 ev_gm_req_t *r = alloc_req(CB_GRAB_JOB, cb_sv);
2342 0 enqueue_packet(aTHX_ self,
2343 0 ▶ 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 0 0
2344 NULL, 0, r);
2345 }
2346  
2347 void
2348 all_yours(EV::Gearman self)
2349 CODE:
2350 {
2351 0 ▶ if (self->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (self->connected) 0 0 0
2352 0 enqueue_packet(aTHX_ self, GM_CMD_ALL_YOURS, NULL, 0, NULL);
2353 }
2354  
2355 void
2356 admin(EV::Gearman self, SV *cmd_sv, ...)
2357 CODE:
2358 {
2359 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
2360 STRLEN clen;
2361 0 const char *cmd = SvPV(cmd_sv, clen);
2362 0 SV *cb = NULL;
2363 0 ▶ 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) 0 0 0
if (items > 2 && SvOK(ST(items-1)) && SvROK(ST(items-1)) && SvTYPE(SvRV(ST(items-1))) == SVt_PVCV) 0 0 0
if (items > 2 && SvOK(ST(items-1)) && SvROK(ST(items-1)) && SvTYPE(SvRV(ST(items-1))) == SVt_PVCV) 0 0 0
if (items > 2 && SvOK(ST(items-1)) && SvROK(ST(items-1)) && SvTYPE(SvRV(ST(items-1))) == SVt_PVCV) 0 0 0
2364 0 cb = ST(items-1);
2365  
2366 /* Multi-line admin commands terminate with ".\n"; single-line ones don't. */
2367 0 int multi = 0;
2368 /* Strip trailing whitespace for matching */
2369 0 size_t cl = clen;
2370 0 ▶ 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')) 0 0 0
while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r')) 0 0 0
while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r')) 0 0 0
while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r')) 0 0 0
while (cl > 0 && (cmd[cl-1] == '\n' || cmd[cl-1] == ' ' || cmd[cl-1] == '\t' || cmd[cl-1] == '\r')) 0 0 0
2371 0 cl--;
2372 0 ▶ if ((cl == 6 && memcmp(cmd, "status", 6) == 0) ||
Branch: 3 branches
BranchTrueFalseTotal
if ((cl == 6 && memcmp(cmd, "status", 6) == 0) || 0 0 0
if ((cl == 6 && memcmp(cmd, "status", 6) == 0) || 0 0 0
if ((cl == 6 && memcmp(cmd, "status", 6) == 0) || 0 0 0
2373 0 ▶ (cl == 7 && memcmp(cmd, "workers", 7) == 0) ||
Branch: 2 branches
BranchTrueFalseTotal
(cl == 7 && memcmp(cmd, "workers", 7) == 0) || 0 0 0
(cl == 7 && memcmp(cmd, "workers", 7) == 0) || 0 0 0
2374 0 ▶ (cl == 14 && memcmp(cmd, "prioritystatus", 14) == 0))
Branch: 1 branch
BranchTrueFalseTotal
(cl == 14 && memcmp(cmd, "prioritystatus", 14) == 0)) 0 0 0
2375 0 multi = 1;
2376 /* "shutdown" / "shutdown graceful" => single line ("OK\n" then disconnect) */
2377 /* "version" => single line */
2378 /* "maxqueue X N" => "OK\n" */
2379  
2380 /* Build full text command (must end in \n) */
2381 char *txt;
2382 size_t txt_len;
2383 0 ▶ int has_nl = (clen > 0 && cmd[clen-1] == '\n');
Branch: 2 branches
BranchTrueFalseTotal
int has_nl = (clen > 0 && cmd[clen-1] == '\n'); 0 0 0
int has_nl = (clen > 0 && cmd[clen-1] == '\n'); 0 0 0
2384 0 txt_len = clen + (has_nl ? 0 : 1);
2385 0 Newx(txt, txt_len, char);
2386 0 memcpy(txt, cmd, clen);
2387 0 ▶ if (!has_nl) txt[txt_len - 1] = '\n';
Branch: 1 branch
BranchTrueFalseTotal
if (!has_nl) txt[txt_len - 1] = '\n'; 0 0 0
2388  
2389 0 ev_gm_req_t *r = alloc_req(CB_ADMIN, cb);
2390 0 r->admin_terminator = multi;
2391  
2392 /* submit_bytes takes ownership of txt and Safefrees it. */
2393 0 submit_bytes(aTHX_ self, txt, txt_len, r);
2394 }
2395  
2396 int
2397 connect_timeout(EV::Gearman self, ...)
2398 CODE:
2399 {
2400 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2401 0 self->connect_timeout_ms = SvIV(ST(1));
2402 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
2403 }
2404 0 ▶ RETVAL = self->connect_timeout_ms;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->connect_timeout_ms; 0 0 0
2405 }
2406 OUTPUT:
2407 RETVAL
2408  
2409 int
2410 command_timeout(EV::Gearman self, ...)
2411 CODE:
2412 {
2413 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2414 0 self->command_timeout_ms = SvIV(ST(1));
2415 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
2416 0 ▶ if (self->command_timeout_ms == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (self->command_timeout_ms == 0) 0 0 0
2417 0 disarm_cmd_timer(self);
2418 0 ▶ else if (!ngx_queue_empty(&self->cb_queue))
Branch: 1 branch
BranchTrueFalseTotal
else if (!ngx_queue_empty(&self->cb_queue)) 0 0 0
2419 0 arm_cmd_timer(self);
2420 }
2421 0 ▶ RETVAL = self->command_timeout_ms;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->command_timeout_ms; 0 0 0
2422 }
2423 OUTPUT:
2424 RETVAL
2425  
2426 void
2427 reconnect(EV::Gearman self, ...)
2428 CODE:
2429 {
2430 /* Optional args: only overwrite delay / max_attempts when the
2431 caller supplies them, so reconnect(1) re-enables without
2432 resetting the values configured at construction time. */
2433 2 ▶ if (items < 2) croak("reconnect: enable arg required");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("reconnect: enable arg required"); 1 1 2
2434 1 self->reconnect = SvTRUE(ST(1)) ? 1 : 0;
2435 1 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 1 1
2436 0 int d = SvIV(ST(2));
2437 0 self->reconnect_delay_ms = d >= 0 ? d : 0;
2438 }
2439 1 ▶ if (items > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) { 0 1 1
2440 0 int m = SvIV(ST(3));
2441 0 self->max_reconnect_attempts = m >= 0 ? m : 0;
2442 }
2443 1 ▶ if (!self->reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->reconnect) { 1 0 1
2444 1 self->reconnect_attempts = 0;
2445 1 stop_reconnect_timer(self);
2446 }
2447 }
2448  
2449 int
2450 reconnect_enabled(EV::Gearman self)
2451 CODE:
2452 0 ▶ RETVAL = self->reconnect;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->reconnect; 0 0 0
2453 OUTPUT:
2454 RETVAL
2455  
2456 int
2457 priority(EV::Gearman self, ...)
2458 CODE:
2459 {
2460 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2461 0 self->priority = SvIV(ST(1));
2462 0 ▶ if (self->priority < -2) self->priority = -2;
Branch: 1 branch
BranchTrueFalseTotal
if (self->priority < -2) self->priority = -2; 0 0 0
2463 0 ▶ if (self->priority > 2) self->priority = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (self->priority > 2) self->priority = 2; 0 0 0
2464 0 ▶ if (self->reading) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reading) { 0 0 0
2465 0 ev_io_stop(self->loop, &self->rio);
2466 0 ev_set_priority(&self->rio, self->priority);
2467 0 ev_io_start(self->loop, &self->rio);
2468 } else {
2469 0 ev_set_priority(&self->rio, self->priority);
2470 }
2471 0 ▶ if (self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->writing) { 0 0 0
2472 0 ev_io_stop(self->loop, &self->wio);
2473 0 ev_set_priority(&self->wio, self->priority);
2474 0 ev_io_start(self->loop, &self->wio);
2475 } else {
2476 0 ev_set_priority(&self->wio, self->priority);
2477 }
2478 }
2479 0 ▶ RETVAL = self->priority;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->priority; 0 0 0
2480 }
2481 OUTPUT:
2482 RETVAL
2483  
2484 int
2485 keepalive(EV::Gearman self, ...)
2486 CODE:
2487 {
2488 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
2489 0 self->keepalive = SvIV(ST(1));
2490 0 ▶ if (self->keepalive < 0) self->keepalive = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (self->keepalive < 0) self->keepalive = 0; 0 0 0
2491 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
2492 0 apply_keepalive(self);
2493 }
2494 0 ▶ RETVAL = self->keepalive;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->keepalive; 0 0 0
2495 }
2496 OUTPUT:
2497 RETVAL
2498  
2499 # ===== Job object methods (called from Perl via $job->complete etc.) =====
2500  
2501 MODULE = EV::Gearman PACKAGE = EV::Gearman::Job
2502  
2503 void
2504 _send_event(SV *job_sv, int kind, SV *data_sv = &PL_sv_undef)
2505 CODE:
2506 {
2507 /* kind: 0=complete, 1=fail, 2=exception, 3=data, 4=warning */
2508 STRLEN hl;
2509 const char *h;
2510 0 ev_gm_t *self = job_resolve(aTHX_ job_sv, &h, &hl, "_send_event");
2511  
2512 0 STRLEN dl = 0;
2513 0 ▶ 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; 0 0 0
2514  
2515 static const uint32_t cmds[] = {
2516 GM_CMD_WORK_COMPLETE, GM_CMD_WORK_FAIL, GM_CMD_WORK_EXCEPTION,
2517 GM_CMD_WORK_DATA, GM_CMD_WORK_WARNING,
2518 };
2519 0 ▶ if (kind < 0 || kind >= (int)(sizeof(cmds)/sizeof(cmds[0])))
Branch: 2 branches
BranchTrueFalseTotal
if (kind < 0 || kind >= (int)(sizeof(cmds)/sizeof(cmds[0]))) 0 0 0
if (kind < 0 || kind >= (int)(sizeof(cmds)/sizeof(cmds[0]))) 0 0 0
2520 0 croak("_send_event: unknown kind %d", kind);
2521  
2522 /* WORK_FAIL takes no data; the rest take handle\0data even if data
2523 is empty (the trailing NUL is the separator, not a terminator). */
2524 0 ▶ send_work_event(aTHX_ self, cmds[kind], h, hl,
Branch: 1 branch
BranchTrueFalseTotal
send_work_event(aTHX_ self, cmds[kind], h, hl, 0 0 0
2525 0 ▶ kind == 1 ? NULL : (dp ? dp : ""), dl);
Branch: 1 branch
BranchTrueFalseTotal
kind == 1 ? NULL : (dp ? dp : ""), dl); 0 0 0
2526 }
2527  
2528 void
2529 _send_status(SV *job_sv, SV *num_sv, SV *denom_sv)
2530 CODE:
2531 {
2532 STRLEN hl;
2533 const char *h;
2534 0 ev_gm_t *self = job_resolve(aTHX_ job_sv, &h, &hl, "_send_status");
2535  
2536 STRLEN nl, dl;
2537 0 ▶ const char *n = SvPV(num_sv, nl);
Branch: 1 branch
BranchTrueFalseTotal
const char *n = SvPV(num_sv, nl); 0 0 0
2538 0 const char *d = SvPV(denom_sv, dl);
2539 0 const char *ptrs[3] = { h, n, d };
2540 0 STRLEN lens[3] = { hl, nl, dl };
2541 size_t blen;
2542 0 char *body = gm_encode_args(3, ptrs, lens, &blen);
2543 0 enqueue_packet(aTHX_ self, GM_CMD_WORK_STATUS, body, blen, NULL);
2544 0 Safefree(body);
2545 }
2546  
« blib/lib/EV/Gearman/Job.pm ↑ Summary