| 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
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
193 |
37 |
230 |
|
| 54 |
193 |
| unsigned char ch = (unsigned char)s[i]; |
| 55 |
193 |
| switch (ch) { |
| 56 |
0 |
| case '"': sv_catpvs(out, "\\\""); break; |
| 57 |
0 |
| case '\\': sv_catpvs(out, "\\\\"); break; |
| 58 |
0 |
| case '\n': sv_catpvs(out, "\\n"); break; |
| 59 |
0 |
| case '\r': sv_catpvs(out, "\\r"); break; |
| 60 |
0 |
| case '\t': sv_catpvs(out, "\\t"); break; |
| 61 |
193 |
| default: |
| 62 |
193 |
▶ | if (ch < 0x20) sv_catpvf(out, "\\u%04x", (unsigned)ch); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch < 0x20) sv_catpvf(out, "\\u%04x", (unsigned)ch); |
0 |
193 |
193 |
|
| 63 |
193 |
| else sv_catpvn(out, (const char *)&ch, 1); |
| 64 |
|
| } |
| 65 |
|
| } |
| 66 |
37 |
| sv_catpvs(out, "\""); |
| 67 |
37 |
| } |
| 68 |
|
| |
| 69 |
|
| /* now(), as a double. */ |
| 70 |
158 |
| static double ph_now(pTHX) { |
| 71 |
158 |
| dSP; double t = 0.0; |
| 72 |
|
| int count; |
| 73 |
158 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
158 |
158 |
|
| 74 |
158 |
| count = call_pv("Time::HiRes::time", G_SCALAR | G_EVAL); |
| 75 |
158 |
| SPAGAIN; |
| 76 |
|
| /* POPPED ONCE, into an SV*, before SvNV touches it. SvNV is a macro that |
| 77 |
|
| * mentions its argument more than once, so SvNV(POPs) pops repeatedly and |
| 78 |
|
| * reads the value off the wrong slot - `PL_valid_types_NVX` on a |
| 79 |
|
| * DEBUGGING perl, silent nonsense everywhere else. */ |
| 80 |
158 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
158 |
0 |
158 |
|
| 81 |
158 |
| SV *sv = POPs; |
| 82 |
158 |
▶ | if (!SvTRUE(ERRSV)) t = SvNV(sv); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV)) t = SvNV(sv); |
158 |
0 |
158 |
| if (!SvTRUE(ERRSV)) t = SvNV(sv); |
158 |
0 |
158 |
|
| 83 |
|
| } |
| 84 |
158 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
158 |
0 |
158 |
|
| 85 |
158 |
| return t; |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| /* Run one check coderef. Returns 1 ready, 0 not, and fills `why` (mortal, may |
| 89 |
|
| * stay NULL) with the reason when it is not. |
| 90 |
|
| * |
| 91 |
|
| * A check that DIES is not ready, and the exception is the reason. A check |
| 92 |
|
| * that returns false is not ready with no reason. Both are ordinary answers |
| 93 |
|
| * rather than errors: a readiness probe exists to be told no. */ |
| 94 |
27 |
| static int ph_run_check(pTHX_ SV *cb, SV *c, SV **why) { |
| 95 |
27 |
| dSP; int ok = 0, count; |
| 96 |
27 |
| SV *err = NULL; |
| 97 |
|
| |
| 98 |
27 |
| ENTER; SAVETMPS; |
| 99 |
27 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
27 |
27 |
|
| 100 |
27 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
27 |
27 |
|
| 101 |
27 |
▶ | PUSHs(c ? c : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(c ? c : &PL_sv_undef); |
18 |
9 |
27 |
|
| 102 |
27 |
| PUTBACK; |
| 103 |
27 |
| count = call_sv(cb, G_SCALAR | G_EVAL); |
| 104 |
27 |
| SPAGAIN; |
| 105 |
27 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
27 |
0 |
27 |
| if (SvTRUE(ERRSV)) { |
4 |
23 |
27 |
|
| 106 |
4 |
▶ | STRLEN el; const char *ep = SvPV_const(ERRSV, el); |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN el; const char *ep = SvPV_const(ERRSV, el); |
4 |
0 |
4 |
|
| 107 |
8 |
▶ | while (el && (ep[el - 1] == '\n' || ep[el - 1] == '\r')) el--; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 108 |
4 |
| err = newSVpvn(ep, el); |
| 109 |
4 |
▶ | if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) (void)POPs; |
4 |
0 |
4 |
|
| 110 |
|
| } |
| 111 |
23 |
▶ | else if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) { |
23 |
0 |
23 |
|
| 112 |
23 |
| SV *sv = POPs; /* once - SvTRUE mentions it twice */ |
| 113 |
23 |
| ok = SvTRUE(sv) ? 1 : 0; |
| 114 |
|
| } |
| 115 |
27 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
27 |
0 |
27 |
|
| 116 |
|
| |
| 117 |
|
| /* Mortalised OUTSIDE the SAVETMPS above: an SV made mortal inside it is |
| 118 |
|
| * freed by the FREETMPS, and the caller would read a corpse. */ |
| 119 |
27 |
▶ | if (err && why) *why = sv_2mortal(err); |
Branch: 2 branches
| Branch | True | False | Total |
| if (err && why) *why = sv_2mortal(err); |
4 |
23 |
27 |
| if (err && why) *why = sv_2mortal(err); |
4 |
0 |
4 |
|
| 120 |
23 |
▶ | else if (err) SvREFCNT_dec(err); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (err) SvREFCNT_dec(err); |
0 |
23 |
23 |
|
| 121 |
27 |
| return ok; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| /* The readiness answer: 1 ready, 0 not. Appends the per-check JSON detail to |
| 125 |
|
| * `detail` when it is non-NULL. */ |
| 126 |
19 |
| static int ph_readiness(pTHX_ SV *app, SV *c, SV *detail) { |
| 127 |
19 |
| HV *h = app_hv(aTHX_ app); |
| 128 |
19 |
▶ | SV *checks = h ? app_get(aTHX_ h, "health_checks") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *checks = h ? app_get(aTHX_ h, "health_checks") : NULL; |
19 |
0 |
19 |
|
| 129 |
18 |
▶ | HV *ch = (checks && SvROK(checks) && SvTYPE(SvRV(checks)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 130 |
37 |
▶ | ? (HV *)SvRV(checks) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(checks) : NULL; |
18 |
1 |
19 |
|
| 131 |
19 |
▶ | SV *bsv = h ? app_get(aTHX_ h, "health_budget") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *bsv = h ? app_get(aTHX_ h, "health_budget") : NULL; |
19 |
0 |
19 |
|
| 132 |
19 |
▶ | double budget = (bsv && SvOK(bsv)) ? SvNV(bsv) : PH_DEFAULT_BUDGET; |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 133 |
19 |
| double started = ph_now(aTHX); |
| 134 |
19 |
| int ready = 1, first = 1; |
| 135 |
|
| HE *ent; |
| 136 |
|
| |
| 137 |
19 |
▶ | if (detail) sv_catpvs(detail, "\"checks\":{"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (detail) sv_catpvs(detail, "\"checks\":{"); |
11 |
8 |
19 |
|
| 138 |
19 |
▶ | if (!ch) { if (detail) sv_catpvs(detail, "}"); return 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ch) { if (detail) sv_catpvs(detail, "}"); return 1; } |
1 |
18 |
19 |
| if (!ch) { if (detail) sv_catpvs(detail, "}"); return 1; } |
0 |
1 |
1 |
|
| 139 |
|
| |
| 140 |
|
| /* Sorted, so the document is stable between probes: an operator diffing |
| 141 |
|
| * two answers should see what changed, not what moved. */ |
| 142 |
|
| { |
| 143 |
18 |
| AV *names = (AV *)sv_2mortal((SV *)newAV()); |
| 144 |
|
| SSize_t i, n; |
| 145 |
18 |
| hv_iterinit(ch); |
| 146 |
46 |
▶ | while ((ent = hv_iternext(ch))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((ent = hv_iternext(ch))) |
28 |
18 |
46 |
|
| 147 |
28 |
| av_push(names, newSVsv(hv_iterkeysv(ent))); |
| 148 |
18 |
| n = av_len(names) + 1; |
| 149 |
18 |
| sortsv(AvARRAY(names), (SSize_t)n, Perl_sv_cmp); |
| 150 |
|
| |
| 151 |
46 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
28 |
18 |
46 |
|
| 152 |
28 |
| SV *name = *av_fetch(names, i, 0); |
| 153 |
28 |
| HE *he = hv_fetch_ent(ch, name, 0, 0); |
| 154 |
28 |
▶ | SV *cb = he ? HeVAL(he) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *cb = he ? HeVAL(he) : NULL; |
28 |
0 |
28 |
|
| 155 |
28 |
| SV *why = NULL; |
| 156 |
|
| int ok; |
| 157 |
|
| double t0, spent; |
| 158 |
28 |
| STRLEN nl; const char *np = SvPV_const(name, nl); |
| 159 |
|
| |
| 160 |
28 |
| spent = ph_now(aTHX) - started; |
| 161 |
28 |
▶ | if (spent >= budget) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (spent >= budget) { |
1 |
27 |
28 |
|
| 162 |
|
| /* The budget is spent, so the remaining checks are not |
| 163 |
|
| * STARTED. This cannot rescue a check already blocked in a |
| 164 |
|
| * driver - see the header - but it stops a probe adding more |
| 165 |
|
| * of them once it is already late. */ |
| 166 |
1 |
| ready = 0; |
| 167 |
1 |
▶ | if (detail) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (detail) { |
1 |
0 |
1 |
|
| 168 |
1 |
▶ | if (!first) sv_catpvs(detail, ","); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!first) sv_catpvs(detail, ","); |
1 |
0 |
1 |
|
| 169 |
1 |
| ph_json_str(aTHX_ detail, np, nl); |
| 170 |
1 |
| sv_catpvs(detail, ":{\"ok\":false,\"skipped\":true," |
| 171 |
|
| "\"why\":\"the readiness budget was " |
| 172 |
|
| "spent before this check was run\"}"); |
| 173 |
1 |
| first = 0; |
| 174 |
|
| } |
| 175 |
1 |
| continue; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
27 |
▶ | if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 179 |
|
| |
| 180 |
27 |
| t0 = ph_now(aTHX); |
| 181 |
27 |
| ok = ph_run_check(aTHX_ cb, c, &why); |
| 182 |
27 |
▶ | if (!ok) ready = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) ready = 0; |
8 |
19 |
27 |
|
| 183 |
|
| |
| 184 |
27 |
▶ | if (detail) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (detail) { |
20 |
7 |
27 |
|
| 185 |
20 |
▶ | if (!first) sv_catpvs(detail, ","); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!first) sv_catpvs(detail, ","); |
9 |
11 |
20 |
|
| 186 |
20 |
| ph_json_str(aTHX_ detail, np, nl); |
| 187 |
|
| /* NVff and (NV), because sv_catpvf reads the width its |
| 188 |
|
| * length modifier names and a bare %f names `double` - which |
| 189 |
|
| * is not perl's NV on a long-double or quadmath build. */ |
| 190 |
20 |
▶ | sv_catpvf(detail, ":{\"ok\":%s,\"ms\":%.2" NVff, |
Branch: 1 branch
| Branch | True | False | Total |
| sv_catpvf(detail, ":{\"ok\":%s,\"ms\":%.2" NVff, |
14 |
6 |
20 |
|
| 191 |
|
| ok ? "true" : "false", |
| 192 |
|
| (NV)((ph_now(aTHX) - t0) * 1000.0)); |
| 193 |
20 |
▶ | if (why && SvOK(why)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (why && SvOK(why)) { |
4 |
16 |
20 |
| if (why && SvOK(why)) { |
4 |
0 |
4 |
|
| 194 |
4 |
| STRLEN wl; const char *wp = SvPV_const(why, wl); |
| 195 |
4 |
| sv_catpvs(detail, ",\"why\":"); |
| 196 |
4 |
| ph_json_str(aTHX_ detail, wp, wl); |
| 197 |
|
| } |
| 198 |
20 |
| sv_catpvs(detail, "}"); |
| 199 |
20 |
| first = 0; |
| 200 |
|
| } |
| 201 |
|
| } |
| 202 |
|
| } |
| 203 |
18 |
▶ | if (detail) sv_catpvs(detail, "}"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (detail) sv_catpvs(detail, "}"); |
11 |
7 |
18 |
|
| 204 |
18 |
| return ready; |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| /* The `version` member, when one was configured. */ |
| 208 |
12 |
| static void ph_cat_version(pTHX_ HV *h, SV *out) { |
| 209 |
12 |
▶ | SV *b = h ? app_get(aTHX_ h, "health_version") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *b = h ? app_get(aTHX_ h, "health_version") : NULL; |
12 |
0 |
12 |
|
| 210 |
12 |
▶ | if (b && SvOK(b) && SvCUR(b)) { |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 211 |
12 |
| STRLEN bl; const char *bp = SvPV_const(b, bl); |
| 212 |
12 |
| sv_catpvs(out, ",\"version\":"); |
| 213 |
12 |
| ph_json_str(aTHX_ out, bp, bl); |
| 214 |
|
| } |
| 215 |
12 |
| } |
| 216 |
|
| |
| 217 |
|
| /* Whether detail was asked for. */ |
| 218 |
73 |
| static int ph_detail(pTHX_ HV *h) { |
| 219 |
73 |
▶ | SV *d = h ? app_get(aTHX_ h, "health_detail") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *d = h ? app_get(aTHX_ h, "health_detail") : NULL; |
73 |
0 |
73 |
|
| 220 |
73 |
▶ | return (d && SvTRUE(d)) ? 1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (d && SvTRUE(d)) ? 1 : 0; |
12 |
61 |
73 |
| return (d && SvTRUE(d)) ? 1 : 0; |
12 |
0 |
12 |
|
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
|
| /* The response, ready-made. */ |
| 224 |
73 |
| static SV *ph_reply(pTHX_ IV status, SV *body) { |
| 225 |
73 |
| AV *extra = (AV *)sv_2mortal((SV *)newAV()); |
| 226 |
|
| /* A probe answer is never worth caching, anywhere, by anyone: a cached |
| 227 |
|
| * 200 is a health check that reports the past. */ |
| 228 |
73 |
| av_push(extra, newSVpvs("Cache-Control")); |
| 229 |
73 |
| av_push(extra, newSVpvs("no-store")); |
| 230 |
73 |
| return punk_triplet(aTHX_ status, sv_2mortal(newSVpvs("application/json")), |
| 231 |
|
| body, extra); |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
|
| /* ---- /healthz --------------------------------------------------------------- |
| 235 |
|
| * |
| 236 |
|
| * Deliberately has no access to the check list. Liveness that consults a |
| 237 |
|
| * dependency is the bug this plugin exists to stop, and the way to not write |
| 238 |
|
| * it is to not have the thing available. */ |
| 239 |
|
| XS_INTERNAL(ph_live_cb); |
| 240 |
9 |
| XS_INTERNAL(ph_live_cb) { |
| 241 |
9 |
| dXSARGS; |
| 242 |
9 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 243 |
9 |
▶ | SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; |
9 |
0 |
9 |
|
| 244 |
9 |
▶ | HV *h = app ? app_hv(aTHX_ app) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| HV *h = app ? app_hv(aTHX_ app) : NULL; |
9 |
0 |
9 |
|
| 245 |
9 |
| SV *body = sv_2mortal(newSVpvs("{\"status\":\"ok\"")); |
| 246 |
|
| PERL_UNUSED_VAR(items); |
| 247 |
|
| |
| 248 |
9 |
▶ | if (ph_detail(aTHX_ h)) ph_cat_version(aTHX_ h, body); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ph_detail(aTHX_ h)) ph_cat_version(aTHX_ h, body); |
6 |
3 |
9 |
|
| 249 |
9 |
| sv_catpvs(body, "}"); |
| 250 |
|
| |
| 251 |
9 |
| ST(0) = sv_2mortal(ph_reply(aTHX_ 200, newSVsv(body))); |
| 252 |
9 |
| XSRETURN(1); |
| 253 |
|
| } |
| 254 |
|
| |
| 255 |
|
| /* ---- /readyz --------------------------------------------------------------- */ |
| 256 |
|
| XS_INTERNAL(ph_ready_cb); |
| 257 |
64 |
| XS_INTERNAL(ph_ready_cb) { |
| 258 |
64 |
| dXSARGS; |
| 259 |
64 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 260 |
64 |
▶ | SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *app = cap ? *av_fetch(cap, 0, 0) : NULL; |
64 |
0 |
64 |
|
| 261 |
64 |
▶ | SV *c = items > 0 ? ST(0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *c = items > 0 ? ST(0) : NULL; |
64 |
0 |
64 |
|
| 262 |
64 |
▶ | HV *h = app ? app_hv(aTHX_ app) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| HV *h = app ? app_hv(aTHX_ app) : NULL; |
64 |
0 |
64 |
|
| 263 |
64 |
▶ | SV *tsv = h ? app_get(aTHX_ h, "health_ttl") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *tsv = h ? app_get(aTHX_ h, "health_ttl") : NULL; |
64 |
0 |
64 |
|
| 264 |
64 |
▶ | double ttl = (tsv && SvOK(tsv)) ? SvNV(tsv) : PH_DEFAULT_TTL; |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 265 |
64 |
▶ | SV *cached = h ? app_get(aTHX_ h, "health_cached") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *cached = h ? app_get(aTHX_ h, "health_cached") : NULL; |
64 |
0 |
64 |
|
| 266 |
51 |
▶ | AV *slot = (cached && SvROK(cached) && SvTYPE(SvRV(cached)) == SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 267 |
115 |
▶ | ? (AV *)SvRV(cached) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (AV *)SvRV(cached) : NULL; |
51 |
13 |
64 |
|
| 268 |
64 |
| double now = ph_now(aTHX); |
| 269 |
64 |
| int detail = ph_detail(aTHX_ h); |
| 270 |
|
| SV *body; |
| 271 |
|
| int ready; |
| 272 |
|
| |
| 273 |
|
| /* A cached answer, if it is still fresh AND this worker computed it. The |
| 274 |
|
| * pid stamp is what stops a forked worker serving its parent's verdict: |
| 275 |
|
| * cheap, and the alternative is a whole pool agreeing about a dependency |
| 276 |
|
| * only one of them ever contacted. */ |
| 277 |
64 |
▶ | if (slot && ttl > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (slot && ttl > 0) { |
51 |
13 |
64 |
| if (slot && ttl > 0) { |
51 |
0 |
51 |
|
| 278 |
51 |
| SV **exp = av_fetch(slot, 0, 0); |
| 279 |
51 |
| SV **pid = av_fetch(slot, 1, 0); |
| 280 |
51 |
| SV **st = av_fetch(slot, 2, 0); |
| 281 |
51 |
| SV **bd = av_fetch(slot, 3, 0); |
| 282 |
51 |
▶ | if (exp && *exp && pid && *pid && st && *st && bd && *bd |
Branch: 8 branches
| Branch | True | False | Total |
| 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 |
|
| 283 |
51 |
▶ | && SvNV(*exp) > now && SvIV(*pid) == (IV)PerlProc_getpid()) { |
Branch: 2 branches
| Branch | True | False | Total |
| && SvNV(*exp) > now && SvIV(*pid) == (IV)PerlProc_getpid()) { |
51 |
0 |
51 |
| && SvNV(*exp) > now && SvIV(*pid) == (IV)PerlProc_getpid()) { |
50 |
1 |
51 |
|
| 284 |
50 |
| ST(0) = sv_2mortal(ph_reply(aTHX_ SvIV(*st), newSVsv(*bd))); |
| 285 |
50 |
| XSRETURN(1); |
| 286 |
|
| } |
| 287 |
|
| } |
| 288 |
|
| |
| 289 |
14 |
| body = sv_2mortal(newSVpvs("")); |
| 290 |
|
| { |
| 291 |
14 |
▶ | SV *det = detail ? sv_2mortal(newSVpvs("")) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *det = detail ? sv_2mortal(newSVpvs("")) : NULL; |
6 |
8 |
14 |
|
| 292 |
14 |
| ready = ph_readiness(aTHX_ app, c, det); |
| 293 |
14 |
▶ | sv_catpvf(body, "{\"status\":\"%s\"", ready ? "ok" : "unready"); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_catpvf(body, "{\"status\":\"%s\"", ready ? "ok" : "unready"); |
10 |
4 |
14 |
|
| 294 |
14 |
▶ | if (detail) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (detail) { |
6 |
8 |
14 |
|
| 295 |
6 |
| ph_cat_version(aTHX_ h, body); |
| 296 |
6 |
| sv_catpvs(body, ","); |
| 297 |
6 |
| sv_catsv(body, det); |
| 298 |
|
| } |
| 299 |
14 |
| sv_catpvs(body, "}"); |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
|
| /* 503, not 500: unready is an ordinary answer that a load balancer knows |
| 303 |
|
| * how to act on, and it means "not me, right now" rather than "something |
| 304 |
|
| * broke". */ |
| 305 |
|
| { |
| 306 |
14 |
▶ | IV status = ready ? 200 : 503; |
Branch: 1 branch
| Branch | True | False | Total |
| IV status = ready ? 200 : 503; |
10 |
4 |
14 |
|
| 307 |
14 |
▶ | if (h && ttl > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (h && ttl > 0) { |
14 |
0 |
14 |
| if (h && ttl > 0) { |
8 |
6 |
14 |
|
| 308 |
8 |
| AV *fresh = newAV(); |
| 309 |
8 |
| av_push(fresh, newSVnv(now + ttl)); |
| 310 |
8 |
| av_push(fresh, newSViv((IV)PerlProc_getpid())); |
| 311 |
8 |
| av_push(fresh, newSViv(status)); |
| 312 |
8 |
| av_push(fresh, newSVsv(body)); |
| 313 |
8 |
| (void)hv_stores(h, "health_cached", newRV_noinc((SV *)fresh)); |
| 314 |
|
| } |
| 315 |
14 |
| ST(0) = sv_2mortal(ph_reply(aTHX_ status, newSVsv(body))); |
| 316 |
|
| } |
| 317 |
14 |
| XSRETURN(1); |
| 318 |
|
| } |
| 319 |
|
| |
| 320 |
|
| #endif /* PUNK_HEALTH_H */ |