include/punk/punk_afterres.h

filter: Statementstmt 99.1% 116 / 117 Branchbran 61.1% 70 / 126 (13 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.4% 186 / 243 (13 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/health.xs ↑ Summary xs/blob.xs »
1 /* punk_afterres.h - work that runs after the client has the response.
2 *
3 * hook after_response => sub { my ($c, $resp) = @_; ... };
4 * $c->after_response(sub { my ($c, $resp) = @_; ... });
5 *
6 * `after_dispatch` sees the finalized triplet and may replace it, which means
7 * it necessarily runs BEFORE the response is written. There was no phase after
8 * it, and on an event loop that is the natural home for the work a request
9 * generates but the client is not waiting for: an audit row, a cache warm, a
10 * counter, an enqueue. Without one an application either pays for that work
11 * inside the response or reaches for a job queue for something that never
12 * needed durability.
13 *
14 * WHEN "after" is:
15 *
16 * - `psgix.cleanup` server (the PSGI extension): pushed onto
17 * psgix.cleanup.handlers, which the server runs once the response is
18 * complete. This is the exact promise, and it is the server's to keep.
19 * - a Hyperman worker: a zero-delay loop timer, so the callbacks run on the
20 * next pass of the loop - after the response has been handed to the
21 * kernel, and off the request's own path.
22 * - anywhere else: inline, immediately after the triplet is final. There is
23 * no loop to hand the work to and no server extension to take it, so the
24 * phase still runs in the right ORDER; only the promise that the client is
25 * no longer waiting is one the environment cannot make.
26 *
27 * A callback that dies is logged through the application's logger and the rest
28 * still run: there is no response left to turn into a 500, and a die that
29 * vanished silently here would be the worst of both.
30 *
31 * Must be included after hm_abi.h (the loop timer), punk_static.h
32 * (punk_closure) and punk_context.h (which forward-declares pk_after_res).
33 */
34  
35 #ifndef PUNK_AFTERRES_H
36 #define PUNK_AFTERRES_H
37  
38 /* Run the callbacks. Each gets ($c, $resp); the response is already gone, so
39 * a return value has nowhere to go and is discarded. */
40 9 static void pk_ar_run(pTHX_ SV *c, SV *resp, AV *cbs) {
41 9 ▶ SSize_t i, n = cbs ? av_len(cbs) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = cbs ? av_len(cbs) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 0 9
42 26 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17 9 26
43 17 SV **cp = av_fetch(cbs, i, 0);
44 17 int died = 0;
45 SV *r;
46 /*
47 # uncoverable branch true
48 # uncoverable condition left count:1 class:ignore_covered_err
49 # uncoverable condition left count:2 class:ignore_covered_err
50 # uncoverable condition left count:3 class:ignore_covered_err */
51 17 ▶ if (!cp || !*cp || !SvOK(*cp)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!cp || !*cp || !SvOK(*cp)) continue; 17 0 17
if (!cp || !*cp || !SvOK(*cp)) continue; 17 0 17
if (!cp || !*cp || !SvOK(*cp)) continue; 0 17 17
52 17 r = pcx_call2(aTHX_ *cp, c, resp, &died);
53 /*
54 # uncoverable branch false
55 # uncoverable condition right count:1 class:ignore_covered_err */
56 17 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 17 0 17
57 17 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 1 16 17
58 /* The logger, not warn: an application that configured logging
59 * said where its diagnostics go, and this is a diagnostic. Its
60 * own failure is swallowed - there is nothing above to tell. */
61 1 ▶ SV *err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_2mortal(newSVsv(ERRSV)); 1 0 1
62 1 SV *msg = sv_2mortal(newSVpvs("after_response hook died: "));
63 1 SV *log = NULL;
64 1 sv_catsv(msg, err);
65 { /* $c->log, trapped: everything from here down is a
66 * diagnostic, and a diagnostic that dies must not take the
67 * remaining callbacks with it */
68 1 dSP; int count;
69 1 ENTER; SAVETMPS;
70 1 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 1 1
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 1 1
71 1 count = call_method("log", G_SCALAR | G_EVAL);
72 1 SPAGAIN;
73 /*
74 # uncoverable branch false
75 # uncoverable condition right count:1 class:ignore_covered_err */
76 1 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 1 0 1
77 1 SV *l = POPs;
78 /*
79 # uncoverable branch false
80 # uncoverable condition right count:1 class:ignore_covered_err
81 # uncoverable condition right count:2 class:ignore_covered_err */
82 1 ▶ if (!SvTRUE(ERRSV) && SvOK(l)) log = newSVsv(l);
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && SvOK(l)) log = newSVsv(l); 1 0 1
if (!SvTRUE(ERRSV) && SvOK(l)) log = newSVsv(l); 1 0 1
if (!SvTRUE(ERRSV) && SvOK(l)) log = newSVsv(l); 1 0 1
83 }
84 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
85 }
86 /*
87 # uncoverable branch false
88 # uncoverable condition right count:1 class:ignore_covered_err */
89 1 ▶ if (log) {
Branch: 1 branch
BranchTrueFalseTotal
if (log) { 1 0 1
90 1 dSP;
91 1 ENTER; SAVETMPS;
92 1 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 1 1
PUSHMARK(SP); EXTEND(SP, 2); 0 1 1
93 1 PUSHs(sv_2mortal(log));
94 1 PUSHs(msg);
95 1 PUTBACK;
96 1 (void)call_method("error", G_VOID | G_EVAL | G_DISCARD);
97 1 SPAGAIN;
98 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
99 }
100 }
101 }
102 9 }
103  
104 /* the job, for the two deferred paths */
105 typedef struct {
106 SV *c;
107 SV *resp;
108 AV *cbs;
109 } pk_ar_job;
110  
111 2 static pk_ar_job *pk_ar_job_new(pTHX_ SV *c, SV *resp, AV *cbs) {
112 pk_ar_job *j;
113 2 Newxz(j, 1, pk_ar_job);
114 2 j->c = SvREFCNT_inc_simple_NN(c);
115 2 j->resp = SvREFCNT_inc_simple_NN(resp);
116 2 j->cbs = cbs; /* takes the +1 */
117 2 return j;
118 }
119  
120 2 static void pk_ar_job_free(pTHX_ pk_ar_job *j) {
121 2 SvREFCNT_dec(j->c);
122 2 SvREFCNT_dec(j->resp);
123 2 SvREFCNT_dec((SV *)j->cbs);
124 2 Safefree(j);
125 2 }
126  
127 2 static void pk_ar_timer_cb_in(pTHX_ void *ud) {
128 2 pk_ar_job *j = (pk_ar_job *)ud;
129 2 pk_ar_run(aTHX_ j->c, j->resp, j->cbs);
130 2 pk_ar_job_free(aTHX_ j);
131 2 }
132  
133 2 static void pk_ar_timer_cb(pTHX_ void *ud) {
134 2 PK_LOOP_ENTER;
135 2 pk_ar_timer_cb_in(aTHX_ ud);
136 2 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 1 1 2
137 2 }
138  
139 /* The psgix.cleanup handler: the server calls it with the env, which this
140 * does not need - everything it runs on is in the capture. */
141 XS_INTERNAL(pk_ar_cleanup_cb);
142 1 XS_INTERNAL(pk_ar_cleanup_cb) {
143 1 dXSARGS;
144 1 AV *cap = punk_clos_cap(aTHX_ cv);
145 1 ▶ SV **c = cap ? av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **c = cap ? av_fetch(cap, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
146 1 ▶ SV **rp = cap ? av_fetch(cap, 1, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **rp = cap ? av_fetch(cap, 1, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
147 1 ▶ SV **cbs = cap ? av_fetch(cap, 2, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **cbs = cap ? av_fetch(cap, 2, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
148 PERL_UNUSED_VAR(items);
149 /*
150 # uncoverable branch false
151 # uncoverable condition right count:1 class:ignore_covered_err
152 # uncoverable condition right count:2 class:ignore_covered_err
153 # uncoverable condition right count:3 class:ignore_covered_err
154 # uncoverable condition right count:4 class:ignore_covered_err
155 # uncoverable condition right count:5 class:ignore_covered_err
156 # uncoverable condition right count:6 class:ignore_covered_err
157 # uncoverable condition right count:7 class:ignore_covered_err */
158 1 ▶ if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs))
Branch: 7 branches
BranchTrueFalseTotal
if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) 1 0 1
if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) 1 0 1
if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) 1 0 1
if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) 1 0 1
if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) 1 0 1
if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) 1 0 1
if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) 1 0 1
159 1 pk_ar_run(aTHX_ *c, *rp, (AV *)SvRV(*cbs));
160 1 XSRETURN_EMPTY;
161 }
162  
163 /* The callbacks for this response: the application's own hooks first, then
164 * anything the request queued, in the order they were registered. Returns a
165 * new AV (+1), or NULL when there is nothing to run - which is the answer for
166 * every request in an application that uses none of this. */
167 220964 static AV *pk_ar_collect(pTHX_ SV *c) {
168 220964 AV *av = pcx_av(aTHX_ c);
169 220964 SV *appsv = pcx_get(aTHX_ av, PCX_APP);
170 220964 SV *q = pcx_get(aTHX_ av, PCX_AFTER_RES);
171 220964 AV *out = NULL;
172 SSize_t i, n;
173  
174 /*
175 # uncoverable branch false
176 # uncoverable condition right count:1 class:ignore_covered_err
177 # uncoverable condition right count:2 class:ignore_covered_err
178 # uncoverable condition right count:3 class:ignore_covered_err */
179 220964 ▶ if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { 220964 0 220964
if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { 220964 0 220964
if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { 220964 0 220964
180 220964 SV **cp = hv_fetchs((HV *)SvRV(appsv), K_AFTER_RES_C, 0);
181 /*
182 # uncoverable condition right count:2 class:ignore_covered_err
183 # uncoverable condition right count:3 class:ignore_covered_err
184 # uncoverable condition right count:4 class:ignore_covered_err */
185 220964 ▶ if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) { 9 220955 220964
if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) { 9 0 9
if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) { 9 0 9
if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) { 9 0 9
186 9 AV *chain = (AV *)SvRV(*cp);
187 9 n = av_len(chain) + 1;
188 18 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 9 18
189 9 SV **e = av_fetch(chain, i, 0);
190 /*
191 # uncoverable branch true
192 # uncoverable condition left count:1 class:ignore_covered_err
193 # uncoverable condition left count:2 class:ignore_covered_err */
194 9 ▶ if (!e || !*e) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !*e) continue; 9 0 9
if (!e || !*e) continue; 0 9 9
195 /*
196 # uncoverable branch false
197 # uncoverable condition right count:1 class:ignore_covered_err */
198 9 ▶ if (!out) out = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!out) out = newAV(); 9 0 9
199 9 av_push(out, newSVsv(*e));
200 }
201 }
202 }
203 /*
204 # uncoverable condition right count:2 class:ignore_covered_err
205 # uncoverable condition right count:3 class:ignore_covered_err */
206 220964 ▶ if (q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV) { 6 220958 220964
if (q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV) { 6 0 6
if (q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV) { 6 0 6
207 6 AV *queue = (AV *)SvRV(q);
208 6 n = av_len(queue) + 1;
209 14 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 6 14
210 8 SV **e = av_fetch(queue, i, 0);
211 /*
212 # uncoverable branch true
213 # uncoverable condition left count:1 class:ignore_covered_err
214 # uncoverable condition left count:2 class:ignore_covered_err */
215 8 ▶ if (!e || !*e) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !*e) continue; 8 0 8
if (!e || !*e) continue; 0 8 8
216 /*
217 # uncoverable branch true
218 # uncoverable condition left count:1 class:ignore_covered_err */
219 8 ▶ if (!out) out = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!out) out = newAV(); 0 8 8
220 8 av_push(out, newSVsv(*e));
221 }
222 }
223 220964 return out;
224 }
225  
226 /* Called from punk_deliver, once, with the response that is about to be
227 * returned to the server. */
228 220964 static void pk_after_res(pTHX_ SV *c, SV *resp) {
229 220964 AV *cbs = pk_ar_collect(aTHX_ c);
230 AV *av;
231 SV *envsv;
232  
233 220964 ▶ if (!cbs) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cbs) return; 220955 9 220964
234 9 av = pcx_av(aTHX_ c);
235 9 envsv = pcx_get(aTHX_ av, PCX_ENV);
236  
237 /* 1. the server's own post-response phase */
238 /*
239 # uncoverable branch false
240 # uncoverable condition right count:1 class:ignore_covered_err
241 # uncoverable condition right count:2 class:ignore_covered_err
242 # uncoverable condition right count:3 class:ignore_covered_err */
243 9 ▶ if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 9 0 9
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 9 0 9
if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { 9 0 9
244 9 HV *env = (HV *)SvRV(envsv);
245 9 SV **cl = hv_fetchs(env, "psgix.cleanup", 0);
246 /*
247 # uncoverable condition right count:2 class:ignore_covered_err
248 # uncoverable condition right count:3 class:ignore_covered_err */
249 9 ▶ if (cl && *cl && SvTRUE(*cl)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cl && *cl && SvTRUE(*cl)) { 1 8 9
if (cl && *cl && SvTRUE(*cl)) { 1 0 1
if (cl && *cl && SvTRUE(*cl)) { 1 0 1
250 1 SV **hp = hv_fetchs(env, "psgix.cleanup.handlers", 0);
251 1 AV *handlers = NULL;
252 /*
253 # uncoverable branch true
254 # uncoverable condition left count:1 class:ignore_covered_err
255 # uncoverable condition left count:2 class:ignore_covered_err
256 # uncoverable condition right count:2 class:ignore_covered_err
257 # uncoverable condition left count:3 class:ignore_covered_err
258 # uncoverable condition right count:3 class:ignore_covered_err
259 # uncoverable condition left count:4 class:ignore_covered_err
260 # uncoverable condition right count:4 class:ignore_covered_err */
261 1 ▶ if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 0 1 1
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 0 0 0
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 0 0 0
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 0 0 0
262 0 handlers = (AV *)SvRV(*hp); /* # uncoverable statement */
263 else {
264 1 handlers = newAV();
265 1 (void)hv_stores(env, "psgix.cleanup.handlers",
266 newRV_noinc((SV *)handlers));
267 }
268 {
269 1 AV *cap = newAV();
270 1 av_push(cap, newSVsv(c));
271 1 av_push(cap, newSVsv(resp));
272 1 av_push(cap, newRV_noinc((SV *)cbs)); /* takes the +1 */
273 1 av_push(handlers, punk_closure(aTHX_ pk_ar_cleanup_cb, cap));
274 }
275 1 return;
276 }
277 }
278  
279 /* 2. the worker's loop */
280 {
281 8 const hm_abi *A = punk_hm(aTHX);
282 /*
283 # uncoverable condition right count:1 class:ignore_covered_err
284 # uncoverable condition right count:2 class:ignore_covered_err
285 # uncoverable condition right count:3 class:ignore_covered_err */
286 8 ▶ void *loop = (A && A->cur_loop && A->timer) ? A->cur_loop(aTHX) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
void *loop = (A && A->cur_loop && A->timer) ? A->cur_loop(aTHX) : NULL; 8 0 8
void *loop = (A && A->cur_loop && A->timer) ? A->cur_loop(aTHX) : NULL; 8 0 8
void *loop = (A && A->cur_loop && A->timer) ? A->cur_loop(aTHX) : NULL; 8 0 8
287 8 ▶ if (loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop) { 2 6 8
288 2 (void)A->timer(aTHX_ loop, 0.0, pk_ar_timer_cb,
289 2 pk_ar_job_new(aTHX_ c, resp, cbs));
290 2 return;
291 }
292 }
293  
294 /* 3. here, now */
295 6 pk_ar_run(aTHX_ c, resp, cbs);
296 6 SvREFCNT_dec((SV *)cbs);
297 }
298  
299 #endif /* PUNK_AFTERRES_H */
« xs/health.xs ↑ Summary xs/blob.xs »