include/punk/punk_health.h

filter: Statementstmt 96.8% 156 / 161 Branchbran 70.3% 113 / 172 (10 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.1% 269 / 333 (10 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_host.h ↑ Summary xs/metrics.xs »
1 /* punk_health.h - /healthz and /readyz, and the distinction between them.
2 *
3 * Every deployment target asks for a probe endpoint, so every application
4 * writes one, and the version it writes is a route that returns 200 without
5 * checking anything. That answers "is the process up", which the TCP connect
6 * already answered, and it keeps answering 200 while the database is gone.
7 *
8 * LIVENESS IS NOT READINESS, AND THIS IS THE WHOLE PLUGIN.
9 *
10 * /healthz (liveness) asks: is this process wedged? It MUST NOT check a
11 * dependency. Failing a liveness probe gets the worker KILLED AND RESTARTED,
12 * which does not fix the database and removes capacity at the moment of
13 * maximum load. A restart loop across every worker, caused by one dependency
14 * being slow, is the outage this separation exists to prevent. So the
15 * liveness handler here runs no checks at all - not "runs the cheap ones",
16 * none - and that is enforced by construction: it does not have the check
17 * list.
18 *
19 * /readyz (readiness) asks: should this worker be sent traffic right now?
20 * That is where dependencies belong. Failing it takes the worker out of the
21 * pool without killing it, and it goes back in when the dependency returns.
22 *
23 * WHAT CANNOT BE PROMISED.
24 *
25 * A budget bounds the checks, and a budget is not a timeout: nothing here can
26 * interrupt a check that has already blocked in the driver. Punk runs on a
27 * single-threaded event loop, so a check that blocks blocks the worker, and
28 * SIGALRM inside a request would be a worse cure than the disease. What the
29 * budget does is refuse to START a check once the time is spent, and answer
30 * unready. A check that can hang must carry its own driver-level timeout, and
31 * the documentation says so rather than implying otherwise.
32 *
33 * The answer is cached for `ttl` seconds, which is what stops a probe every
34 * 100ms becoming load. That cache is per worker and stamped with the pid, so
35 * a forked worker never serves the answer its parent computed.
36 *
37 * Must be included after punk_context.h (pcx_call_meth), punk_app.h (app_hv,
38 * app_get), punk_response.h (punk_triplet) and punk_static.h (punk_closure).
39 */
40  
41 #ifndef PUNK_HEALTH_H
42 #define PUNK_HEALTH_H
43  
44 #define PH_DEFAULT_TTL 1.0
45 #define PH_DEFAULT_BUDGET 2.0
46  
47 /* Append a JSON string literal, escaped. Health output contains check names
48 * and error messages, and an error message is whatever a driver said - which
49 * is neither trusted nor known to be JSON-safe. */
50 37 static void ph_json_str(pTHX_ SV *out, const char *s, STRLEN len) {
51 STRLEN i;
52 37 sv_catpvs(out, "\"");
53 230 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 193 37 230
54 193 unsigned char ch = (unsigned char)s[i];
55 /*
56 # uncoverable branch true count:1
57 # uncoverable branch true count:2
58 # uncoverable branch true count:3
59 # uncoverable branch true count:4
60 # uncoverable branch true count:5
61 # uncoverable branch false count:6 */
62 193 switch (ch) {
63 0 case '"': sv_catpvs(out, "\\\""); break; /* # uncoverable statement */
64 0 case '\\': sv_catpvs(out, "\\\\"); break; /* # uncoverable statement */
65 0 case '\n': sv_catpvs(out, "\\n"); break; /* # uncoverable statement */
66 0 case '\r': sv_catpvs(out, "\\r"); break; /* # uncoverable statement */
67 0 case '\t': sv_catpvs(out, "\\t"); break; /* # uncoverable statement */
68 193 default:
69 /*
70 # uncoverable branch true
71 # uncoverable condition left count:1 class:ignore_covered_err */
72 193 ▶ if (ch < 0x20) sv_catpvf(out, "\\u%04x", (unsigned)ch);
Branch: 1 branch
BranchTrueFalseTotal
if (ch < 0x20) sv_catpvf(out, "\\u%04x", (unsigned)ch); 0 193 193
73 193 else sv_catpvn(out, (const char *)&ch, 1);
74 }
75 }
76 37 sv_catpvs(out, "\"");
77 37 }
78  
79 /* now(), as a double. */
80 158 static double ph_now(pTHX) {
81 158 dSP; double t = 0.0;
82 int count;
83 158 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 158 158
84 158 count = call_pv("Time::HiRes::time", G_SCALAR | G_EVAL);
85 158 SPAGAIN;
86 /* POPPED ONCE, into an SV*, before SvNV touches it. SvNV is a macro that
87 * mentions its argument more than once, so SvNV(POPs) pops repeatedly and
88 * reads the value off the wrong slot - `PL_valid_types_NVX` on a
89 * DEBUGGING perl, silent nonsense everywhere else. */
90 /*
91 # uncoverable branch false
92 # uncoverable condition right count:1 class:ignore_covered_err */
93 158 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 158 0 158
94 158 SV *sv = POPs;
95 /*
96 # uncoverable branch false
97 # uncoverable condition right count:1 class:ignore_covered_err
98 # uncoverable condition right count:2 class:ignore_covered_err */
99 158 ▶ if (!SvTRUE(ERRSV)) t = SvNV(sv);
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) t = SvNV(sv); 158 0 158
if (!SvTRUE(ERRSV)) t = SvNV(sv); 158 0 158
100 }
101 158 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 158 0 158
102 158 return t;
103 }
104  
105 /* Run one check coderef. Returns 1 ready, 0 not, and fills `why` (mortal, may
106 * stay NULL) with the reason when it is not.
107 *
108 * A check that DIES is not ready, and the exception is the reason. A check
109 * that returns false is not ready with no reason. Both are ordinary answers
110 * rather than errors: a readiness probe exists to be told no. */
111 27 static int ph_run_check(pTHX_ SV *cb, SV *c, SV **why) {
112 27 dSP; int ok = 0, count;
113 27 SV *err = NULL;
114  
115 27 ENTER; SAVETMPS;
116 27 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 27 27
117 27 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 27 27
118 27 ▶ PUSHs(c ? c : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(c ? c : &PL_sv_undef); 18 9 27
119 27 PUTBACK;
120 27 count = call_sv(cb, G_SCALAR | G_EVAL);
121 27 SPAGAIN;
122 27 ▶ if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 27 0 27
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 23 27
123 4 ▶ STRLEN el; const char *ep = SvPV_const(ERRSV, el);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN el; const char *ep = SvPV_const(ERRSV, el); 4 0 4
124 /*
125 # uncoverable condition right count:1 class:ignore_covered_err
126 # uncoverable condition left count:3 class:ignore_covered_err */
127 8 ▶ while (el && (ep[el - 1] == '\n' || ep[el - 1] == '\r')) el--;
Branch: 3 branches
BranchTrueFalseTotal
while (el && (ep[el - 1] == '\n' || ep[el - 1] == '\r')) el--; 8 0 8
while (el && (ep[el - 1] == '\n' || ep[el - 1] == '\r')) el--; 4 4 8
while (el && (ep[el - 1] == '\n' || ep[el - 1] == '\r')) el--; 0 4 4
128 4 err = newSVpvn(ep, el);
129 /*
130 # uncoverable branch false
131 # uncoverable condition right count:1 class:ignore_covered_err */
132 4 ▶ if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) (void)POPs; 4 0 4
133 }
134 /*
135 # uncoverable branch false
136 # uncoverable condition right count:1 class:ignore_covered_err */
137 23 ▶ else if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) { 23 0 23
138 23 SV *sv = POPs; /* once - SvTRUE mentions it twice */
139 23 ok = SvTRUE(sv) ? 1 : 0;
140 }
141 27 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 27 0 27
142  
143 /* Mortalised OUTSIDE the SAVETMPS above: an SV made mortal inside it is
144 * freed by the FREETMPS, and the caller would read a corpse. */
145 27 ▶ if (err && why) *why = sv_2mortal(err); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (err && why) *why = sv_2mortal(err); /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 23 27
if (err && why) *why = sv_2mortal(err); /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
146 /*
147 # uncoverable branch true
148 # uncoverable condition left count:1 class:ignore_covered_err */
149 23 ▶ else if (err) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
else if (err) SvREFCNT_dec(err); 0 23 23
150 27 return ok;
151 }
152  
153 /* The readiness answer: 1 ready, 0 not. Appends the per-check JSON detail to
154 * `detail` when it is non-NULL. */
155 19 static int ph_readiness(pTHX_ SV *app, SV *c, SV *detail) {
156 19 HV *h = app_hv(aTHX_ app);
157 19 ▶ SV *checks = h ? app_get(aTHX_ h, "health_checks") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *checks = h ? app_get(aTHX_ h, "health_checks") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 19 0 19
158 18 ▶ HV *ch = (checks && SvROK(checks) && SvTYPE(SvRV(checks)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *ch = (checks && SvROK(checks) && SvTYPE(SvRV(checks)) == SVt_PVHV) 18 0 18
HV *ch = (checks && SvROK(checks) && SvTYPE(SvRV(checks)) == SVt_PVHV) 18 0 18
159 37 ▶ ? (HV *)SvRV(checks) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(checks) : NULL; 18 1 19
160 19 ▶ SV *bsv = h ? app_get(aTHX_ h, "health_budget") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *bsv = h ? app_get(aTHX_ h, "health_budget") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 19 0 19
161 /*
162 # uncoverable condition right count:1 class:ignore_covered_err
163 # uncoverable condition right count:2 class:ignore_covered_err */
164 19 ▶ double budget = (bsv && SvOK(bsv)) ? SvNV(bsv) : PH_DEFAULT_BUDGET;
Branch: 2 branches
BranchTrueFalseTotal
double budget = (bsv && SvOK(bsv)) ? SvNV(bsv) : PH_DEFAULT_BUDGET; 19 0 19
double budget = (bsv && SvOK(bsv)) ? SvNV(bsv) : PH_DEFAULT_BUDGET; 19 0 19
165 19 double started = ph_now(aTHX);
166 19 int ready = 1, first = 1;
167 HE *ent;
168  
169 19 ▶ if (detail) sv_catpvs(detail, "\"checks\":{");
Branch: 1 branch
BranchTrueFalseTotal
if (detail) sv_catpvs(detail, "\"checks\":{"); 11 8 19
170 19 ▶ if (!ch) { if (detail) sv_catpvs(detail, "}"); return 1; } /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!ch) { if (detail) sv_catpvs(detail, "}"); return 1; } /* # uncoverable condition left count:2 class:ignore_covered_err */ 1 18 19
if (!ch) { if (detail) sv_catpvs(detail, "}"); return 1; } /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1 1
171  
172 /* Sorted, so the document is stable between probes: an operator diffing
173 * two answers should see what changed, not what moved. */
174 {
175 18 AV *names = (AV *)sv_2mortal((SV *)newAV());
176 SSize_t i, n;
177 18 hv_iterinit(ch);
178 46 ▶ while ((ent = hv_iternext(ch)))
Branch: 1 branch
BranchTrueFalseTotal
while ((ent = hv_iternext(ch))) 28 18 46
179 28 av_push(names, newSVsv(hv_iterkeysv(ent)));
180 18 n = av_len(names) + 1;
181 18 sortsv(AvARRAY(names), (SSize_t)n, Perl_sv_cmp);
182  
183 46 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 28 18 46
184 28 SV *name = *av_fetch(names, i, 0);
185 28 HE *he = hv_fetch_ent(ch, name, 0, 0);
186 28 ▶ SV *cb = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *cb = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 28 0 28
187 28 SV *why = NULL;
188 int ok;
189 double t0, spent;
190 28 STRLEN nl; const char *np = SvPV_const(name, nl);
191  
192 28 spent = ph_now(aTHX) - started;
193 28 ▶ if (spent >= budget) {
Branch: 1 branch
BranchTrueFalseTotal
if (spent >= budget) { 1 27 28
194 /* The budget is spent, so the remaining checks are not
195 * STARTED. This cannot rescue a check already blocked in a
196 * driver - see the header - but it stops a probe adding more
197 * of them once it is already late. */
198 1 ready = 0;
199 /*
200 # uncoverable branch false
201 # uncoverable condition right count:1 class:ignore_covered_err */
202 1 ▶ if (detail) {
Branch: 1 branch
BranchTrueFalseTotal
if (detail) { 1 0 1
203 /*
204 # uncoverable branch false
205 # uncoverable condition right count:1 class:ignore_covered_err */
206 1 ▶ if (!first) sv_catpvs(detail, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (!first) sv_catpvs(detail, ","); 1 0 1
207 1 ph_json_str(aTHX_ detail, np, nl);
208 1 sv_catpvs(detail, ":{\"ok\":false,\"skipped\":true,"
209 "\"why\":\"the readiness budget was "
210 "spent before this check was run\"}");
211 1 first = 0;
212 }
213 1 continue;
214 }
215  
216 /*
217 # uncoverable branch true
218 # uncoverable condition left count:1 class:ignore_covered_err
219 # uncoverable condition left count:2 class:ignore_covered_err
220 # uncoverable condition left count:3 class:ignore_covered_err */
221 27 ▶ if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) continue; 27 0 27
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) continue; 27 0 27
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) continue; 0 27 27
222  
223 27 t0 = ph_now(aTHX);
224 27 ok = ph_run_check(aTHX_ cb, c, &why);
225 27 ▶ if (!ok) ready = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) ready = 0; 8 19 27
226  
227 27 ▶ if (detail) {
Branch: 1 branch
BranchTrueFalseTotal
if (detail) { 20 7 27
228 20 ▶ if (!first) sv_catpvs(detail, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (!first) sv_catpvs(detail, ","); 9 11 20
229 20 ph_json_str(aTHX_ detail, np, nl);
230 /* NVff and (NV), because sv_catpvf reads the width its
231 * length modifier names and a bare %f names `double` - which
232 * is not perl's NV on a long-double or quadmath build. */
233 20 ▶ sv_catpvf(detail, ":{\"ok\":%s,\"ms\":%.2" NVff,
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(detail, ":{\"ok\":%s,\"ms\":%.2" NVff, 14 6 20
234 ok ? "true" : "false",
235 (NV)((ph_now(aTHX) - t0) * 1000.0));
236 20 ▶ if (why && SvOK(why)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (why && SvOK(why)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 16 20
if (why && SvOK(why)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
237 4 STRLEN wl; const char *wp = SvPV_const(why, wl);
238 4 sv_catpvs(detail, ",\"why\":");
239 4 ph_json_str(aTHX_ detail, wp, wl);
240 }
241 20 sv_catpvs(detail, "}");
242 20 first = 0;
243 }
244 }
245 }
246 18 ▶ if (detail) sv_catpvs(detail, "}");
Branch: 1 branch
BranchTrueFalseTotal
if (detail) sv_catpvs(detail, "}"); 11 7 18
247 18 return ready;
248 }
249  
250 /* The `version` member, when one was configured. */
251 12 static void ph_cat_version(pTHX_ HV *h, SV *out) {
252 12 ▶ SV *b = h ? app_get(aTHX_ h, "health_version") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *b = h ? app_get(aTHX_ h, "health_version") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 0 12
253 /*
254 # uncoverable branch false
255 # uncoverable condition right count:1 class:ignore_covered_err
256 # uncoverable condition right count:2 class:ignore_covered_err
257 # uncoverable condition right count:3 class:ignore_covered_err */
258 12 ▶ if (b && SvOK(b) && SvCUR(b)) {
Branch: 3 branches
BranchTrueFalseTotal
if (b && SvOK(b) && SvCUR(b)) { 12 0 12
if (b && SvOK(b) && SvCUR(b)) { 12 0 12
if (b && SvOK(b) && SvCUR(b)) { 12 0 12
259 12 STRLEN bl; const char *bp = SvPV_const(b, bl);
260 12 sv_catpvs(out, ",\"version\":");
261 12 ph_json_str(aTHX_ out, bp, bl);
262 }
263 12 }
264  
265 /* Whether detail was asked for. */
266 73 static int ph_detail(pTHX_ HV *h) {
267 73 ▶ SV *d = h ? app_get(aTHX_ h, "health_detail") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *d = h ? app_get(aTHX_ h, "health_detail") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 73 0 73
268 73 ▶ return (d && SvTRUE(d)) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return (d && SvTRUE(d)) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 61 73
return (d && SvTRUE(d)) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 0 12
269 }
270  
271 /* The response, ready-made. */
272 73 static SV *ph_reply(pTHX_ IV status, SV *body) {
273 73 AV *extra = (AV *)sv_2mortal((SV *)newAV());
274 /* A probe answer is never worth caching, anywhere, by anyone: a cached
275 * 200 is a health check that reports the past. */
276 73 av_push(extra, newSVpvs("Cache-Control"));
277 73 av_push(extra, newSVpvs("no-store"));
278 73 return punk_triplet(aTHX_ status, sv_2mortal(newSVpvs("application/json")),
279 body, extra);
280 }
281  
282 /* ---- /healthz ---------------------------------------------------------------
283 *
284 * Deliberately has no access to the check list. Liveness that consults a
285 * dependency is the bug this plugin exists to stop, and the way to not write
286 * it is to not have the thing available. */
287 XS_INTERNAL(ph_live_cb);
288 9 XS_INTERNAL(ph_live_cb) {
289 9 dXSARGS;
290 9 AV *cap = punk_clos_cap(aTHX_ cv);
291 9 ▶ SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 0 9
292 9 ▶ HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 0 9
293 9 SV *body = sv_2mortal(newSVpvs("{\"status\":\"ok\""));
294 PERL_UNUSED_VAR(items);
295  
296 9 ▶ if (ph_detail(aTHX_ h)) ph_cat_version(aTHX_ h, body);
Branch: 1 branch
BranchTrueFalseTotal
if (ph_detail(aTHX_ h)) ph_cat_version(aTHX_ h, body); 6 3 9
297 9 sv_catpvs(body, "}");
298  
299 9 ST(0) = sv_2mortal(ph_reply(aTHX_ 200, newSVsv(body)));
300 9 XSRETURN(1);
301 }
302  
303 /* ---- /readyz --------------------------------------------------------------- */
304 XS_INTERNAL(ph_ready_cb);
305 64 XS_INTERNAL(ph_ready_cb) {
306 64 dXSARGS;
307 64 AV *cap = punk_clos_cap(aTHX_ cv);
308 64 ▶ SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 64 0 64
309 64 ▶ SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 64 0 64
310 64 ▶ HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HV *h = app ? app_hv(aTHX_ app) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 64 0 64
311 64 ▶ SV *tsv = h ? app_get(aTHX_ h, "health_ttl") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *tsv = h ? app_get(aTHX_ h, "health_ttl") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 64 0 64
312 /*
313 # uncoverable condition right count:1 class:ignore_covered_err
314 # uncoverable condition right count:2 class:ignore_covered_err */
315 64 ▶ double ttl = (tsv && SvOK(tsv)) ? SvNV(tsv) : PH_DEFAULT_TTL;
Branch: 2 branches
BranchTrueFalseTotal
double ttl = (tsv && SvOK(tsv)) ? SvNV(tsv) : PH_DEFAULT_TTL; 64 0 64
double ttl = (tsv && SvOK(tsv)) ? SvNV(tsv) : PH_DEFAULT_TTL; 64 0 64
316 64 ▶ SV *cached = h ? app_get(aTHX_ h, "health_cached") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *cached = h ? app_get(aTHX_ h, "health_cached") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 64 0 64
317 51 ▶ AV *slot = (cached && SvROK(cached) && SvTYPE(SvRV(cached)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
AV *slot = (cached && SvROK(cached) && SvTYPE(SvRV(cached)) == SVt_PVAV) 51 0 51
AV *slot = (cached && SvROK(cached) && SvTYPE(SvRV(cached)) == SVt_PVAV) 51 0 51
318 115 ▶ ? (AV *)SvRV(cached) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(cached) : NULL; 51 13 64
319 64 double now = ph_now(aTHX);
320 64 int detail = ph_detail(aTHX_ h);
321 SV *body;
322 int ready;
323  
324 /* A cached answer, if it is still fresh AND this worker computed it. The
325 * pid stamp is what stops a forked worker serving its parent's verdict:
326 * cheap, and the alternative is a whole pool agreeing about a dependency
327 * only one of them ever contacted. */
328 64 ▶ if (slot && ttl > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (slot && ttl > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 51 13 64
if (slot && ttl > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 51 0 51
329 51 SV **exp = av_fetch(slot, 0, 0);
330 51 SV **pid = av_fetch(slot, 1, 0);
331 51 SV **st = av_fetch(slot, 2, 0);
332 51 SV **bd = av_fetch(slot, 3, 0);
333 /*
334 # uncoverable condition right count:1 class:ignore_covered_err
335 # uncoverable condition right count:2 class:ignore_covered_err
336 # uncoverable condition right count:3 class:ignore_covered_err
337 # uncoverable condition right count:4 class:ignore_covered_err
338 # uncoverable condition right count:5 class:ignore_covered_err
339 # uncoverable condition right count:6 class:ignore_covered_err
340 # uncoverable condition right count:7 class:ignore_covered_err
341 # uncoverable condition right count:8 class:ignore_covered_err
342 # uncoverable condition right count:9 class:ignore_covered_err */
343 51 ▶ if (exp && *exp && pid && *pid && st && *st && bd && *bd
Branch: 8 branches
BranchTrueFalseTotal
if (exp && *exp && pid && *pid && st && *st && bd && *bd 51 0 51
if (exp && *exp && pid && *pid && st && *st && bd && *bd 51 0 51
if (exp && *exp && pid && *pid && st && *st && bd && *bd 51 0 51
if (exp && *exp && pid && *pid && st && *st && bd && *bd 51 0 51
if (exp && *exp && pid && *pid && st && *st && bd && *bd 51 0 51
if (exp && *exp && pid && *pid && st && *st && bd && *bd 51 0 51
if (exp && *exp && pid && *pid && st && *st && bd && *bd 51 0 51
if (exp && *exp && pid && *pid && st && *st && bd && *bd 51 0 51
344 51 ▶ && SvNV(*exp) > now && SvIV(*pid) == (IV)PerlProc_getpid()) {
Branch: 2 branches
BranchTrueFalseTotal
&& SvNV(*exp) > now && SvIV(*pid) == (IV)PerlProc_getpid()) { 51 0 51
&& SvNV(*exp) > now && SvIV(*pid) == (IV)PerlProc_getpid()) { 50 1 51
345 50 ST(0) = sv_2mortal(ph_reply(aTHX_ SvIV(*st), newSVsv(*bd)));
346 50 XSRETURN(1);
347 }
348 }
349  
350 14 body = sv_2mortal(newSVpvs(""));
351 {
352 14 ▶ SV *det = detail ? sv_2mortal(newSVpvs("")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *det = detail ? sv_2mortal(newSVpvs("")) : NULL; 6 8 14
353 14 ready = ph_readiness(aTHX_ app, c, det);
354 14 ▶ sv_catpvf(body, "{\"status\":\"%s\"", ready ? "ok" : "unready");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(body, "{\"status\":\"%s\"", ready ? "ok" : "unready"); 10 4 14
355 14 ▶ if (detail) {
Branch: 1 branch
BranchTrueFalseTotal
if (detail) { 6 8 14
356 6 ph_cat_version(aTHX_ h, body);
357 6 sv_catpvs(body, ",");
358 6 sv_catsv(body, det);
359 }
360 14 sv_catpvs(body, "}");
361 }
362  
363 /* 503, not 500: unready is an ordinary answer that a load balancer knows
364 * how to act on, and it means "not me, right now" rather than "something
365 * broke". */
366 {
367 14 ▶ IV status = ready ? 200 : 503;
Branch: 1 branch
BranchTrueFalseTotal
IV status = ready ? 200 : 503; 10 4 14
368 14 ▶ if (h && ttl > 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (h && ttl > 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 0 14
if (h && ttl > 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 8 6 14
369 8 AV *fresh = newAV();
370 8 av_push(fresh, newSVnv(now + ttl));
371 8 av_push(fresh, newSViv((IV)PerlProc_getpid()));
372 8 av_push(fresh, newSViv(status));
373 8 av_push(fresh, newSVsv(body));
374 8 (void)hv_stores(h, "health_cached", newRV_noinc((SV *)fresh));
375 }
376 14 ST(0) = sv_2mortal(ph_reply(aTHX_ status, newSVsv(body)));
377 }
378 14 XSRETURN(1);
379 }
380  
381 #endif /* PUNK_HEALTH_H */
« include/punk/punk_host.h ↑ Summary xs/metrics.xs »