| 1 |
|
| /* punk_idem.h - Punk::Plugin::Idempotency. |
| 2 |
|
| * |
| 3 |
|
| * plugin 'Idempotency' => { scope => sub { $_[0]->current_user->{id} } }; |
| 4 |
|
| * post '/orders' => sub { ... }, { idempotent => 1 }; |
| 5 |
|
| * |
| 6 |
|
| * A client that sends POST /orders and loses the connection cannot know |
| 7 |
|
| * whether the order was created. It has two choices and both are bad: retry |
| 8 |
|
| * and risk two orders, or give up and risk none. An `Idempotency-Key` |
| 9 |
|
| * removes the choice - the server recognises the retry and replays the first |
| 10 |
|
| * response. |
| 11 |
|
| * |
| 12 |
|
| * ---- what this guarantees, exactly ------------------------------------- |
| 13 |
|
| * |
| 14 |
|
| * A retry carrying the same key, WITHIN THE TTL, that reaches a worker able |
| 15 |
|
| * to see the store, AFTER the first request's response was recorded, replays |
| 16 |
|
| * that response instead of executing the work again. |
| 17 |
|
| * |
| 18 |
|
| * Every clause is load-bearing, and the last one is a window: between the |
| 19 |
|
| * handler committing the order and the entry reaching the store, this |
| 20 |
|
| * provides nothing. A process killed there leaves an order created and no |
| 21 |
|
| * key recorded, so the retry executes and creates a second one - the exact |
| 22 |
|
| * failure the plugin exists to prevent, in the one situation where a client |
| 23 |
|
| * is most likely to retry. |
| 24 |
|
| * |
| 25 |
|
| * CACHE-BACKED IDEMPOTENCY COLLAPSES THAT WINDOW. IT DOES NOT REMOVE IT. |
| 26 |
|
| * Closing it needs the key written inside the same transaction as the work, |
| 27 |
|
| * which means the store has to BE the database - and the plugin does not own |
| 28 |
|
| * the handler's transaction. So the store is reached through a narrow seam |
| 29 |
|
| * (read, write, lock) that a database-backed implementation could take over, |
| 30 |
|
| * rather than through Punk::Cache calls scattered across this file. |
| 31 |
|
| * |
| 32 |
|
| * ---- where the two halves run ------------------------------------------ |
| 33 |
|
| * |
| 34 |
|
| * Replay is between a route's GUARDS and its handler. Not before_dispatch, |
| 35 |
|
| * which runs ahead of the guards: a replay returns a STORED RESPONSE BODY, |
| 36 |
|
| * so answering there hands somebody else's order to a caller the guard was |
| 37 |
|
| * about to refuse. Not a timing leak - the document. |
| 38 |
|
| * |
| 39 |
|
| * Recording is last on the after-dispatch chain, so what is stored is what |
| 40 |
|
| * was actually sent, after every application hook has had its say. |
| 41 |
|
| * |
| 42 |
|
| * Include after punk_cachefront.h (the store), punk_request.h (pq_body) and |
| 43 |
|
| * punk_session.h (ps_stash). |
| 44 |
|
| */ |
| 45 |
|
| |
| 46 |
|
| #ifndef PUNK_IDEM_H |
| 47 |
|
| #define PUNK_IDEM_H |
| 48 |
|
| |
| 49 |
|
| #define PI_STASH_KEY "punk.idem" |
| 50 |
|
| #define PI_HEADER "HTTP_IDEMPOTENCY_KEY" |
| 51 |
|
| |
| 52 |
|
| /* ---- the inbound key is request bytes --------------------------------------- |
| 53 |
|
| * |
| 54 |
|
| * It reaches a cache key, and with the file store a cache key reaches the |
| 55 |
|
| * filesystem. That is the bug class this workspace keeps meeting |
| 56 |
|
| * (CVE-2026-75628, the markdown 301, the Reverse::Proxy smuggling fix), so |
| 57 |
|
| * the rules are Punk::Plugin::RequestId's for an adopted id, for the same |
| 58 |
|
| * reasons: bounded, a conservative charset, and REFUSED rather than repaired. |
| 59 |
|
| * |
| 60 |
|
| * Repairing is the subtle half. Trimming a bad key into a good one means two |
| 61 |
|
| * different client keys can become one server key, which is the collision |
| 62 |
|
| * this whole file exists to prevent. */ |
| 63 |
56 |
| static int pi_key_valid(const char *k, STRLEN l) { |
| 64 |
|
| STRLEN i; |
| 65 |
56 |
▶ | if (l < 1 || l > 255) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (l < 1 || l > 255) return 0; |
56 |
0 |
56 |
| if (l < 1 || l > 255) return 0; |
1 |
55 |
56 |
|
| 66 |
532 |
▶ | for (i = 0; i < l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) { |
484 |
48 |
532 |
|
| 67 |
484 |
| unsigned char c = (unsigned char)k[i]; |
| 68 |
484 |
▶ | if (c <= 0x20 || c >= 0x7f) return 0; /* no CR, LF, NUL, tab, space */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (c <= 0x20 || c >= 0x7f) return 0; /* no CR, LF, NUL, tab, space */ |
478 |
6 |
484 |
| if (c <= 0x20 || c >= 0x7f) return 0; /* no CR, LF, NUL, tab, space */ |
1 |
477 |
478 |
|
| 69 |
|
| } |
| 70 |
48 |
| return 1; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| /* The methods a key means anything on. GET is already idempotent, and |
| 74 |
|
| * storing responses to it is Punk::Cache - a different feature with expiry |
| 75 |
|
| * semantics an application chooses deliberately. */ |
| 76 |
57 |
| static int pi_unsafe_method(const char *m, STRLEN l) { |
| 77 |
56 |
▶ | return (l == 4 && memEQ(m, "POST", 4)) |
Branch: 1 branch
| Branch | True | False | Total |
| return (l == 4 && memEQ(m, "POST", 4)) |
0 |
56 |
56 |
|
| 78 |
1 |
▶ | || (l == 3 && memEQ(m, "PUT", 3)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (l == 3 && memEQ(m, "PUT", 3)) |
1 |
0 |
1 |
| || (l == 3 && memEQ(m, "PUT", 3)) |
0 |
1 |
1 |
|
| 79 |
0 |
▶ | || (l == 5 && memEQ(m, "PATCH", 5)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (l == 5 && memEQ(m, "PATCH", 5)) |
0 |
0 |
0 |
| || (l == 5 && memEQ(m, "PATCH", 5)) |
0 |
0 |
0 |
|
| 80 |
114 |
▶ | || (l == 6 && memEQ(m, "DELETE", 6)); |
Branch: 3 branches
| Branch | True | False | Total |
| || (l == 6 && memEQ(m, "DELETE", 6)); |
56 |
1 |
57 |
| || (l == 6 && memEQ(m, "DELETE", 6)); |
0 |
0 |
0 |
| || (l == 6 && memEQ(m, "DELETE", 6)); |
0 |
0 |
0 |
|
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
92 |
| static void pi_sha256_hex(pTHX_ const char *p, STRLEN l, char *out) { |
| 84 |
|
| unsigned char sum[32]; |
| 85 |
92 |
| pk_sha256((const unsigned char *)p, (size_t)l, sum); |
| 86 |
92 |
| pa_hex(sum, 16, out); /* 32 hex characters */ |
| 87 |
92 |
| } |
| 88 |
|
| |
| 89 |
|
| /* A warn line on the request logger, so it carries the request id and the |
| 90 |
|
| * method and path the rest of the log has. Built the way $c->log builds it, |
| 91 |
|
| * and cached in the stash the same way, so this does not make a second |
| 92 |
|
| * logger per request. */ |
| 93 |
8 |
| static void pi_warn(pTHX_ SV *c, const char *msg) { |
| 94 |
8 |
| AV *av = pcx_av(aTHX_ c); |
| 95 |
8 |
| SV *st, *lg = NULL; |
| 96 |
|
| HV *stash; |
| 97 |
|
| SV **cached; |
| 98 |
8 |
▶ | if (!av) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!av) return; |
0 |
8 |
8 |
|
| 99 |
8 |
| st = pcx_get(aTHX_ av, PCX_STASH); |
| 100 |
8 |
▶ | if (!st) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!st) { |
2 |
6 |
8 |
|
| 101 |
2 |
| st = newRV_noinc((SV *)newHV()); |
| 102 |
2 |
| (void)av_store(av, PCX_STASH, st); |
| 103 |
|
| } |
| 104 |
8 |
| stash = (HV *)SvRV(st); |
| 105 |
8 |
| cached = hv_fetchs(stash, "punk.logger", 0); |
| 106 |
8 |
▶ | if (cached && *cached && SvROK(*cached)) lg = *cached; |
Branch: 3 branches
| Branch | True | False | Total |
| if (cached && *cached && SvROK(*cached)) lg = *cached; |
0 |
8 |
8 |
| if (cached && *cached && SvROK(*cached)) lg = *cached; |
0 |
0 |
0 |
| if (cached && *cached && SvROK(*cached)) lg = *cached; |
0 |
0 |
0 |
|
| 107 |
|
| else { |
| 108 |
8 |
| lg = pl_make_logger(aTHX_ pcx_get(aTHX_ av, PCX_APP), c); |
| 109 |
8 |
| (void)hv_stores(stash, "punk.logger", newSVsv(lg)); |
| 110 |
8 |
| lg = *hv_fetchs(stash, "punk.logger", 0); |
| 111 |
|
| } |
| 112 |
8 |
| pl_emit(aTHX_ lg, PL_WARN, sv_2mortal(newSVpv(msg, 0)), NULL); |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
|
| /* ---- the composite key ------------------------------------------------------ |
| 116 |
|
| * |
| 117 |
|
| * idem:<scope>:<METHOD> <declared path>:<hash of the client key> |
| 118 |
|
| * |
| 119 |
|
| * The scope, because one account's key must never collide with another's - |
| 120 |
|
| * the stored value is a whole response, so a collision is a way to read |
| 121 |
|
| * somebody else's order by guessing a UUID. |
| 122 |
|
| * |
| 123 |
|
| * The route as DECLARED (/orders/:id, not /orders/7), because the same key |
| 124 |
|
| * against two endpoints is a client bug that must surface rather than replay |
| 125 |
|
| * one endpoint's answer for the other. The method is in there with it: |
| 126 |
|
| * POST /orders and DELETE /orders are different operations a client could |
| 127 |
|
| * legitimately key the same way. |
| 128 |
|
| * |
| 129 |
|
| * The client's key HASHED rather than interpolated. The file store already |
| 130 |
|
| * hashes and shards its keys - a key never becomes a path there - but this |
| 131 |
|
| * does not depend on that: a backend that stored keys more literally would |
| 132 |
|
| * still get 32 hex characters from us. */ |
| 133 |
46 |
| static SV *pi_composite(pTHX_ SV *scope, HV *rech, const char *method, |
| 134 |
|
| STRLEN mlen, const char *key, STRLEN klen) { |
| 135 |
|
| char hex[33]; |
| 136 |
46 |
| SV **pp = hv_fetchs(rech, K_PATH, 0); |
| 137 |
46 |
| SV *out = newSVpvs("idem:"); |
| 138 |
46 |
| pi_sha256_hex(aTHX_ key, klen, hex); |
| 139 |
46 |
▶ | if (scope && SvOK(scope)) sv_catsv(out, scope); |
Branch: 2 branches
| Branch | True | False | Total |
| if (scope && SvOK(scope)) sv_catsv(out, scope); |
46 |
0 |
46 |
| if (scope && SvOK(scope)) sv_catsv(out, scope); |
46 |
0 |
46 |
|
| 140 |
46 |
| sv_catpvs(out, ":"); |
| 141 |
46 |
| sv_catpvn(out, method, mlen); |
| 142 |
46 |
| sv_catpvs(out, " "); |
| 143 |
46 |
▶ | if (pp && *pp && SvOK(*pp)) sv_catsv(out, *pp); |
Branch: 3 branches
| Branch | True | False | Total |
| if (pp && *pp && SvOK(*pp)) sv_catsv(out, *pp); |
46 |
0 |
46 |
| if (pp && *pp && SvOK(*pp)) sv_catsv(out, *pp); |
46 |
0 |
46 |
| if (pp && *pp && SvOK(*pp)) sv_catsv(out, *pp); |
46 |
0 |
46 |
|
| 144 |
46 |
| sv_catpvs(out, ":"); |
| 145 |
46 |
| sv_catpvn(out, hex, 32); |
| 146 |
46 |
| return out; |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
|
| /* The request fingerprint: the method, the declared route, the path and |
| 150 |
|
| * query string as they were ASKED FOR, and the body. |
| 151 |
|
| * |
| 152 |
|
| * A key reused with a DIFFERENT body is a client bug, and a silent replay of |
| 153 |
|
| * an unrelated response is the worst possible answer - the client believes |
| 154 |
|
| * its second, different order succeeded, and it never ran. |
| 155 |
|
| * |
| 156 |
|
| * The same goes for a different resource. The declared route is |
| 157 |
|
| * /orders/:id/cancel for order 7 and for order 8, so without the real path |
| 158 |
|
| * a client that keys per operation and varies the id was handed 7's answer |
| 159 |
|
| * for 8, which was never cancelled. They are in the fingerprint and not the |
| 160 |
|
| * key, so that reuse is the 422 and not a second, separate record. |
| 161 |
|
| * |
| 162 |
|
| * $c->req->body reads once and rewinds, so this costs one hash of bytes |
| 163 |
|
| * already in memory rather than a second read of the socket. */ |
| 164 |
46 |
| static SV *pi_fingerprint(pTHX_ SV *c, HV *rech, HV *env, const char *method, |
| 165 |
|
| STRLEN mlen) { |
| 166 |
46 |
| AV *cav = pcx_av(aTHX_ c); |
| 167 |
46 |
▶ | SV *reqsv = cav ? pcx_req(aTHX_ cav) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *reqsv = cav ? pcx_req(aTHX_ cav) : NULL; |
46 |
0 |
46 |
|
| 168 |
46 |
| SV *acc = newSVpvn(method, mlen); |
| 169 |
46 |
| SV **pp = hv_fetchs(rech, K_PATH, 0); |
| 170 |
|
| char hex[33]; |
| 171 |
46 |
| sv_catpvs(acc, " "); |
| 172 |
46 |
▶ | if (pp && *pp && SvOK(*pp)) sv_catsv(acc, *pp); |
Branch: 3 branches
| Branch | True | False | Total |
| if (pp && *pp && SvOK(*pp)) sv_catsv(acc, *pp); |
46 |
0 |
46 |
| if (pp && *pp && SvOK(*pp)) sv_catsv(acc, *pp); |
46 |
0 |
46 |
| if (pp && *pp && SvOK(*pp)) sv_catsv(acc, *pp); |
46 |
0 |
46 |
|
| 173 |
46 |
| sv_catpvs(acc, "\n"); |
| 174 |
|
| { |
| 175 |
46 |
▶ | SV **pi = env ? hv_fetchs(env, "PATH_INFO", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **pi = env ? hv_fetchs(env, "PATH_INFO", 0) : NULL; |
46 |
0 |
46 |
|
| 176 |
46 |
▶ | SV **qs = env ? hv_fetchs(env, "QUERY_STRING", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **qs = env ? hv_fetchs(env, "QUERY_STRING", 0) : NULL; |
46 |
0 |
46 |
|
| 177 |
46 |
▶ | if (pi && *pi && SvOK(*pi)) sv_catsv(acc, *pi); |
Branch: 3 branches
| Branch | True | False | Total |
| if (pi && *pi && SvOK(*pi)) sv_catsv(acc, *pi); |
46 |
0 |
46 |
| if (pi && *pi && SvOK(*pi)) sv_catsv(acc, *pi); |
46 |
0 |
46 |
| if (pi && *pi && SvOK(*pi)) sv_catsv(acc, *pi); |
46 |
0 |
46 |
|
| 178 |
46 |
| sv_catpvs(acc, "\n"); |
| 179 |
46 |
▶ | if (qs && *qs && SvOK(*qs)) sv_catsv(acc, *qs); |
Branch: 3 branches
| Branch | True | False | Total |
| if (qs && *qs && SvOK(*qs)) sv_catsv(acc, *qs); |
46 |
0 |
46 |
| if (qs && *qs && SvOK(*qs)) sv_catsv(acc, *qs); |
46 |
0 |
46 |
| if (qs && *qs && SvOK(*qs)) sv_catsv(acc, *qs); |
46 |
0 |
46 |
|
| 180 |
46 |
| sv_catpvs(acc, "\n"); |
| 181 |
|
| } |
| 182 |
46 |
▶ | if (reqsv && SvROK(reqsv) && SvTYPE(SvRV(reqsv)) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (reqsv && SvROK(reqsv) && SvTYPE(SvRV(reqsv)) == SVt_PVAV) { |
46 |
0 |
46 |
| if (reqsv && SvROK(reqsv) && SvTYPE(SvRV(reqsv)) == SVt_PVAV) { |
46 |
0 |
46 |
| if (reqsv && SvROK(reqsv) && SvTYPE(SvRV(reqsv)) == SVt_PVAV) { |
46 |
0 |
46 |
|
| 183 |
46 |
| SV *b = pq_body(aTHX_ (AV *)SvRV(reqsv)); |
| 184 |
46 |
▶ | if (b && SvOK(b)) sv_catsv(acc, b); |
Branch: 2 branches
| Branch | True | False | Total |
| if (b && SvOK(b)) sv_catsv(acc, b); |
46 |
0 |
46 |
| if (b && SvOK(b)) sv_catsv(acc, b); |
31 |
15 |
46 |
|
| 185 |
|
| } |
| 186 |
46 |
| pi_sha256_hex(aTHX_ SvPVX(acc), SvCUR(acc), hex); |
| 187 |
46 |
| SvREFCNT_dec(acc); |
| 188 |
46 |
| return newSVpvn(hex, 32); |
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
|
| /* ---- the store seam --------------------------------------------------------- |
| 192 |
|
| * Three operations, and nothing else reaches the cache from here: read, write |
| 193 |
|
| * with a TTL, and a best-effort lock. A database-backed store closing the |
| 194 |
|
| * window would implement these three. */ |
| 195 |
|
| |
| 196 |
76 |
| static punk_cachefront *pi_store(pTHX_ SV *c, SV **errp) { |
| 197 |
76 |
| AV *cav = pcx_av(aTHX_ c); |
| 198 |
76 |
▶ | SV *app = cav ? pcx_get(aTHX_ cav, PCX_APP) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *app = cav ? pcx_get(aTHX_ cav, PCX_APP) : NULL; |
76 |
0 |
76 |
|
| 199 |
76 |
| SV **built = NULL, **slot = NULL; |
| 200 |
76 |
| *errp = NULL; |
| 201 |
76 |
▶ | if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) |
76 |
0 |
76 |
| if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) |
76 |
0 |
76 |
| if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) |
76 |
0 |
76 |
|
| 202 |
76 |
| built = hv_fetchs((HV *)SvRV(app), "cache", 0); |
| 203 |
76 |
▶ | if (built && *built && SvROK(*built) && SvTYPE(SvRV(*built)) == SVt_PVHV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (built && *built && SvROK(*built) && SvTYPE(SvRV(*built)) == SVt_PVHV) |
76 |
0 |
76 |
| if (built && *built && SvROK(*built) && SvTYPE(SvRV(*built)) == SVt_PVHV) |
76 |
0 |
76 |
| if (built && *built && SvROK(*built) && SvTYPE(SvRV(*built)) == SVt_PVHV) |
76 |
0 |
76 |
| if (built && *built && SvROK(*built) && SvTYPE(SvRV(*built)) == SVt_PVHV) |
76 |
0 |
76 |
|
| 204 |
76 |
| slot = hv_fetchs((HV *)SvRV(*built), K_DEFAULT, 0); |
| 205 |
76 |
▶ | if (!(slot && *slot && SvOK(*slot))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(slot && *slot && SvOK(*slot))) { |
76 |
0 |
76 |
| if (!(slot && *slot && SvOK(*slot))) { |
76 |
0 |
76 |
| if (!(slot && *slot && SvOK(*slot))) { |
0 |
76 |
76 |
|
| 206 |
0 |
| *errp = newSVpvs("Punk::Plugin::Idempotency: no cache configured - " |
| 207 |
|
| "add a `cache` keyword for the keys to live in\n"); |
| 208 |
0 |
| return NULL; |
| 209 |
|
| } |
| 210 |
76 |
| return pkc_of(aTHX_ *slot); |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
|
| /* ---- the stored entry ------------------------------------------------------- |
| 214 |
|
| * [ status, [ header pairs ], body, fingerprint ], JSON through the ABI Punk |
| 215 |
|
| * already consumes. The cache stores bytes; this decodes at the edge. */ |
| 216 |
|
| |
| 217 |
24 |
| static SV *pi_encode(pTHX_ AV *ra, SV *fp) { |
| 218 |
24 |
| AV *out = newAV(); |
| 219 |
24 |
| SV **st = av_fetch(ra, 0, 0), **hp = av_fetch(ra, 1, 0), |
| 220 |
24 |
| **bp = av_fetch(ra, 2, 0); |
| 221 |
24 |
| SV *body = newSVpvs(""); |
| 222 |
24 |
▶ | av_push(out, newSViv(st && *st ? SvIV(*st) : 200)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(out, newSViv(st && *st ? SvIV(*st) : 200)); |
24 |
0 |
24 |
| av_push(out, newSViv(st && *st ? SvIV(*st) : 200)); |
24 |
0 |
24 |
|
| 223 |
48 |
▶ | if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
24 |
0 |
24 |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
24 |
0 |
24 |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
24 |
0 |
24 |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
24 |
0 |
24 |
|
| 224 |
24 |
| AV *h = (AV *)SvRV(*hp), *h2 = newAV(); |
| 225 |
24 |
| SSize_t i, n = av_len(h) + 1; |
| 226 |
138 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
114 |
24 |
138 |
|
| 227 |
114 |
| SV **e = av_fetch(h, i, 0); |
| 228 |
114 |
▶ | av_push(h2, (e && *e) ? newSVsv(*e) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(h2, (e && *e) ? newSVsv(*e) : newSV(0)); |
114 |
0 |
114 |
| av_push(h2, (e && *e) ? newSVsv(*e) : newSV(0)); |
114 |
0 |
114 |
|
| 229 |
|
| } |
| 230 |
24 |
| av_push(out, newRV_noinc((SV *)h2)); |
| 231 |
|
| } |
| 232 |
0 |
| else av_push(out, newRV_noinc((SV *)newAV())); |
| 233 |
24 |
▶ | if (bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV) { |
24 |
0 |
24 |
| if (bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV) { |
24 |
0 |
24 |
| if (bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV) { |
24 |
0 |
24 |
| if (bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV) { |
24 |
0 |
24 |
|
| 234 |
24 |
| AV *b = (AV *)SvRV(*bp); |
| 235 |
24 |
| SSize_t i, n = av_len(b) + 1; |
| 236 |
48 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
24 |
24 |
48 |
|
| 237 |
24 |
| SV **e = av_fetch(b, i, 0); |
| 238 |
24 |
▶ | if (e && *e && SvOK(*e)) sv_catsv(body, *e); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) sv_catsv(body, *e); |
24 |
0 |
24 |
| if (e && *e && SvOK(*e)) sv_catsv(body, *e); |
24 |
0 |
24 |
| if (e && *e && SvOK(*e)) sv_catsv(body, *e); |
24 |
0 |
24 |
|
| 239 |
|
| } |
| 240 |
|
| } |
| 241 |
24 |
| av_push(out, body); |
| 242 |
24 |
| av_push(out, newSVsv(fp)); |
| 243 |
24 |
| return sv_2mortal(newRV_noinc((SV *)out)); |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
|
| /* The replay triplet from a stored entry (+1), or NULL when the entry is not |
| 247 |
|
| * the shape this wrote (a store shared with something else, or a format from |
| 248 |
|
| * a previous version - a miss is always safer than a guess). */ |
| 249 |
16 |
| static SV *pi_replay(pTHX_ SV *entry, SV *fp_now, int *conflict) { |
| 250 |
|
| AV *e; |
| 251 |
|
| SV **st, **hp, **bp, **fp; |
| 252 |
|
| AV *resp, *hdr, *body; |
| 253 |
16 |
| *conflict = 0; |
| 254 |
16 |
▶ | if (!(entry && SvROK(entry) && SvTYPE(SvRV(entry)) == SVt_PVAV)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(entry && SvROK(entry) && SvTYPE(SvRV(entry)) == SVt_PVAV)) return NULL; |
16 |
0 |
16 |
| if (!(entry && SvROK(entry) && SvTYPE(SvRV(entry)) == SVt_PVAV)) return NULL; |
16 |
0 |
16 |
| if (!(entry && SvROK(entry) && SvTYPE(SvRV(entry)) == SVt_PVAV)) return NULL; |
0 |
16 |
16 |
|
| 255 |
16 |
| e = (AV *)SvRV(entry); |
| 256 |
16 |
▶ | if (av_len(e) < 3) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(e) < 3) return NULL; |
0 |
16 |
16 |
|
| 257 |
16 |
| st = av_fetch(e, 0, 0); hp = av_fetch(e, 1, 0); |
| 258 |
16 |
| bp = av_fetch(e, 2, 0); fp = av_fetch(e, 3, 0); |
| 259 |
16 |
▶ | if (!(st && *st && hp && *hp && bp && *bp && fp && *fp)) return NULL; |
Branch: 8 branches
| Branch | True | False | Total |
| if (!(st && *st && hp && *hp && bp && *bp && fp && *fp)) return NULL; |
16 |
0 |
16 |
| if (!(st && *st && hp && *hp && bp && *bp && fp && *fp)) return NULL; |
16 |
0 |
16 |
| if (!(st && *st && hp && *hp && bp && *bp && fp && *fp)) return NULL; |
16 |
0 |
16 |
| if (!(st && *st && hp && *hp && bp && *bp && fp && *fp)) return NULL; |
16 |
0 |
16 |
| if (!(st && *st && hp && *hp && bp && *bp && fp && *fp)) return NULL; |
16 |
0 |
16 |
| if (!(st && *st && hp && *hp && bp && *bp && fp && *fp)) return NULL; |
16 |
0 |
16 |
| if (!(st && *st && hp && *hp && bp && *bp && fp && *fp)) return NULL; |
16 |
0 |
16 |
| if (!(st && *st && hp && *hp && bp && *bp && fp && *fp)) return NULL; |
0 |
16 |
16 |
|
| 260 |
|
| |
| 261 |
|
| /* the same key attached to a different request: a client bug, and the |
| 262 |
|
| * one answer that must never be a replay */ |
| 263 |
16 |
▶ | if (!sv_eq(*fp, fp_now)) { *conflict = 1; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_eq(*fp, fp_now)) { *conflict = 1; return NULL; } |
3 |
13 |
16 |
|
| 264 |
|
| |
| 265 |
13 |
| hdr = newAV(); |
| 266 |
13 |
▶ | if (SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
13 |
0 |
13 |
| if (SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
13 |
0 |
13 |
|
| 267 |
13 |
| AV *h = (AV *)SvRV(*hp); |
| 268 |
13 |
| SSize_t i, n = av_len(h) + 1; |
| 269 |
42 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
29 |
13 |
42 |
|
| 270 |
29 |
| SV **k = av_fetch(h, i, 0); |
| 271 |
29 |
| SV **v = av_fetch(h, i + 1, 0); |
| 272 |
|
| STRLEN kl; |
| 273 |
|
| const char *kp; |
| 274 |
30 |
▶ | if (!(k && *k && SvOK(*k))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(k && *k && SvOK(*k))) continue; |
29 |
0 |
29 |
| if (!(k && *k && SvOK(*k))) continue; |
29 |
0 |
29 |
| if (!(k && *k && SvOK(*k))) continue; |
0 |
29 |
29 |
|
| 275 |
29 |
| kp = SvPV_const(*k, kl); |
| 276 |
|
| /* A stored Set-Cookie is NOT replayed, and this is the one |
| 277 |
|
| * deliberate exception to "return what they got". That cookie |
| 278 |
|
| * carries the first request's session state; handing it to a |
| 279 |
|
| * retry - which may arrive from a newer session, or another tab - |
| 280 |
|
| * writes back a stale session. The entry keeps it, because the |
| 281 |
|
| * entry is a faithful record of what was sent; the replay drops |
| 282 |
|
| * it. */ |
| 283 |
29 |
▶ | if (kl == 10 && foldEQ(kp, "Set-Cookie", 10)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (kl == 10 && foldEQ(kp, "Set-Cookie", 10)) continue; |
1 |
28 |
29 |
| if (kl == 10 && foldEQ(kp, "Set-Cookie", 10)) continue; |
1 |
0 |
1 |
|
| 284 |
28 |
| av_push(hdr, newSVsv(*k)); |
| 285 |
28 |
▶ | av_push(hdr, (v && *v) ? newSVsv(*v) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(hdr, (v && *v) ? newSVsv(*v) : newSV(0)); |
28 |
0 |
28 |
| av_push(hdr, (v && *v) ? newSVsv(*v) : newSV(0)); |
28 |
0 |
28 |
|
| 286 |
|
| } |
| 287 |
|
| } |
| 288 |
|
| /* A client that cannot tell a replay from a fresh execution cannot debug |
| 289 |
|
| * anything, and neither can the application's own log. */ |
| 290 |
13 |
| av_push(hdr, newSVpvs("Idempotency-Replayed")); |
| 291 |
13 |
| av_push(hdr, newSVpvs("true")); |
| 292 |
|
| |
| 293 |
13 |
| body = newAV(); |
| 294 |
13 |
| av_push(body, newSVsv(*bp)); |
| 295 |
13 |
| resp = newAV(); |
| 296 |
13 |
| av_push(resp, newSViv(SvIV(*st))); |
| 297 |
13 |
| av_push(resp, newRV_noinc((SV *)hdr)); |
| 298 |
13 |
| av_push(resp, newRV_noinc((SV *)body)); |
| 299 |
13 |
| return newRV_noinc((SV *)resp); |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
|
| /* A small JSON error triplet, the shape the dispatcher's own 404 and 405 |
| 303 |
|
| * use, so a refusal from here does not look foreign beside them. */ |
| 304 |
11 |
| static SV *pi_error(pTHX_ IV status, const char *msg) { |
| 305 |
11 |
| SV *body = newSVpvs("{\"errors\":[{\"message\":\""); |
| 306 |
11 |
| AV *resp = newAV(), *hdr = newAV(), *bav = newAV(); |
| 307 |
11 |
| sv_catpv(body, (char *)msg); |
| 308 |
11 |
| sv_catpvs(body, "\"}]}"); |
| 309 |
11 |
| av_push(hdr, newSVpvs("Content-Type")); |
| 310 |
11 |
| av_push(hdr, newSVpvs("application/json")); |
| 311 |
11 |
| av_push(hdr, newSVpvs("Content-Length")); |
| 312 |
11 |
| av_push(hdr, newSViv((IV)SvCUR(body))); |
| 313 |
11 |
| av_push(bav, body); |
| 314 |
11 |
| av_push(resp, newSViv(status)); |
| 315 |
11 |
| av_push(resp, newRV_noinc((SV *)hdr)); |
| 316 |
11 |
| av_push(resp, newRV_noinc((SV *)bav)); |
| 317 |
11 |
| return newRV_noinc((SV *)resp); |
| 318 |
|
| } |
| 319 |
|
| |
| 320 |
|
| /* How long a single-flight lock is honoured, and how long a retry waits for |
| 321 |
|
| * the first attempt, unless the plugin was given `lock_wait`. */ |
| 322 |
|
| #define PI_LOCK_WAIT 30.0 |
| 323 |
|
| |
| 324 |
|
| /* ---- the replay check, between the guards and the handler -------------------- |
| 325 |
|
| * |
| 326 |
|
| * Returns 1 when the request has been ANSWERED (*out) and the handler must be |
| 327 |
|
| * skipped; 0 to carry on, having left what the recorder needs in the stash. |
| 328 |
|
| */ |
| 329 |
57 |
| static int pi_check(pTHX_ SV *c, HV *rech, HV *env, const char *method, |
| 330 |
|
| STRLEN mlen, SV *cfg, SV **out, SV **errp) { |
| 331 |
|
| SV **kp; |
| 332 |
|
| const char *key; |
| 333 |
|
| STRLEN klen; |
| 334 |
57 |
| SV *scope = NULL, *comp, *fp, *entry = NULL; |
| 335 |
|
| punk_cachefront *f; |
| 336 |
|
| HV *ocfg; |
| 337 |
|
| |
| 338 |
57 |
| *out = NULL; *errp = NULL; |
| 339 |
57 |
▶ | if (!(cfg && SvROK(cfg) && SvTYPE(SvRV(cfg)) == SVt_PVHV)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(cfg && SvROK(cfg) && SvTYPE(SvRV(cfg)) == SVt_PVHV)) return 0; |
57 |
0 |
57 |
| if (!(cfg && SvROK(cfg) && SvTYPE(SvRV(cfg)) == SVt_PVHV)) return 0; |
57 |
0 |
57 |
| if (!(cfg && SvROK(cfg) && SvTYPE(SvRV(cfg)) == SVt_PVHV)) return 0; |
0 |
57 |
57 |
|
| 340 |
57 |
| ocfg = (HV *)SvRV(cfg); |
| 341 |
57 |
▶ | if (!pi_unsafe_method(method, mlen)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pi_unsafe_method(method, mlen)) return 0; |
0 |
57 |
57 |
|
| 342 |
|
| |
| 343 |
57 |
| kp = hv_fetchs(env, PI_HEADER, 0); |
| 344 |
|
| /* No key is not an error. Requiring one is an API decision an |
| 345 |
|
| * application makes in a guard - some endpoints want to refuse an |
| 346 |
|
| * unkeyed write, most do not care - and a plugin that imposed it would |
| 347 |
|
| * break the second kind. */ |
| 348 |
57 |
▶ | if (!(kp && *kp && SvOK(*kp) && SvCUR(*kp))) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(kp && *kp && SvOK(*kp) && SvCUR(*kp))) return 0; |
56 |
1 |
57 |
| if (!(kp && *kp && SvOK(*kp) && SvCUR(*kp))) return 0; |
56 |
0 |
56 |
| if (!(kp && *kp && SvOK(*kp) && SvCUR(*kp))) return 0; |
56 |
0 |
56 |
| if (!(kp && *kp && SvOK(*kp) && SvCUR(*kp))) return 0; |
0 |
56 |
56 |
|
| 349 |
56 |
| key = SvPV_const(*kp, klen); |
| 350 |
56 |
▶ | if (!pi_key_valid(key, klen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pi_key_valid(key, klen)) { |
8 |
48 |
56 |
|
| 351 |
8 |
| *out = pi_error(aTHX_ 400, |
| 352 |
|
| "Idempotency-Key must be 1 to 255 printable ASCII characters " |
| 353 |
|
| "with no space"); |
| 354 |
8 |
| return 1; |
| 355 |
|
| } |
| 356 |
|
| |
| 357 |
|
| { /* the scope: the application's, and there is no default. The plugin |
| 358 |
|
| * cannot know what an account is - a session, an auth identity, an |
| 359 |
|
| * OAuth2 token, an API key are four this workspace already ships - |
| 360 |
|
| * and inventing one means picking one and being silently wrong for |
| 361 |
|
| * the rest. */ |
| 362 |
48 |
| SV **sc = hv_fetchs(ocfg, "scope", 0); |
| 363 |
48 |
| dSP; int count; |
| 364 |
48 |
▶ | if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
48 |
0 |
48 |
| if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
48 |
0 |
48 |
| if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
48 |
0 |
48 |
| if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVCV)) |
0 |
48 |
48 |
|
| 365 |
0 |
| return 0; |
| 366 |
48 |
| ENTER; SAVETMPS; |
| 367 |
48 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
48 |
48 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
48 |
48 |
|
| 368 |
48 |
| count = call_sv(*sc, G_SCALAR | G_EVAL); |
| 369 |
48 |
| SPAGAIN; |
| 370 |
48 |
▶ | scope = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| scope = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
48 |
0 |
48 |
|
| 371 |
48 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
48 |
0 |
48 |
|
| 372 |
48 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
48 |
0 |
48 |
| if (SvTRUE(ERRSV)) { |
0 |
48 |
48 |
|
| 373 |
0 |
| SvREFCNT_dec(scope); |
| 374 |
0 |
▶ | *errp = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| *errp = newSVsv(ERRSV); |
0 |
0 |
0 |
|
| 375 |
0 |
| return 1; |
| 376 |
|
| } |
| 377 |
|
| } |
| 378 |
|
| |
| 379 |
|
| /* An undefined scope means the plugin cannot say whose key this is, and |
| 380 |
|
| * it will not invent an answer: the request proceeds as though the |
| 381 |
|
| * plugin were not there, and nothing is stored. An application that |
| 382 |
|
| * wants idempotency for anonymous callers - a signup, a payment carrying |
| 383 |
|
| * its own token - returns something itself; it is a coderef, and that |
| 384 |
|
| * puts the decision with the person able to make it safely. */ |
| 385 |
48 |
▶ | if (!SvOK(scope)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(scope)) { |
2 |
46 |
48 |
|
| 386 |
2 |
| SvREFCNT_dec(scope); |
| 387 |
2 |
| pi_warn(aTHX_ c, |
| 388 |
|
| "idempotency: scope returned undef, so this request is not " |
| 389 |
|
| "idempotent - see Punk::Plugin::Idempotency"); |
| 390 |
2 |
| return 0; |
| 391 |
|
| } |
| 392 |
|
| |
| 393 |
46 |
| f = pi_store(aTHX_ c, errp); |
| 394 |
46 |
▶ | if (!f) { SvREFCNT_dec(scope); return *errp ? 1 : 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) { SvREFCNT_dec(scope); return *errp ? 1 : 0; } |
0 |
46 |
46 |
|
| 395 |
|
| |
| 396 |
46 |
| comp = pi_composite(aTHX_ scope, rech, method, mlen, key, klen); |
| 397 |
46 |
| SvREFCNT_dec(scope); |
| 398 |
46 |
| sv_2mortal(comp); |
| 399 |
46 |
| fp = sv_2mortal(pi_fingerprint(aTHX_ c, rech, env, method, mlen)); |
| 400 |
|
| |
| 401 |
|
| { /* Look, and on a miss take the lock so two retries arriving together |
| 402 |
|
| * do not both execute. Hyperman serves a worker's requests one at a |
| 403 |
|
| * time, so the concurrency is ACROSS workers - which is exactly the |
| 404 |
|
| * arrangement a dropped connection produces. |
| 405 |
|
| * |
| 406 |
|
| * pkc_be_get, NOT pkc_read: the read goes STRAIGHT TO THE BACKEND |
| 407 |
|
| * and never through the memory tier. A tier is a copy per worker, |
| 408 |
|
| * eventually consistent and invalidated best-effort, and that trade |
| 409 |
|
| * is right for a cache and wrong here - a tier that has not yet seen |
| 410 |
|
| * a write answers "no entry", and "no entry" means execute the work |
| 411 |
|
| * a second time. The failure is in the dangerous direction, so the |
| 412 |
|
| * plugin does not use the tier for its own keys even when the |
| 413 |
|
| * application configured one. */ |
| 414 |
46 |
| SV *raw = pkc_be_get(aTHX_ f, comp, NULL); |
| 415 |
46 |
▶ | if (raw) entry = pkc_ready(aTHX_ raw, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (raw) entry = pkc_ready(aTHX_ raw, 1); |
15 |
31 |
46 |
|
| 416 |
46 |
▶ | if (!entry && (f->caps & PKC_CAN_LOCK)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!entry && (f->caps & PKC_CAN_LOCK)) { |
31 |
15 |
46 |
| if (!entry && (f->caps & PKC_CAN_LOCK)) { |
30 |
1 |
31 |
|
| 417 |
|
| /* THE BUDGET IS THE PLUGIN'S, not the cache's. A cache's |
| 418 |
|
| * lock_wait is tuned for a compute - five seconds by default - |
| 419 |
|
| * and it is also how old a lock may get before it is stolen. A |
| 420 |
|
| * handler that takes longer than that, a payment being the usual |
| 421 |
|
| * one, had its lock taken by the very retry the lock was for, and |
| 422 |
|
| * the two ran together. */ |
| 423 |
30 |
| SV **lw = hv_fetchs(ocfg, "lock_wait", 0); |
| 424 |
30 |
▶ | double budget = (lw && *lw && SvOK(*lw)) ? (double)SvNV(*lw) |
Branch: 2 branches
| Branch | True | False | Total |
| double budget = (lw && *lw && SvOK(*lw)) ? (double)SvNV(*lw) |
30 |
0 |
30 |
| double budget = (lw && *lw && SvOK(*lw)) ? (double)SvNV(*lw) |
30 |
0 |
30 |
|
| 425 |
30 |
▶ | : PI_LOCK_WAIT; |
Branch: 1 branch
| Branch | True | False | Total |
| : PI_LOCK_WAIT; |
30 |
0 |
30 |
|
| 426 |
30 |
| int locked = pkc_be_lock_for(aTHX_ f, comp, budget); |
| 427 |
30 |
▶ | if (!locked) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!locked) { |
0 |
30 |
30 |
|
| 428 |
0 |
| double deadline = pc_now(aTHX) + budget; |
| 429 |
0 |
| double nap = 0.002; |
| 430 |
0 |
▶ | while (pc_now(aTHX) < deadline) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (pc_now(aTHX) < deadline) { |
0 |
0 |
0 |
|
| 431 |
0 |
| pkc_sleep(nap); |
| 432 |
0 |
▶ | if (nap < 0.05) nap *= 1.5; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nap < 0.05) nap *= 1.5; |
0 |
0 |
0 |
|
| 433 |
0 |
| raw = pkc_be_get(aTHX_ f, comp, NULL); |
| 434 |
0 |
▶ | if (raw) { entry = pkc_ready(aTHX_ raw, 1); break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (raw) { entry = pkc_ready(aTHX_ raw, 1); break; } |
0 |
0 |
0 |
|
| 435 |
0 |
▶ | if (pkc_be_lock_for(aTHX_ f, comp, budget)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pkc_be_lock_for(aTHX_ f, comp, budget)) { |
0 |
0 |
0 |
|
| 436 |
0 |
| locked = 1; /* the holder gave up */ |
| 437 |
0 |
| break; |
| 438 |
|
| } |
| 439 |
|
| } |
| 440 |
|
| /* waited it out with nothing to replay: execute rather than |
| 441 |
|
| * hang. Duplicated work is a cost; a stalled request is an |
| 442 |
|
| * outage. The same rule Punk::Cache's compute follows. */ |
| 443 |
|
| } |
| 444 |
|
| /* Winning the lock is not finding nothing. The holder records |
| 445 |
|
| * and THEN unlocks, so a request that looked before the record |
| 446 |
|
| * and locked after the unlock has the lock and an answer waiting |
| 447 |
|
| * - and would run the work a second time. Look again. */ |
| 448 |
30 |
▶ | if (locked && !entry) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (locked && !entry) { |
30 |
0 |
30 |
| if (locked && !entry) { |
30 |
0 |
30 |
|
| 449 |
30 |
| raw = pkc_be_get(aTHX_ f, comp, NULL); |
| 450 |
30 |
▶ | if (raw) entry = pkc_ready(aTHX_ raw, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (raw) entry = pkc_ready(aTHX_ raw, 1); |
1 |
29 |
30 |
|
| 451 |
|
| } |
| 452 |
|
| } |
| 453 |
|
| } |
| 454 |
|
| |
| 455 |
46 |
▶ | if (entry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry) { |
16 |
30 |
46 |
|
| 456 |
16 |
| int conflict = 0; |
| 457 |
16 |
| SV *rep = pi_replay(aTHX_ entry, fp, &conflict); |
| 458 |
16 |
| SvREFCNT_dec(entry); |
| 459 |
16 |
▶ | if (f->caps & PKC_CAN_UNLOCK) pkc_be_unlock(aTHX_ f, comp); |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->caps & PKC_CAN_UNLOCK) pkc_be_unlock(aTHX_ f, comp); |
15 |
1 |
16 |
|
| 460 |
16 |
▶ | if (conflict) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (conflict) { |
3 |
13 |
16 |
|
| 461 |
3 |
| *out = pi_error(aTHX_ 422, |
| 462 |
|
| "Idempotency-Key was already used for a different request"); |
| 463 |
16 |
| return 1; |
| 464 |
|
| } |
| 465 |
13 |
▶ | if (rep) { *out = rep; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rep) { *out = rep; return 1; } |
13 |
0 |
13 |
|
| 466 |
|
| /* an entry we cannot read is a miss, and a miss re-executes */ |
| 467 |
|
| } |
| 468 |
|
| |
| 469 |
|
| { /* what the recorder needs, left where it can find it */ |
| 470 |
30 |
| AV *cav = pcx_av(aTHX_ c); |
| 471 |
30 |
| HV *stash = ps_stash(aTHX_ cav); |
| 472 |
30 |
| AV *pending = newAV(); |
| 473 |
30 |
| av_push(pending, newSVsv(comp)); |
| 474 |
30 |
| av_push(pending, newSVsv(fp)); |
| 475 |
30 |
| (void)hv_stores(stash, PI_STASH_KEY, newRV_noinc((SV *)pending)); |
| 476 |
|
| } |
| 477 |
30 |
| return 0; |
| 478 |
|
| } |
| 479 |
|
| |
| 480 |
|
| /* ---- the recorder, last on the after chain ---------------------------------- */ |
| 481 |
|
| |
| 482 |
|
| XS_INTERNAL(pi_record_cb); |
| 483 |
60 |
| XS_INTERNAL(pi_record_cb) { |
| 484 |
60 |
| dXSARGS; |
| 485 |
|
| SV *c, *resp; |
| 486 |
|
| AV *cav, *ra; |
| 487 |
|
| HV *stash; |
| 488 |
|
| SV **pend, **st, **bp; |
| 489 |
|
| AV *pending; |
| 490 |
|
| IV status; |
| 491 |
|
| punk_cachefront *f; |
| 492 |
60 |
| SV *err = NULL; |
| 493 |
60 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 494 |
60 |
| NV ttl = 86400; |
| 495 |
60 |
| IV maxrec = 1048576; |
| 496 |
60 |
| const char *why = "idempotency: response not recorded, so a retry of " |
| 497 |
|
| "this key will execute again"; |
| 498 |
|
| |
| 499 |
60 |
▶ | if (items < 2) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (items < 2) XSRETURN_EMPTY; |
0 |
60 |
60 |
|
| 500 |
60 |
| c = ST(0); resp = ST(1); |
| 501 |
60 |
| cav = pcx_av(aTHX_ c); |
| 502 |
60 |
▶ | if (!cav) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cav) XSRETURN_EMPTY; |
0 |
60 |
60 |
|
| 503 |
60 |
| stash = ps_stash(aTHX_ cav); |
| 504 |
60 |
| pend = hv_fetchs(stash, PI_STASH_KEY, 0); |
| 505 |
60 |
▶ | if (!(pend && *pend && SvROK(*pend) && SvTYPE(SvRV(*pend)) == SVt_PVAV)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(pend && *pend && SvROK(*pend) && SvTYPE(SvRV(*pend)) == SVt_PVAV)) |
30 |
30 |
60 |
| if (!(pend && *pend && SvROK(*pend) && SvTYPE(SvRV(*pend)) == SVt_PVAV)) |
30 |
0 |
30 |
| if (!(pend && *pend && SvROK(*pend) && SvTYPE(SvRV(*pend)) == SVt_PVAV)) |
30 |
0 |
30 |
| if (!(pend && *pend && SvROK(*pend) && SvTYPE(SvRV(*pend)) == SVt_PVAV)) |
0 |
30 |
30 |
|
| 506 |
30 |
| XSRETURN_EMPTY; /* not an idempotent request */ |
| 507 |
30 |
| pending = (AV *)SvRV(*pend); |
| 508 |
30 |
▶ | if (av_len(pending) < 1) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(pending) < 1) XSRETURN_EMPTY; |
0 |
30 |
30 |
|
| 509 |
|
| /* Once: a second after-hook pass must not write twice. The reference is |
| 510 |
|
| * taken BEFORE the delete - hv_delete with G_DISCARD frees the value, |
| 511 |
|
| * and everything below still reads through `pending`, so deleting first |
| 512 |
|
| * is a use-after-free that survives every small test and crashes under |
| 513 |
|
| * a real one. */ |
| 514 |
30 |
| sv_2mortal(SvREFCNT_inc((SV *)pending)); |
| 515 |
30 |
| (void)hv_delete(stash, PI_STASH_KEY, sizeof(PI_STASH_KEY) - 1, G_DISCARD); |
| 516 |
|
| |
| 517 |
30 |
▶ | if (cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cap) { |
30 |
0 |
30 |
|
| 518 |
30 |
| SV **t = av_fetch(cap, 0, 0); |
| 519 |
30 |
| SV **m = av_fetch(cap, 1, 0); |
| 520 |
30 |
▶ | if (t && *t && SvOK(*t)) ttl = SvNV(*t); |
Branch: 3 branches
| Branch | True | False | Total |
| if (t && *t && SvOK(*t)) ttl = SvNV(*t); |
30 |
0 |
30 |
| if (t && *t && SvOK(*t)) ttl = SvNV(*t); |
30 |
0 |
30 |
| if (t && *t && SvOK(*t)) ttl = SvNV(*t); |
30 |
0 |
30 |
|
| 521 |
30 |
▶ | if (m && *m && SvOK(*m)) maxrec = SvIV(*m); |
Branch: 3 branches
| Branch | True | False | Total |
| if (m && *m && SvOK(*m)) maxrec = SvIV(*m); |
30 |
0 |
30 |
| if (m && *m && SvOK(*m)) maxrec = SvIV(*m); |
30 |
0 |
30 |
| if (m && *m && SvOK(*m)) maxrec = SvIV(*m); |
30 |
0 |
30 |
|
| 522 |
|
| } |
| 523 |
|
| |
| 524 |
30 |
| f = pi_store(aTHX_ c, &err); |
| 525 |
30 |
▶ | if (err) SvREFCNT_dec(err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) SvREFCNT_dec(err); |
0 |
30 |
30 |
|
| 526 |
30 |
▶ | if (!f) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f) XSRETURN_EMPTY; |
0 |
30 |
30 |
|
| 527 |
|
| |
| 528 |
30 |
▶ | if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) goto unrecorded; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) goto unrecorded; |
30 |
0 |
30 |
| if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) goto unrecorded; |
0 |
30 |
30 |
|
| 529 |
30 |
| ra = (AV *)SvRV(resp); |
| 530 |
30 |
| st = av_fetch(ra, 0, 0); |
| 531 |
30 |
| bp = av_fetch(ra, 2, 0); |
| 532 |
30 |
▶ | status = (st && *st) ? SvIV(*st) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| status = (st && *st) ? SvIV(*st) : 0; |
30 |
0 |
30 |
| status = (st && *st) ? SvIV(*st) : 0; |
30 |
0 |
30 |
|
| 533 |
|
| |
| 534 |
|
| /* 2xx, 3xx and 4xx are recorded; 5xx is not. |
| 535 |
|
| * |
| 536 |
|
| * A 5xx is transient by construction, and replaying one turns a single |
| 537 |
|
| * bad minute into a permanent bad day for the life of the TTL: the |
| 538 |
|
| * client retries, gets the stored 500 instantly, retries again, and the |
| 539 |
|
| * endpoint is broken for that key until it expires. Exactly backwards. |
| 540 |
|
| * |
| 541 |
|
| * A 4xx IS recorded, which is less obvious: a 422 for a malformed order |
| 542 |
|
| * is a real answer about that request, and replaying it is correct. */ |
| 543 |
30 |
▶ | if (status < 200 || status >= 500) goto unrecorded; |
Branch: 2 branches
| Branch | True | False | Total |
| if (status < 200 || status >= 500) goto unrecorded; |
30 |
0 |
30 |
| if (status < 200 || status >= 500) goto unrecorded; |
2 |
28 |
30 |
|
| 544 |
|
| |
| 545 |
|
| /* Only an arrayref body can be stored. A filehandle or a reader would |
| 546 |
|
| * have to be consumed to be recorded, and consuming it is the response. */ |
| 547 |
28 |
▶ | if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV)) |
28 |
0 |
28 |
| if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV)) |
28 |
0 |
28 |
| if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV)) |
28 |
0 |
28 |
| if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV)) |
2 |
26 |
28 |
|
| 548 |
2 |
| goto unrecorded; |
| 549 |
|
| |
| 550 |
|
| { /* The ceiling. The plugin stores whole responses, so a 50MB export |
| 551 |
|
| * behind a key is 50MB in the cache per key, for the TTL. Over the |
| 552 |
|
| * ceiling the response is served but not recorded, and the response |
| 553 |
|
| * says so, because "my retry re-executed" must be debuggable from |
| 554 |
|
| * the client side. The single-flight lock was held to here all the |
| 555 |
|
| * same - the concurrent-retry protection covered the request's |
| 556 |
|
| * duration, and only the replayability is given up. */ |
| 557 |
26 |
| AV *b = (AV *)SvRV(*bp); |
| 558 |
26 |
| SSize_t i, n = av_len(b) + 1; |
| 559 |
26 |
| UV blen = 0; |
| 560 |
52 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
26 |
26 |
52 |
|
| 561 |
26 |
| SV **e = av_fetch(b, i, 0); |
| 562 |
|
| STRLEN l; |
| 563 |
52 |
▶ | if (e && *e && SvOK(*e)) { (void)SvPV_const(*e, l); blen += l; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) { (void)SvPV_const(*e, l); blen += l; } |
26 |
0 |
26 |
| if (e && *e && SvOK(*e)) { (void)SvPV_const(*e, l); blen += l; } |
26 |
0 |
26 |
| if (e && *e && SvOK(*e)) { (void)SvPV_const(*e, l); blen += l; } |
26 |
0 |
26 |
|
| 564 |
|
| } |
| 565 |
26 |
▶ | if (blen > (UV)maxrec) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (blen > (UV)maxrec) { |
2 |
24 |
26 |
|
| 566 |
2 |
| SV **hdrp = av_fetch(ra, 1, 0); |
| 567 |
2 |
▶ | if (hdrp && *hdrp && SvROK(*hdrp) |
Branch: 3 branches
| Branch | True | False | Total |
| if (hdrp && *hdrp && SvROK(*hdrp) |
2 |
0 |
2 |
| if (hdrp && *hdrp && SvROK(*hdrp) |
2 |
0 |
2 |
| if (hdrp && *hdrp && SvROK(*hdrp) |
2 |
0 |
2 |
|
| 568 |
2 |
▶ | && SvTYPE(SvRV(*hdrp)) == SVt_PVAV) { |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(SvRV(*hdrp)) == SVt_PVAV) { |
2 |
0 |
2 |
|
| 569 |
2 |
| av_push((AV *)SvRV(*hdrp), newSVpvs("Idempotency-Recorded")); |
| 570 |
2 |
| av_push((AV *)SvRV(*hdrp), newSVpvs("false")); |
| 571 |
|
| } |
| 572 |
2 |
| why = "idempotency: response is larger than max_record and was " |
| 573 |
|
| "not recorded, so a retry of this key will execute again"; |
| 574 |
2 |
| goto unrecorded; |
| 575 |
|
| } |
| 576 |
|
| } |
| 577 |
|
| |
| 578 |
|
| { |
| 579 |
24 |
| SV *fp = *av_fetch(pending, 1, 0); |
| 580 |
24 |
| SV *key = *av_fetch(pending, 0, 0); |
| 581 |
24 |
| SV *val = pi_encode(aTHX_ ra, fp); |
| 582 |
24 |
| SV *bytes = sv_2mortal(punk_frj(aTHX)->encode(aTHX_ val, NULL)); |
| 583 |
24 |
| SV *r = pkc_be_set(aTHX_ f, key, bytes, ttl); |
| 584 |
24 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
24 |
0 |
24 |
|
| 585 |
24 |
▶ | if (f->caps & PKC_CAN_UNLOCK) pkc_be_unlock(aTHX_ f, key); |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->caps & PKC_CAN_UNLOCK) pkc_be_unlock(aTHX_ f, key); |
23 |
1 |
24 |
|
| 586 |
|
| } |
| 587 |
24 |
| XSRETURN_EMPTY; |
| 588 |
|
| |
| 589 |
6 |
| unrecorded: |
| 590 |
|
| /* The window's only symptom an operator will ever see. A response that |
| 591 |
|
| * was NOT recorded means the next retry re-executes, and without this |
| 592 |
|
| * line a doubled order is indistinguishable from a client bug. */ |
| 593 |
|
| { |
| 594 |
6 |
| SV *key = *av_fetch(pending, 0, 0); |
| 595 |
6 |
▶ | if (f->caps & PKC_CAN_UNLOCK) pkc_be_unlock(aTHX_ f, key); |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->caps & PKC_CAN_UNLOCK) pkc_be_unlock(aTHX_ f, key); |
6 |
0 |
6 |
|
| 596 |
6 |
| pi_warn(aTHX_ c, why); |
| 597 |
|
| } |
| 598 |
6 |
| XSRETURN_EMPTY; |
| 599 |
|
| } |
| 600 |
|
| |
| 601 |
|
| #endif /* PUNK_IDEM_H */ |