include/punk/punk_bus.h

filter: Statementstmt 87.1% 134 / 163 (8 uncoverable) Branchbran 51.1% 83 / 178 (35 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.3% 217 / 341 (43 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/headers.xs ↑ Summary include/punk/punk_wsroom.h »
1 /* punk_bus.h - Punk's use of Hyperman's cross-worker message bus.
2 *
3 * WHAT THIS IS FOR.
4 *
5 * A prefork server makes anything held in a worker a lie about the pool.
6 * Punk::WebSocket::Room said so in its own documentation: a room is per
7 * worker, so under `workers => 4` a broadcast reached roughly a quarter of the
8 * people in it, the call succeeded, the return value was a plausible number,
9 * and nobody was told.
10 *
11 * Hyperman 0.28 grew the substrate that fixes it - one ring in shared memory,
12 * mapped before the fork, reached through hm_abi v5. This is the half that
13 * knows what a room is.
14 *
15 * OPTIONAL, AND LAZILY RESOLVED. A Punk running under a server that is not
16 * Hyperman, or under a Hyperman too old to have the bus, keeps working exactly
17 * as it did: a broadcast reaches this worker's own members and nothing else,
18 * which is the behaviour that existed before any of this. What it does NOT do
19 * is pretend - punk_bus_live() is how a caller finds out.
20 *
21 * THE ONE DELIVERY PATH.
22 *
23 * A room broadcast publishes, and every worker's subscriber - INCLUDING the
24 * publishing worker's own - fans the frame out to its members. The publisher
25 * does not also send locally.
26 *
27 * That is worth being deliberate about, because sending locally and then
28 * publishing is the obvious implementation and it is wrong twice over: the
29 * origin worker's members would receive the frame twice, and the two paths
30 * would drift, so a bug in the shared one would be invisible to whoever tested
31 * on a single worker. One path, exercised by everybody.
32 */
33  
34 #ifndef PUNK_BUS_H
35 #define PUNK_BUS_H
36  
37 /* The bus arrived in hm_abi v5. Punk compiles against whatever Hyperman
38 * header is installed, so this is checked at runtime rather than assumed:
39 * a Punk built against v5 may still be loaded beside an older Hyperman. */
40 #define PUNK_BUS_ABI_MIN 5
41  
42 static int PUNK_BUS_READY = 0; /* subscriptions registered in this process */
43 static int PUNK_BUS_ROOMSUB = -1; /* the room fanout subscription id */
44  
45 /* How many members the last local fan reached. broadcast() reads it, because
46 * across a pool "how many got it" is otherwise unanswerable at call time and
47 * the alternative is to invent a plausible number - which is the exact fault
48 * this whole thing exists to remove. */
49 static IV PUNK_BUS_LAST_FAN = 0;
50  
51 /* Is there a pool to talk to at all?
52 *
53 * False under a non-Hyperman server, an older Hyperman, Windows, or a
54 * compiler without the atomics the arena needs. Every one of those is a
55 * supported configuration in which a room is simply local, which is what it
56 * always was. */
57 /*
58 # uncoverable statement
59 # uncoverable subroutine */
60 0 static int punk_bus_live(pTHX) {
61 0 const hm_abi *A = punk_hm(aTHX); /* # uncoverable statement */
62 /*
63 # uncoverable statement
64 # uncoverable branch true
65 # uncoverable branch false
66 # uncoverable condition left count:1
67 # uncoverable condition right count:1
68 # uncoverable condition left count:2
69 # uncoverable condition right count:2
70 # uncoverable condition left count:3
71 # uncoverable condition right count:3 */
72 0 ▶ if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 0 0 0
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 0 0 0
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 0 0 0
73 0 return 1; /* # uncoverable statement */
74 }
75  
76 /* ONE topic for every room, with the room name inside the payload.
77 *
78 * The obvious design is a topic per room - "punk:ws:lobby" - but the bus
79 * matches topics EXACTLY and has no wildcards, deliberately, so that would
80 * mean a subscription per room and a registration to add and tear down every
81 * time a room came or went. One topic and one subscription instead, with the
82 * name length-prefixed ahead of the frame.
83 *
84 * A length prefix rather than a separator, because a separator has to be a
85 * byte a room name cannot contain, and "cannot" is a promise about somebody
86 * else's strings that this has no business making.
87 *
88 * [u16 name length][name][u32 pid][u64 connection][the encoded frame]
89 *
90 * The pid and connection name the one member a broadcast leaves out, or are
91 * zero. A connection lives in one worker, so its address means something
92 * only there - and forked workers lay their memory out alike, so the same
93 * address in another worker is somebody else. The pid says which worker the
94 * address belongs to; every other worker delivers to all of its members.
95 */
96 #define PUNK_BUS_ROOM_SKIP_LEN 12
97 #define PUNK_BUS_ROOM_TOPIC "punk:ws"
98 #define PUNK_BUS_ROOM_TOPIC_LEN 7
99  
100 /* Pack a room broadcast. Mortal SV. */
101 3 static SV *punk_bus_room_pack(pTHX_ SV *name, SV *frame, void *skip) {
102 STRLEN nl, fl;
103 3 ▶ const char *np = SvPV_const(name, nl);
Branch: 1 branch
BranchTrueFalseTotal
const char *np = SvPV_const(name, nl); 3 0 3
104 3 const char *fp = SvPV_const(frame, fl);
105 SV *out;
106 unsigned char hdr[2], who[PUNK_BUS_ROOM_SKIP_LEN];
107 3 ▶ U32 pid = skip ? (U32)PerlProc_getpid() : 0;
Branch: 1 branch
BranchTrueFalseTotal
U32 pid = skip ? (U32)PerlProc_getpid() : 0; 1 2 3
108 3 UV addr = PTR2UV(skip);
109 int i;
110 /*
111 # uncoverable branch true
112 # uncoverable condition left count:1 class:ignore_covered_err */
113 3 ▶ if (nl > 0xFFFF) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (nl > 0xFFFF) return NULL; 0 3 3
114 3 hdr[0] = (unsigned char)((nl >> 8) & 0xFF);
115 3 hdr[1] = (unsigned char)(nl & 0xFF);
116 15 ▶ for (i = 0; i < 4; i++) who[i] = (unsigned char)(pid >> (24 - 8 * i));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) who[i] = (unsigned char)(pid >> (24 - 8 * i)); 12 3 15
117 27 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 24 3 27
118 24 who[4 + i] = (unsigned char)(((uint64_t)addr >> (56 - 8 * i)) & 0xFF);
119 3 out = sv_2mortal(newSVpvn((const char *)hdr, 2));
120 3 sv_catpvn(out, np, nl);
121 3 sv_catpvn(out, (const char *)who, sizeof who);
122 3 sv_catpvn(out, fp, fl);
123 3 return out;
124 }
125  
126 /* A frame arriving from any worker, including this one.
127 *
128 * The payload is the ALREADY ENCODED WebSocket frame, so it is encoded once
129 * per broadcast rather than once per worker, and every member of the room
130 * across the whole pool is sent byte-identical bytes. That is the property
131 * pwr_fan was built for; publishing the raw text instead would have thrown it
132 * away and re-encoded N times.
133 *
134 * MUST NOT CROAK: reached from the event loop with no Perl frame to unwind
135 * into. Everything here is either C or a call that cannot die.
136 */
137 3 static void punk_bus_room_cb_in(pTHX_ const char *topic, STRLEN tlen,
138 const char *payload, STRLEN plen, void *ud) {
139 SV *name, *frame, *room;
140 STRLEN nl;
141 3 void *skip = NULL;
142 PERL_UNUSED_ARG(topic);
143 PERL_UNUSED_ARG(tlen);
144 PERL_UNUSED_ARG(ud);
145  
146 /*
147 # uncoverable branch true
148 # uncoverable condition left count:1 class:ignore_covered_err */
149 3 ▶ if (plen < 2) return;
Branch: 1 branch
BranchTrueFalseTotal
if (plen < 2) return; 0 3 3
150 3 nl = ((STRLEN)(unsigned char)payload[0] << 8)
151 3 | (STRLEN)(unsigned char)payload[1];
152 /* truncated: refuse to guess */
153 /*
154 # uncoverable branch true
155 # uncoverable condition left count:1 class:ignore_covered_err */
156 3 ▶ if (plen < 2 + nl + PUNK_BUS_ROOM_SKIP_LEN) return;
Branch: 1 branch
BranchTrueFalseTotal
if (plen < 2 + nl + PUNK_BUS_ROOM_SKIP_LEN) return; 0 3 3
157  
158 { /* the member left out, when it is one of ours */
159 3 const unsigned char *w = (const unsigned char *)payload + 2 + nl;
160 3 U32 pid = 0;
161 3 uint64_t addr = 0;
162 int i;
163 15 ▶ for (i = 0; i < 4; i++) pid = (pid << 8) | w[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) pid = (pid << 8) | w[i]; 12 3 15
164 27 ▶ for (i = 0; i < 8; i++) addr = (addr << 8) | w[4 + i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) addr = (addr << 8) | w[4 + i]; 24 3 27
165 3 ▶ if (pid && pid == (U32)PerlProc_getpid()) skip = INT2PTR(void *, (UV)addr); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (pid && pid == (U32)PerlProc_getpid()) skip = INT2PTR(void *, (UV)addr); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 2 3
if (pid && pid == (U32)PerlProc_getpid()) skip = INT2PTR(void *, (UV)addr); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
166 }
167  
168 3 name = sv_2mortal(newSVpvn(payload + 2, nl));
169 3 frame = sv_2mortal(newSVpvn(payload + 2 + nl + PUNK_BUS_ROOM_SKIP_LEN,
170 plen - 2 - nl - PUNK_BUS_ROOM_SKIP_LEN));
171  
172 /* A lookup, never a creation. A worker that has not named this room
173 * has no members in it, and creating it here let one client's stream
174 * of fresh names grow every worker in the pool for the pool's life. */
175 {
176 SV **rp;
177 /*
178 # uncoverable branch true
179 # uncoverable condition left count:1 class:ignore_covered_err
180 # uncoverable condition left count:2 class:ignore_covered_err */
181 3 ▶ if (!pwr_rooms || pwr_rooms_pid != (IV)PerlProc_getpid()) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!pwr_rooms || pwr_rooms_pid != (IV)PerlProc_getpid()) return; 3 0 3
if (!pwr_rooms || pwr_rooms_pid != (IV)PerlProc_getpid()) return; 0 3 3
182 3 rp = hv_fetch(pwr_rooms, SvPVX(name), (I32)nl, 0);
183 /*
184 # uncoverable branch true
185 # uncoverable condition left count:1 class:ignore_covered_err
186 # uncoverable condition left count:2 class:ignore_covered_err
187 # uncoverable condition left count:3 class:ignore_covered_err */
188 3 ▶ if (!(rp && *rp && SvROK(*rp))) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(rp && *rp && SvROK(*rp))) return; 3 0 3
if (!(rp && *rp && SvROK(*rp))) return; 3 0 3
if (!(rp && *rp && SvROK(*rp))) return; 0 3 3
189 3 room = *rp;
190 }
191  
192 3 PUNK_BUS_LAST_FAN += pwr_fan_skip(aTHX_ room, frame, skip);
193 }
194  
195 3 static void punk_bus_room_cb(pTHX_ const char *topic, STRLEN tlen,
196 const char *payload, STRLEN plen, void *ud) {
197 3 PK_LOOP_ENTER;
198 3 punk_bus_room_cb_in(aTHX_ topic, tlen, payload, plen, ud);
199 3 ▶ PK_LOOP_LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PK_LOOP_LEAVE; 3 0 3
200 3 }
201  
202 /* Register this worker's subscriptions. Called from the worker hook, so it
203 * runs once per worker, after the fork, on the process that will serve. */
204 927 static void punk_bus_attach(pTHX) {
205 const hm_abi *A;
206 927 ▶ if (PUNK_BUS_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (PUNK_BUS_READY) return; 681 246 927
207 246 A = punk_hm(aTHX);
208 /*
209 # uncoverable branch true
210 # uncoverable condition left count:1 class:ignore_covered_err
211 # uncoverable condition left count:2 class:ignore_covered_err
212 # uncoverable condition left count:3 class:ignore_covered_err */
213 246 ▶ if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return; 246 0 246
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return; 246 0 246
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return; 0 246 246
214 246 PUNK_BUS_READY = 1;
215 /* One subscription for every room. The topic carries the room name, so a
216 * per-room registration would buy nothing and would need tearing down as
217 * rooms come and go. */
218 246 PUNK_BUS_ROOMSUB = A->bus_subscribe(aTHX_ PUNK_BUS_ROOM_TOPIC,
219 PUNK_BUS_ROOM_TOPIC_LEN,
220 NULL, 0, punk_bus_room_cb, NULL);
221 }
222  
223 /* Publish a room broadcast to the pool.
224 *
225 * Returns 1 when it reached the ring - in which case the caller must NOT also
226 * send locally, because this worker's own subscriber will deliver it like
227 * everybody else's. Returns 0 when there is no pool, and then the caller does
228 * the local send it always did.
229 *
230 * That is the whole "one delivery path" rule, expressed as a return value. */
231 4 static int punk_bus_room_publish(pTHX_ SV *name, SV *frame, SV *except) {
232 4 const hm_abi *A = punk_hm(aTHX);
233 SV *packed;
234 STRLEN pl;
235 const char *pp;
236  
237 /* No subscription in THIS process means nothing here would deliver the
238 * frame to this worker's own members - so publishing would serve the rest
239 * of the pool and silently skip the people in front of us. Fall back to
240 * the local fan instead, which is what a Punk outside a worker does
241 * anyway. */
242 4 ▶ if (!PUNK_BUS_READY) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_BUS_READY) return 0; 1 3 4
243 /*
244 # uncoverable branch true
245 # uncoverable condition left count:1 class:ignore_covered_err
246 # uncoverable condition left count:2 class:ignore_covered_err
247 # uncoverable condition left count:3 class:ignore_covered_err */
248 3 ▶ if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 3 0 3
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 3 0 3
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 0 3 3
249 6 ▶ packed = punk_bus_room_pack(aTHX_ name, frame,
Branch: 1 branch
BranchTrueFalseTotal
packed = punk_bus_room_pack(aTHX_ name, frame, 3 0 3
250 3 ▶ (except && SvOK(except)) ? PWR_ADDR(except) : NULL);
Branch: 2 branches
BranchTrueFalseTotal
(except && SvOK(except)) ? PWR_ADDR(except) : NULL); 1 2 3
(except && SvOK(except)) ? PWR_ADDR(except) : NULL); 1 0 1
251 /*
252 # uncoverable branch true
253 # uncoverable condition left count:1 class:ignore_covered_err */
254 3 ▶ if (!packed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!packed) return 0; 0 3 3
255 3 pp = SvPV_const(packed, pl);
256 /* A refusal (too big for a slot) is NOT success: the caller falls back to
257 * the local send, so the members on this worker still get their frame
258 * rather than nobody getting anything. */
259 /*
260 # uncoverable branch true
261 # uncoverable condition left count:1 class:ignore_covered_err */
262 3 ▶ if (A->bus_publish(PUNK_BUS_ROOM_TOPIC, PUNK_BUS_ROOM_TOPIC_LEN, pp, pl)
Branch: 1 branch
BranchTrueFalseTotal
if (A->bus_publish(PUNK_BUS_ROOM_TOPIC, PUNK_BUS_ROOM_TOPIC_LEN, pp, pl) 0 3 3
263 != 1)
264 0 return 0; /* # uncoverable statement */
265  
266 /* Run this worker's own subscription NOW, so the local half of the
267 * broadcast has happened by the time broadcast() returns and there is a
268 * real number to give back. The other workers get it from their wakeup.
269 *
270 * Still ONE delivery path: this is the same subscriber they run, invoked
271 * synchronously rather than from the loop. */
272 3 PUNK_BUS_LAST_FAN = 0;
273 /*
274 # uncoverable branch false
275 # uncoverable condition right count:1 class:ignore_covered_err
276 # uncoverable condition right count:2 class:ignore_covered_err */
277 3 ▶ if (A->abi_version >= PUNK_BUS_ABI_MIN && A->bus_dispatch)
Branch: 2 branches
BranchTrueFalseTotal
if (A->abi_version >= PUNK_BUS_ABI_MIN && A->bus_dispatch) 3 0 3
if (A->abi_version >= PUNK_BUS_ABI_MIN && A->bus_dispatch) 3 0 3
278 3 (void)A->bus_dispatch(aTHX);
279 3 return 1;
280 }
281  
282 /* The hm_abi worker callback: this is what runs inside each worker. */
283 32 static void punk_bus_worker_cb(pTHX_ void *loop, void *ud) {
284 PERL_UNUSED_ARG(loop);
285 PERL_UNUSED_ARG(ud);
286 32 punk_bus_attach(aTHX);
287 32 }
288  
289 /* Ask Hyperman to run punk_bus_attach in every worker.
290 *
291 * Called from compile(), which runs in the parent before any fork - the only
292 * moment a registration reaches every worker. Registering from inside a
293 * request would reach exactly one of them, which is the same class of mistake
294 * the bus exists to fix. */
295 896 static void punk_bus_register(pTHX) {
296 896 const hm_abi *A = punk_hm(aTHX);
297 /*
298 # uncoverable branch true class:ignore_covered_err
299 # uncoverable condition left count:1 class:ignore_covered_err
300 # uncoverable condition left count:2 class:ignore_covered_err
301 # uncoverable condition left count:3 class:ignore_covered_err */
302 896 ▶ if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->on_worker_start) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->on_worker_start) return; 895 1 896
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->on_worker_start) return; 895 0 895
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->on_worker_start) return; 0 895 895
303 895 (void)A->on_worker_start(aTHX_ punk_bus_worker_cb, NULL);
304 /* A single-process server never forks and so never fires the hook. It
305 * still has a bus (the arena is mapped either way), so attach now as
306 * well; punk_bus_attach is idempotent. */
307 895 punk_bus_attach(aTHX);
308 }
309  
310 /* ---- the application's own use of the bus -------------------------------- *
311 *
312 * Rooms are the reason this exists, but they are not the only thing that wants
313 * it: a cache key changing, a config being re-read, a presence update. So the
314 * same ring is reachable directly.
315 *
316 * Topics are namespaced away from Punk's own - an application publishing to
317 * "punk:ws" would otherwise be handing raw bytes to the room fanout, which
318 * expects a length-prefixed name in front of a WebSocket frame. */
319 #define PUNK_BUS_APP_PREFIX "app:"
320 #define PUNK_BUS_APP_PREFIX_LEN 4
321  
322 5 static SV *punk_bus_app_topic(pTHX_ SV *topic) {
323 STRLEN tl;
324 5 const char *tp = SvPV_const(topic, tl);
325 5 SV *out = sv_2mortal(newSVpvn(PUNK_BUS_APP_PREFIX, PUNK_BUS_APP_PREFIX_LEN));
326 5 sv_catpvn(out, tp, tl);
327 5 return out;
328 }
329  
330 /* An application subscription. The coderef is held here, in this process,
331 * because a callback cannot cross a fork - the ring is the shared thing, not
332 * the list of people reading it.
333 *
334 * Punk's own cap, not Hyperman's: HM_BUS_SUBS lives in hm_bus.h, which is the
335 * implementation rather than the ABI, and Punk consumes the ABI. If Hyperman
336 * hands back an id past the end of this table the subscription is refused
337 * rather than written out of bounds - a smaller cap here is a limitation, and
338 * a missing bounds check is a corruption. */
339 #define PUNK_BUS_MAX_SUBS 64
340 static SV *PUNK_BUS_APP_CB[PUNK_BUS_MAX_SUBS];
341  
342 /*
343 # uncoverable statement
344 # uncoverable subroutine */
345 0 static void punk_bus_app_cb(pTHX_ const char *topic, STRLEN tlen,
346 const char *payload, STRLEN plen, void *ud) {
347 0 dSP; /* # uncoverable statement */
348 0 SV *cb = (SV *)ud; /* # uncoverable statement */
349 /*
350 # uncoverable statement
351 # uncoverable branch true
352 # uncoverable branch false
353 # uncoverable condition left count:1
354 # uncoverable condition right count:1
355 # uncoverable condition left count:2
356 # uncoverable condition right count:2 */
357 0 ▶ if (!cb || !SvOK(cb)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!cb || !SvOK(cb)) return; 0 0 0
if (!cb || !SvOK(cb)) return; 0 0 0
358 /*
359 # uncoverable statement
360 # uncoverable branch true
361 # uncoverable branch false
362 # uncoverable condition left count:1
363 # uncoverable condition right count:1 */
364 0 ▶ if (tlen < PUNK_BUS_APP_PREFIX_LEN) return;
Branch: 1 branch
BranchTrueFalseTotal
if (tlen < PUNK_BUS_APP_PREFIX_LEN) return; 0 0 0
365  
366 0 ENTER; SAVETMPS; /* # uncoverable statement */
367 0 ▶ PUSHMARK(SP); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); /* # uncoverable statement */ 0 0 0
368 0 ▶ EXTEND(SP, 2); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); /* # uncoverable statement */ 0 0 0
369 0 PUSHs(sv_2mortal(newSVpvn(topic + PUNK_BUS_APP_PREFIX_LEN, /* # uncoverable statement */
370 tlen - PUNK_BUS_APP_PREFIX_LEN)));
371 0 PUSHs(sv_2mortal(newSVpvn(payload, plen))); /* # uncoverable statement */
372 0 PUTBACK; /* # uncoverable statement */
373 /* G_EVAL: this is reached from the event loop with no Perl frame to
374 * unwind into, so a die from a subscriber would take the worker down and
375 * quietly remove it from the pool. */
376 0 (void)call_sv(cb, G_VOID | G_DISCARD | G_EVAL); /* # uncoverable statement */
377 0 SPAGAIN; /* # uncoverable statement */
378 /*
379 # uncoverable statement
380 # uncoverable branch true
381 # uncoverable branch false
382 # uncoverable condition left count:1
383 # uncoverable condition right count:1
384 # uncoverable condition left count:2
385 # uncoverable condition right count:2 */
386 0 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 0 0 0
if (SvTRUE(ERRSV)) 0 0 0
387 0 ▶ warn("Punk: bus subscriber for a topic died: %" SVf, SVfARG(ERRSV)); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
warn("Punk: bus subscriber for a topic died: %" SVf, SVfARG(ERRSV)); /* # uncoverable statement */ 0 0 0
388 0 PUTBACK; /* # uncoverable statement */
389 0 ▶ FREETMPS; LEAVE; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; /* # uncoverable statement */ 0 0 0
390 }
391  
392 /* publish($topic, $payload) -> 1 pool, 0 local-only, -1 too big */
393 2 static int punk_bus_app_publish(pTHX_ SV *topic, SV *payload) {
394 2 const hm_abi *A = punk_hm(aTHX);
395 SV *t;
396 STRLEN tl, pl;
397 const char *tp, *pp;
398 int r;
399 /*
400 # uncoverable condition left count:2 class:ignore_covered_err
401 # uncoverable condition left count:3 class:ignore_covered_err */
402 2 ▶ if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 1 1 2
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 1 0 1
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 0 1 1
403 1 t = punk_bus_app_topic(aTHX_ topic);
404 1 ▶ tp = SvPV_const(t, tl);
Branch: 1 branch
BranchTrueFalseTotal
tp = SvPV_const(t, tl); 1 0 1
405 1 pp = SvPV_const(payload, pl);
406 1 r = A->bus_publish(tp, tl, pp, pl);
407  
408 /* Deliver to THIS process's own subscribers too.
409 *
410 * A publisher deliberately does not poke itself - the poke exists to wake
411 * somebody else's loop - so without this the publishing worker serves
412 * every worker except its own, and its subscribers wait for whatever
413 * unrelated event happens to wake them next. With four SSE streams spread
414 * over two workers that showed up as one hearing the event and three not,
415 * which reads as the bus dropping messages and is nothing of the kind.
416 *
417 * Same rule the room path already follows, and still one delivery path:
418 * this runs the subscriber everybody else runs, synchronously. */
419 /*
420 # uncoverable branch true
421 # uncoverable condition left count:1 class:ignore_covered_err
422 # uncoverable condition left count:2 class:ignore_covered_err
423 # uncoverable condition right count:2 class:ignore_covered_err */
424 1 ▶ if (r == 1 && A->bus_dispatch) (void)A->bus_dispatch(aTHX);
Branch: 2 branches
BranchTrueFalseTotal
if (r == 1 && A->bus_dispatch) (void)A->bus_dispatch(aTHX); 0 1 1
if (r == 1 && A->bus_dispatch) (void)A->bus_dispatch(aTHX); 0 0 0
425 1 return r;
426 }
427  
428 /* subscribe($topic, $cb, $group) -> an id, or -1 */
429 4 static int punk_bus_app_subscribe(pTHX_ SV *topic, SV *cb, SV *group) {
430 4 const hm_abi *A = punk_hm(aTHX);
431 SV *t;
432 4 STRLEN tl, gl = 0;
433 4 const char *tp, *gp = NULL;
434 int id;
435  
436 /*
437 # uncoverable branch true
438 # uncoverable condition left count:1 class:ignore_covered_err
439 # uncoverable condition left count:2 class:ignore_covered_err
440 # uncoverable condition left count:3 class:ignore_covered_err */
441 4 ▶ if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; 4 0 4
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; 4 0 4
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; 0 4 4
442 /*
443 # uncoverable branch true
444 # uncoverable condition left count:1 class:ignore_covered_err
445 # uncoverable condition left count:2 class:ignore_covered_err
446 # uncoverable condition left count:3 class:ignore_covered_err */
447 4 ▶ if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; 4 0 4
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; 4 0 4
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; 0 4 4
448 4 t = punk_bus_app_topic(aTHX_ topic);
449 4 tp = SvPV_const(t, tl);
450 6 ▶ if (group && SvOK(group)) gp = SvPV_const(group, gl); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (group && SvOK(group)) gp = SvPV_const(group, gl); /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
if (group && SvOK(group)) gp = SvPV_const(group, gl); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 2 4
451  
452 /* The coderef goes through as the callback's `ud`, which is what that
453 * argument is for. Reaching into Hyperman's own subscriber table would
454 * need a static it does not export, and would couple Punk to an
455 * implementation detail rather than to the ABI. */
456 {
457 4 SV *held = newSVsv(cb);
458 4 id = A->bus_subscribe(aTHX_ tp, tl, gp, gl, punk_bus_app_cb,
459 (void *)held);
460 /*
461 # uncoverable branch true
462 # uncoverable condition left count:1 class:ignore_covered_err
463 # uncoverable condition left count:2 class:ignore_covered_err */
464 4 ▶ if (id < 0 || id >= PUNK_BUS_MAX_SUBS) {
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { 4 0 4
if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { 0 4 4
465 /* Registered with Hyperman but past this table: hand it back
466 * rather than leak the registration or scribble past the end. */
467 /*
468 # uncoverable statement
469 # uncoverable branch true
470 # uncoverable branch false
471 # uncoverable condition left count:1
472 # uncoverable condition right count:1
473 # uncoverable condition left count:2
474 # uncoverable condition right count:2 */
475 0 ▶ if (id >= 0 && A->bus_unsubscribe) (void)A->bus_unsubscribe(aTHX_ id);
Branch: 2 branches
BranchTrueFalseTotal
if (id >= 0 && A->bus_unsubscribe) (void)A->bus_unsubscribe(aTHX_ id); 0 0 0
if (id >= 0 && A->bus_unsubscribe) (void)A->bus_unsubscribe(aTHX_ id); 0 0 0
476 0 SvREFCNT_dec(held); /* # uncoverable statement */
477 0 return -1; /* # uncoverable statement */
478 }
479 /*
480 # uncoverable branch true
481 # uncoverable condition left count:1 class:ignore_covered_err */
482 4 ▶ if (PUNK_BUS_APP_CB[id]) SvREFCNT_dec(PUNK_BUS_APP_CB[id]);
Branch: 1 branch
BranchTrueFalseTotal
if (PUNK_BUS_APP_CB[id]) SvREFCNT_dec(PUNK_BUS_APP_CB[id]); 0 4 4
483 4 PUNK_BUS_APP_CB[id] = held; /* kept alive for as long as the
484 * subscription is registered */
485 }
486 4 return id;
487 }
488  
489 /* ---- cache invalidation -------------------------------------------------- *
490 *
491 * Its own namespace, separate from application topics and from room frames.
492 * A cache invalidating on a topic an application could publish to by accident
493 * would be a cache that empties itself when somebody names a topic badly.
494 *
495 * What travels is the KEY, never the value. Publishing values would make this
496 * a replication system - every worker paying to hold everything whether it
497 * will be asked for it or not - and a bus slot is 2KB, so a cached page would
498 * be refused outright and the pool would silently diverge.
499 */
500 #define PUNK_BUS_CACHE_PREFIX "pkc:"
501 #define PUNK_BUS_CACHE_PREFIX_LEN 4
502  
503 static SV *PUNK_BUS_CACHE_CB[PUNK_BUS_MAX_SUBS];
504  
505 61 static SV *punk_bus_cache_topic(pTHX_ SV *name) {
506 STRLEN nl;
507 61 const char *np = SvPV_const(name, nl);
508 61 SV *out = sv_2mortal(newSVpvn(PUNK_BUS_CACHE_PREFIX,
509 PUNK_BUS_CACHE_PREFIX_LEN));
510 61 sv_catpvn(out, np, nl);
511 61 return out;
512 }
513  
514 16 static void punk_bus_cache_cb(pTHX_ const char *topic, STRLEN tlen,
515 const char *payload, STRLEN plen, void *ud) {
516 16 dSP;
517 16 SV *cb = (SV *)ud;
518 PERL_UNUSED_ARG(topic);
519 PERL_UNUSED_ARG(tlen);
520 /*
521 # uncoverable branch true
522 # uncoverable condition left count:1 class:ignore_covered_err
523 # uncoverable condition left count:2 class:ignore_covered_err */
524 16 ▶ if (!cb || !SvOK(cb)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!cb || !SvOK(cb)) return; 16 0 16
if (!cb || !SvOK(cb)) return; 0 16 16
525  
526 16 ENTER; SAVETMPS;
527 16 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 16 16
528 16 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 16 16
529 16 PUSHs(sv_2mortal(newSVpvn(payload, plen)));
530 16 PUTBACK;
531 /* G_EVAL: reached from the event loop with no Perl frame to unwind into,
532 * and an invalidation that died would take the worker with it. */
533 16 (void)call_sv(cb, G_VOID | G_DISCARD | G_EVAL);
534 16 SPAGAIN;
535 /*
536 # uncoverable branch true
537 # uncoverable condition right count:1 class:ignore_covered_err
538 # uncoverable condition left count:2 class:ignore_covered_err */
539 16 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 16 0 16
if (SvTRUE(ERRSV)) 0 16 16
540 0 ▶ warn("Punk::Cache: an invalidation handler died: %" SVf, /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
warn("Punk::Cache: an invalidation handler died: %" SVf, /* # uncoverable statement */ 0 0 0
541 SVfARG(ERRSV)); /* # uncoverable statement */
542 16 PUTBACK;
543 16 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 16 0 16
544 }
545  
546 /* 1 published to the pool, 0 local only. */
547 22 static int punk_bus_cache_publish(pTHX_ SV *name, SV *key) {
548 22 const hm_abi *A = punk_hm(aTHX);
549 SV *t;
550 STRLEN tl, kl;
551 const char *tp, *kp;
552 /*
553 # uncoverable branch true
554 # uncoverable condition left count:1 class:ignore_covered_err
555 # uncoverable condition left count:2 class:ignore_covered_err
556 # uncoverable condition left count:3 class:ignore_covered_err */
557 22 ▶ if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 22 0 22
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 22 0 22
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; 0 22 22
558 22 t = punk_bus_cache_topic(aTHX_ name);
559 22 ▶ tp = SvPV_const(t, tl);
Branch: 1 branch
BranchTrueFalseTotal
tp = SvPV_const(t, tl); 22 0 22
560 22 kp = SvPV_const(key, kl);
561 22 return A->bus_publish(tp, tl, kp, kl) == 1;
562 }
563  
564 39 static int punk_bus_cache_subscribe(pTHX_ SV *name, SV *cb) {
565 39 const hm_abi *A = punk_hm(aTHX);
566 SV *t, *held;
567 STRLEN tl;
568 const char *tp;
569 int id;
570  
571 /*
572 # uncoverable branch true
573 # uncoverable condition left count:1 class:ignore_covered_err
574 # uncoverable condition left count:2 class:ignore_covered_err
575 # uncoverable condition left count:3 class:ignore_covered_err */
576 39 ▶ if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; 39 0 39
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; 39 0 39
if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; 0 39 39
577 /*
578 # uncoverable branch true
579 # uncoverable condition left count:1 class:ignore_covered_err
580 # uncoverable condition left count:2 class:ignore_covered_err
581 # uncoverable condition left count:3 class:ignore_covered_err */
582 39 ▶ if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; 39 0 39
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; 39 0 39
if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; 0 39 39
583 39 t = punk_bus_cache_topic(aTHX_ name);
584 39 tp = SvPV_const(t, tl);
585  
586 39 held = newSVsv(cb);
587 39 id = A->bus_subscribe(aTHX_ tp, tl, NULL, 0, punk_bus_cache_cb,
588 (void *)held);
589 /*
590 # uncoverable branch true
591 # uncoverable condition left count:1 class:ignore_covered_err
592 # uncoverable condition left count:2 class:ignore_covered_err */
593 39 ▶ if (id < 0 || id >= PUNK_BUS_MAX_SUBS) {
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { 39 0 39
if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { 0 39 39
594 /*
595 # uncoverable statement
596 # uncoverable branch true
597 # uncoverable branch false
598 # uncoverable condition left count:1
599 # uncoverable condition right count:1
600 # uncoverable condition left count:2
601 # uncoverable condition right count:2 */
602 0 ▶ if (id >= 0 && A->bus_unsubscribe) (void)A->bus_unsubscribe(aTHX_ id);
Branch: 2 branches
BranchTrueFalseTotal
if (id >= 0 && A->bus_unsubscribe) (void)A->bus_unsubscribe(aTHX_ id); 0 0 0
if (id >= 0 && A->bus_unsubscribe) (void)A->bus_unsubscribe(aTHX_ id); 0 0 0
603 0 SvREFCNT_dec(held); /* # uncoverable statement */
604 0 return -1; /* # uncoverable statement */
605 }
606 /*
607 # uncoverable branch true
608 # uncoverable condition left count:1 class:ignore_covered_err */
609 39 ▶ if (PUNK_BUS_CACHE_CB[id]) SvREFCNT_dec(PUNK_BUS_CACHE_CB[id]);
Branch: 1 branch
BranchTrueFalseTotal
if (PUNK_BUS_CACHE_CB[id]) SvREFCNT_dec(PUNK_BUS_CACHE_CB[id]); 0 39 39
610 39 PUNK_BUS_CACHE_CB[id] = held;
611 39 return id;
612 }
613  
614 #endif /* PUNK_BUS_H */
« xs/headers.xs ↑ Summary include/punk/punk_wsroom.h »