| 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 |
0 |
| static int punk_bus_live(pTHX) { |
| 58 |
0 |
| const hm_abi *A = punk_hm(aTHX); |
| 59 |
0 |
▶ | if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 60 |
0 |
| return 1; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
|
| /* ONE topic for every room, with the room name inside the payload. |
| 64 |
|
| * |
| 65 |
|
| * The obvious design is a topic per room - "punk:ws:lobby" - but the bus |
| 66 |
|
| * matches topics EXACTLY and has no wildcards, deliberately, so that would |
| 67 |
|
| * mean a subscription per room and a registration to add and tear down every |
| 68 |
|
| * time a room came or went. One topic and one subscription instead, with the |
| 69 |
|
| * name length-prefixed ahead of the frame. |
| 70 |
|
| * |
| 71 |
|
| * A length prefix rather than a separator, because a separator has to be a |
| 72 |
|
| * byte a room name cannot contain, and "cannot" is a promise about somebody |
| 73 |
|
| * else's strings that this has no business making. |
| 74 |
|
| * |
| 75 |
|
| * [u16 name length][name][u32 pid][u64 connection][the encoded frame] |
| 76 |
|
| * |
| 77 |
|
| * The pid and connection name the one member a broadcast leaves out, or are |
| 78 |
|
| * zero. A connection lives in one worker, so its address means something |
| 79 |
|
| * only there - and forked workers lay their memory out alike, so the same |
| 80 |
|
| * address in another worker is somebody else. The pid says which worker the |
| 81 |
|
| * address belongs to; every other worker delivers to all of its members. |
| 82 |
|
| */ |
| 83 |
|
| #define PUNK_BUS_ROOM_SKIP_LEN 12 |
| 84 |
|
| #define PUNK_BUS_ROOM_TOPIC "punk:ws" |
| 85 |
|
| #define PUNK_BUS_ROOM_TOPIC_LEN 7 |
| 86 |
|
| |
| 87 |
|
| /* Pack a room broadcast. Mortal SV. */ |
| 88 |
3 |
| static SV *punk_bus_room_pack(pTHX_ SV *name, SV *frame, void *skip) { |
| 89 |
|
| STRLEN nl, fl; |
| 90 |
3 |
▶ | const char *np = SvPV_const(name, nl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *np = SvPV_const(name, nl); |
3 |
0 |
3 |
|
| 91 |
3 |
| const char *fp = SvPV_const(frame, fl); |
| 92 |
|
| SV *out; |
| 93 |
|
| unsigned char hdr[2], who[PUNK_BUS_ROOM_SKIP_LEN]; |
| 94 |
3 |
▶ | U32 pid = skip ? (U32)PerlProc_getpid() : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| U32 pid = skip ? (U32)PerlProc_getpid() : 0; |
1 |
2 |
3 |
|
| 95 |
3 |
| UV addr = PTR2UV(skip); |
| 96 |
|
| int i; |
| 97 |
3 |
▶ | if (nl > 0xFFFF) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nl > 0xFFFF) return NULL; |
0 |
3 |
3 |
|
| 98 |
3 |
| hdr[0] = (unsigned char)((nl >> 8) & 0xFF); |
| 99 |
3 |
| hdr[1] = (unsigned char)(nl & 0xFF); |
| 100 |
15 |
▶ | for (i = 0; i < 4; i++) who[i] = (unsigned char)(pid >> (24 - 8 * i)); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 4; i++) who[i] = (unsigned char)(pid >> (24 - 8 * i)); |
12 |
3 |
15 |
|
| 101 |
27 |
▶ | for (i = 0; i < 8; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) |
24 |
3 |
27 |
|
| 102 |
24 |
| who[4 + i] = (unsigned char)(((uint64_t)addr >> (56 - 8 * i)) & 0xFF); |
| 103 |
3 |
| out = sv_2mortal(newSVpvn((const char *)hdr, 2)); |
| 104 |
3 |
| sv_catpvn(out, np, nl); |
| 105 |
3 |
| sv_catpvn(out, (const char *)who, sizeof who); |
| 106 |
3 |
| sv_catpvn(out, fp, fl); |
| 107 |
3 |
| return out; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| /* A frame arriving from any worker, including this one. |
| 111 |
|
| * |
| 112 |
|
| * The payload is the ALREADY ENCODED WebSocket frame, so it is encoded once |
| 113 |
|
| * per broadcast rather than once per worker, and every member of the room |
| 114 |
|
| * across the whole pool is sent byte-identical bytes. That is the property |
| 115 |
|
| * pwr_fan was built for; publishing the raw text instead would have thrown it |
| 116 |
|
| * away and re-encoded N times. |
| 117 |
|
| * |
| 118 |
|
| * MUST NOT CROAK: reached from the event loop with no Perl frame to unwind |
| 119 |
|
| * into. Everything here is either C or a call that cannot die. |
| 120 |
|
| */ |
| 121 |
3 |
| static void punk_bus_room_cb_in(pTHX_ const char *topic, STRLEN tlen, |
| 122 |
|
| const char *payload, STRLEN plen, void *ud) { |
| 123 |
|
| SV *name, *frame, *room; |
| 124 |
|
| STRLEN nl; |
| 125 |
3 |
| void *skip = NULL; |
| 126 |
|
| PERL_UNUSED_ARG(topic); |
| 127 |
|
| PERL_UNUSED_ARG(tlen); |
| 128 |
|
| PERL_UNUSED_ARG(ud); |
| 129 |
|
| |
| 130 |
3 |
▶ | if (plen < 2) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (plen < 2) return; |
0 |
3 |
3 |
|
| 131 |
3 |
| nl = ((STRLEN)(unsigned char)payload[0] << 8) |
| 132 |
3 |
| | (STRLEN)(unsigned char)payload[1]; |
| 133 |
|
| /* truncated: refuse to guess */ |
| 134 |
3 |
▶ | if (plen < 2 + nl + PUNK_BUS_ROOM_SKIP_LEN) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (plen < 2 + nl + PUNK_BUS_ROOM_SKIP_LEN) return; |
0 |
3 |
3 |
|
| 135 |
|
| |
| 136 |
|
| { /* the member left out, when it is one of ours */ |
| 137 |
3 |
| const unsigned char *w = (const unsigned char *)payload + 2 + nl; |
| 138 |
3 |
| U32 pid = 0; |
| 139 |
3 |
| uint64_t addr = 0; |
| 140 |
|
| int i; |
| 141 |
15 |
▶ | for (i = 0; i < 4; i++) pid = (pid << 8) | w[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 4; i++) pid = (pid << 8) | w[i]; |
12 |
3 |
15 |
|
| 142 |
27 |
▶ | for (i = 0; i < 8; i++) addr = (addr << 8) | w[4 + i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) addr = (addr << 8) | w[4 + i]; |
24 |
3 |
27 |
|
| 143 |
3 |
▶ | if (pid && pid == (U32)PerlProc_getpid()) skip = INT2PTR(void *, (UV)addr); |
Branch: 2 branches
| Branch | True | False | Total |
| if (pid && pid == (U32)PerlProc_getpid()) skip = INT2PTR(void *, (UV)addr); |
1 |
2 |
3 |
| if (pid && pid == (U32)PerlProc_getpid()) skip = INT2PTR(void *, (UV)addr); |
1 |
0 |
1 |
|
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
3 |
| name = sv_2mortal(newSVpvn(payload + 2, nl)); |
| 147 |
3 |
| frame = sv_2mortal(newSVpvn(payload + 2 + nl + PUNK_BUS_ROOM_SKIP_LEN, |
| 148 |
|
| plen - 2 - nl - PUNK_BUS_ROOM_SKIP_LEN)); |
| 149 |
|
| |
| 150 |
|
| /* A lookup, never a creation. A worker that has not named this room |
| 151 |
|
| * has no members in it, and creating it here let one client's stream |
| 152 |
|
| * of fresh names grow every worker in the pool for the pool's life. */ |
| 153 |
|
| { |
| 154 |
|
| SV **rp; |
| 155 |
3 |
▶ | if (!pwr_rooms || pwr_rooms_pid != (IV)PerlProc_getpid()) return; |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 156 |
3 |
| rp = hv_fetch(pwr_rooms, SvPVX(name), (I32)nl, 0); |
| 157 |
3 |
▶ | if (!(rp && *rp && SvROK(*rp))) return; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 158 |
3 |
| room = *rp; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
3 |
| PUNK_BUS_LAST_FAN += pwr_fan_skip(aTHX_ room, frame, skip); |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
3 |
| static void punk_bus_room_cb(pTHX_ const char *topic, STRLEN tlen, |
| 165 |
|
| const char *payload, STRLEN plen, void *ud) { |
| 166 |
3 |
| PK_LOOP_ENTER; |
| 167 |
3 |
| punk_bus_room_cb_in(aTHX_ topic, tlen, payload, plen, ud); |
| 168 |
3 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
3 |
0 |
3 |
|
| 169 |
3 |
| } |
| 170 |
|
| |
| 171 |
|
| /* Register this worker's subscriptions. Called from the worker hook, so it |
| 172 |
|
| * runs once per worker, after the fork, on the process that will serve. */ |
| 173 |
680 |
| static void punk_bus_attach(pTHX) { |
| 174 |
|
| const hm_abi *A; |
| 175 |
680 |
▶ | if (PUNK_BUS_READY) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (PUNK_BUS_READY) return; |
505 |
175 |
680 |
|
| 176 |
175 |
| A = punk_hm(aTHX); |
| 177 |
175 |
▶ | if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return; |
175 |
0 |
175 |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return; |
175 |
0 |
175 |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return; |
0 |
175 |
175 |
|
| 178 |
175 |
| PUNK_BUS_READY = 1; |
| 179 |
|
| /* One subscription for every room. The topic carries the room name, so a |
| 180 |
|
| * per-room registration would buy nothing and would need tearing down as |
| 181 |
|
| * rooms come and go. */ |
| 182 |
175 |
| PUNK_BUS_ROOMSUB = A->bus_subscribe(aTHX_ PUNK_BUS_ROOM_TOPIC, |
| 183 |
|
| PUNK_BUS_ROOM_TOPIC_LEN, |
| 184 |
|
| NULL, 0, punk_bus_room_cb, NULL); |
| 185 |
|
| } |
| 186 |
|
| |
| 187 |
|
| /* Publish a room broadcast to the pool. |
| 188 |
|
| * |
| 189 |
|
| * Returns 1 when it reached the ring - in which case the caller must NOT also |
| 190 |
|
| * send locally, because this worker's own subscriber will deliver it like |
| 191 |
|
| * everybody else's. Returns 0 when there is no pool, and then the caller does |
| 192 |
|
| * the local send it always did. |
| 193 |
|
| * |
| 194 |
|
| * That is the whole "one delivery path" rule, expressed as a return value. */ |
| 195 |
4 |
| static int punk_bus_room_publish(pTHX_ SV *name, SV *frame, SV *except) { |
| 196 |
4 |
| const hm_abi *A = punk_hm(aTHX); |
| 197 |
|
| SV *packed; |
| 198 |
|
| STRLEN pl; |
| 199 |
|
| const char *pp; |
| 200 |
|
| |
| 201 |
|
| /* No subscription in THIS process means nothing here would deliver the |
| 202 |
|
| * frame to this worker's own members - so publishing would serve the rest |
| 203 |
|
| * of the pool and silently skip the people in front of us. Fall back to |
| 204 |
|
| * the local fan instead, which is what a Punk outside a worker does |
| 205 |
|
| * anyway. */ |
| 206 |
4 |
▶ | if (!PUNK_BUS_READY) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PUNK_BUS_READY) return 0; |
1 |
3 |
4 |
|
| 207 |
3 |
▶ | if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 208 |
6 |
▶ | packed = punk_bus_room_pack(aTHX_ name, frame, |
Branch: 1 branch
| Branch | True | False | Total |
| packed = punk_bus_room_pack(aTHX_ name, frame, |
3 |
0 |
3 |
|
| 209 |
3 |
▶ | (except && SvOK(except)) ? PWR_ADDR(except) : NULL); |
Branch: 2 branches
| Branch | True | False | Total |
| (except && SvOK(except)) ? PWR_ADDR(except) : NULL); |
1 |
2 |
3 |
| (except && SvOK(except)) ? PWR_ADDR(except) : NULL); |
1 |
0 |
1 |
|
| 210 |
3 |
▶ | if (!packed) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!packed) return 0; |
0 |
3 |
3 |
|
| 211 |
3 |
| pp = SvPV_const(packed, pl); |
| 212 |
|
| /* A refusal (too big for a slot) is NOT success: the caller falls back to |
| 213 |
|
| * the local send, so the members on this worker still get their frame |
| 214 |
|
| * rather than nobody getting anything. */ |
| 215 |
3 |
▶ | if (A->bus_publish(PUNK_BUS_ROOM_TOPIC, PUNK_BUS_ROOM_TOPIC_LEN, pp, pl) |
Branch: 1 branch
| Branch | True | False | Total |
| if (A->bus_publish(PUNK_BUS_ROOM_TOPIC, PUNK_BUS_ROOM_TOPIC_LEN, pp, pl) |
0 |
3 |
3 |
|
| 216 |
|
| != 1) |
| 217 |
0 |
| return 0; |
| 218 |
|
| |
| 219 |
|
| /* Run this worker's own subscription NOW, so the local half of the |
| 220 |
|
| * broadcast has happened by the time broadcast() returns and there is a |
| 221 |
|
| * real number to give back. The other workers get it from their wakeup. |
| 222 |
|
| * |
| 223 |
|
| * Still ONE delivery path: this is the same subscriber they run, invoked |
| 224 |
|
| * synchronously rather than from the loop. */ |
| 225 |
3 |
| PUNK_BUS_LAST_FAN = 0; |
| 226 |
3 |
▶ | if (A->abi_version >= PUNK_BUS_ABI_MIN && A->bus_dispatch) |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 227 |
3 |
| (void)A->bus_dispatch(aTHX); |
| 228 |
3 |
| return 1; |
| 229 |
|
| } |
| 230 |
|
| |
| 231 |
|
| /* The hm_abi worker callback: this is what runs inside each worker. */ |
| 232 |
7 |
| static void punk_bus_worker_cb(pTHX_ void *loop, void *ud) { |
| 233 |
|
| PERL_UNUSED_ARG(loop); |
| 234 |
|
| PERL_UNUSED_ARG(ud); |
| 235 |
7 |
| punk_bus_attach(aTHX); |
| 236 |
7 |
| } |
| 237 |
|
| |
| 238 |
|
| /* Ask Hyperman to run punk_bus_attach in every worker. |
| 239 |
|
| * |
| 240 |
|
| * Called from compile(), which runs in the parent before any fork - the only |
| 241 |
|
| * moment a registration reaches every worker. Registering from inside a |
| 242 |
|
| * request would reach exactly one of them, which is the same class of mistake |
| 243 |
|
| * the bus exists to fix. */ |
| 244 |
674 |
| static void punk_bus_register(pTHX) { |
| 245 |
674 |
| const hm_abi *A = punk_hm(aTHX); |
| 246 |
674 |
▶ | if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->on_worker_start) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->on_worker_start) return; |
673 |
1 |
674 |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->on_worker_start) return; |
673 |
0 |
673 |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->on_worker_start) return; |
0 |
673 |
673 |
|
| 247 |
673 |
| (void)A->on_worker_start(aTHX_ punk_bus_worker_cb, NULL); |
| 248 |
|
| /* A single-process server never forks and so never fires the hook. It |
| 249 |
|
| * still has a bus (the arena is mapped either way), so attach now as |
| 250 |
|
| * well; punk_bus_attach is idempotent. */ |
| 251 |
673 |
| punk_bus_attach(aTHX); |
| 252 |
|
| } |
| 253 |
|
| |
| 254 |
|
| /* ---- the application's own use of the bus -------------------------------- * |
| 255 |
|
| * |
| 256 |
|
| * Rooms are the reason this exists, but they are not the only thing that wants |
| 257 |
|
| * it: a cache key changing, a config being re-read, a presence update. So the |
| 258 |
|
| * same ring is reachable directly. |
| 259 |
|
| * |
| 260 |
|
| * Topics are namespaced away from Punk's own - an application publishing to |
| 261 |
|
| * "punk:ws" would otherwise be handing raw bytes to the room fanout, which |
| 262 |
|
| * expects a length-prefixed name in front of a WebSocket frame. */ |
| 263 |
|
| #define PUNK_BUS_APP_PREFIX "app:" |
| 264 |
|
| #define PUNK_BUS_APP_PREFIX_LEN 4 |
| 265 |
|
| |
| 266 |
3 |
| static SV *punk_bus_app_topic(pTHX_ SV *topic) { |
| 267 |
|
| STRLEN tl; |
| 268 |
3 |
| const char *tp = SvPV_const(topic, tl); |
| 269 |
3 |
| SV *out = sv_2mortal(newSVpvn(PUNK_BUS_APP_PREFIX, PUNK_BUS_APP_PREFIX_LEN)); |
| 270 |
3 |
| sv_catpvn(out, tp, tl); |
| 271 |
3 |
| return out; |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
|
| /* An application subscription. The coderef is held here, in this process, |
| 275 |
|
| * because a callback cannot cross a fork - the ring is the shared thing, not |
| 276 |
|
| * the list of people reading it. |
| 277 |
|
| * |
| 278 |
|
| * Punk's own cap, not Hyperman's: HM_BUS_SUBS lives in hm_bus.h, which is the |
| 279 |
|
| * implementation rather than the ABI, and Punk consumes the ABI. If Hyperman |
| 280 |
|
| * hands back an id past the end of this table the subscription is refused |
| 281 |
|
| * rather than written out of bounds - a smaller cap here is a limitation, and |
| 282 |
|
| * a missing bounds check is a corruption. */ |
| 283 |
|
| #define PUNK_BUS_MAX_SUBS 64 |
| 284 |
|
| static SV *PUNK_BUS_APP_CB[PUNK_BUS_MAX_SUBS]; |
| 285 |
|
| |
| 286 |
0 |
| static void punk_bus_app_cb(pTHX_ const char *topic, STRLEN tlen, |
| 287 |
|
| const char *payload, STRLEN plen, void *ud) { |
| 288 |
0 |
| dSP; |
| 289 |
0 |
| SV *cb = (SV *)ud; |
| 290 |
0 |
▶ | if (!cb || !SvOK(cb)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cb || !SvOK(cb)) return; |
0 |
0 |
0 |
| if (!cb || !SvOK(cb)) return; |
0 |
0 |
0 |
|
| 291 |
0 |
▶ | if (tlen < PUNK_BUS_APP_PREFIX_LEN) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tlen < PUNK_BUS_APP_PREFIX_LEN) return; |
0 |
0 |
0 |
|
| 292 |
|
| |
| 293 |
0 |
| ENTER; SAVETMPS; |
| 294 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 295 |
0 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
0 |
0 |
|
| 296 |
0 |
| PUSHs(sv_2mortal(newSVpvn(topic + PUNK_BUS_APP_PREFIX_LEN, |
| 297 |
|
| tlen - PUNK_BUS_APP_PREFIX_LEN))); |
| 298 |
0 |
| PUSHs(sv_2mortal(newSVpvn(payload, plen))); |
| 299 |
0 |
| PUTBACK; |
| 300 |
|
| /* G_EVAL: this is reached from the event loop with no Perl frame to |
| 301 |
|
| * unwind into, so a die from a subscriber would take the worker down and |
| 302 |
|
| * quietly remove it from the pool. */ |
| 303 |
0 |
| (void)call_sv(cb, G_VOID | G_DISCARD | G_EVAL); |
| 304 |
0 |
| SPAGAIN; |
| 305 |
0 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
0 |
0 |
0 |
| if (SvTRUE(ERRSV)) |
0 |
0 |
0 |
|
| 306 |
0 |
▶ | warn("Punk: bus subscriber for a topic died: %" SVf, SVfARG(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| warn("Punk: bus subscriber for a topic died: %" SVf, SVfARG(ERRSV)); |
0 |
0 |
0 |
|
| 307 |
0 |
| PUTBACK; |
| 308 |
0 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 309 |
|
| } |
| 310 |
|
| |
| 311 |
|
| /* publish($topic, $payload) -> 1 pool, 0 local-only, -1 too big */ |
| 312 |
1 |
| static int punk_bus_app_publish(pTHX_ SV *topic, SV *payload) { |
| 313 |
1 |
| const hm_abi *A = punk_hm(aTHX); |
| 314 |
|
| SV *t; |
| 315 |
|
| STRLEN tl, pl; |
| 316 |
|
| const char *tp, *pp; |
| 317 |
|
| int r; |
| 318 |
1 |
▶ | if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; |
0 |
1 |
1 |
| 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 |
|
| 319 |
0 |
| t = punk_bus_app_topic(aTHX_ topic); |
| 320 |
0 |
▶ | tp = SvPV_const(t, tl); |
Branch: 1 branch
| Branch | True | False | Total |
| tp = SvPV_const(t, tl); |
0 |
0 |
0 |
|
| 321 |
0 |
| pp = SvPV_const(payload, pl); |
| 322 |
0 |
| r = A->bus_publish(tp, tl, pp, pl); |
| 323 |
|
| |
| 324 |
|
| /* Deliver to THIS process's own subscribers too. |
| 325 |
|
| * |
| 326 |
|
| * A publisher deliberately does not poke itself - the poke exists to wake |
| 327 |
|
| * somebody else's loop - so without this the publishing worker serves |
| 328 |
|
| * every worker except its own, and its subscribers wait for whatever |
| 329 |
|
| * unrelated event happens to wake them next. With four SSE streams spread |
| 330 |
|
| * over two workers that showed up as one hearing the event and three not, |
| 331 |
|
| * which reads as the bus dropping messages and is nothing of the kind. |
| 332 |
|
| * |
| 333 |
|
| * Same rule the room path already follows, and still one delivery path: |
| 334 |
|
| * this runs the subscriber everybody else runs, synchronously. */ |
| 335 |
0 |
▶ | if (r == 1 && A->bus_dispatch) (void)A->bus_dispatch(aTHX); |
Branch: 2 branches
| Branch | True | False | Total |
| if (r == 1 && A->bus_dispatch) (void)A->bus_dispatch(aTHX); |
0 |
0 |
0 |
| if (r == 1 && A->bus_dispatch) (void)A->bus_dispatch(aTHX); |
0 |
0 |
0 |
|
| 336 |
0 |
| return r; |
| 337 |
|
| } |
| 338 |
|
| |
| 339 |
|
| /* subscribe($topic, $cb, $group) -> an id, or -1 */ |
| 340 |
3 |
| static int punk_bus_app_subscribe(pTHX_ SV *topic, SV *cb, SV *group) { |
| 341 |
3 |
| const hm_abi *A = punk_hm(aTHX); |
| 342 |
|
| SV *t; |
| 343 |
3 |
| STRLEN tl, gl = 0; |
| 344 |
3 |
| const char *tp, *gp = NULL; |
| 345 |
|
| int id; |
| 346 |
|
| |
| 347 |
3 |
▶ | if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; |
3 |
0 |
3 |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; |
3 |
0 |
3 |
| if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; |
0 |
3 |
3 |
|
| 348 |
3 |
▶ | if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; |
3 |
0 |
3 |
| if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; |
3 |
0 |
3 |
| if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; |
0 |
3 |
3 |
|
| 349 |
3 |
| t = punk_bus_app_topic(aTHX_ topic); |
| 350 |
3 |
| tp = SvPV_const(t, tl); |
| 351 |
4 |
▶ | if (group && SvOK(group)) gp = SvPV_const(group, gl); |
Branch: 2 branches
| Branch | True | False | Total |
| if (group && SvOK(group)) gp = SvPV_const(group, gl); |
3 |
0 |
3 |
| if (group && SvOK(group)) gp = SvPV_const(group, gl); |
1 |
2 |
3 |
|
| 352 |
|
| |
| 353 |
|
| /* The coderef goes through as the callback's `ud`, which is what that |
| 354 |
|
| * argument is for. Reaching into Hyperman's own subscriber table would |
| 355 |
|
| * need a static it does not export, and would couple Punk to an |
| 356 |
|
| * implementation detail rather than to the ABI. */ |
| 357 |
|
| { |
| 358 |
3 |
| SV *held = newSVsv(cb); |
| 359 |
3 |
| id = A->bus_subscribe(aTHX_ tp, tl, gp, gl, punk_bus_app_cb, |
| 360 |
|
| (void *)held); |
| 361 |
3 |
▶ | if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { |
3 |
0 |
3 |
| if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { |
0 |
3 |
3 |
|
| 362 |
|
| /* Registered with Hyperman but past this table: hand it back |
| 363 |
|
| * rather than leak the registration or scribble past the end. */ |
| 364 |
0 |
▶ | if (id >= 0 && A->bus_unsubscribe) (void)A->bus_unsubscribe(aTHX_ id); |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 365 |
0 |
| SvREFCNT_dec(held); |
| 366 |
0 |
| return -1; |
| 367 |
|
| } |
| 368 |
3 |
▶ | if (PUNK_BUS_APP_CB[id]) SvREFCNT_dec(PUNK_BUS_APP_CB[id]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (PUNK_BUS_APP_CB[id]) SvREFCNT_dec(PUNK_BUS_APP_CB[id]); |
0 |
3 |
3 |
|
| 369 |
3 |
| PUNK_BUS_APP_CB[id] = held; /* kept alive for as long as the |
| 370 |
|
| * subscription is registered */ |
| 371 |
|
| } |
| 372 |
3 |
| return id; |
| 373 |
|
| } |
| 374 |
|
| |
| 375 |
|
| /* ---- cache invalidation -------------------------------------------------- * |
| 376 |
|
| * |
| 377 |
|
| * Its own namespace, separate from application topics and from room frames. |
| 378 |
|
| * A cache invalidating on a topic an application could publish to by accident |
| 379 |
|
| * would be a cache that empties itself when somebody names a topic badly. |
| 380 |
|
| * |
| 381 |
|
| * What travels is the KEY, never the value. Publishing values would make this |
| 382 |
|
| * a replication system - every worker paying to hold everything whether it |
| 383 |
|
| * will be asked for it or not - and a bus slot is 2KB, so a cached page would |
| 384 |
|
| * be refused outright and the pool would silently diverge. |
| 385 |
|
| */ |
| 386 |
|
| #define PUNK_BUS_CACHE_PREFIX "pkc:" |
| 387 |
|
| #define PUNK_BUS_CACHE_PREFIX_LEN 4 |
| 388 |
|
| |
| 389 |
|
| static SV *PUNK_BUS_CACHE_CB[PUNK_BUS_MAX_SUBS]; |
| 390 |
|
| |
| 391 |
61 |
| static SV *punk_bus_cache_topic(pTHX_ SV *name) { |
| 392 |
|
| STRLEN nl; |
| 393 |
61 |
| const char *np = SvPV_const(name, nl); |
| 394 |
61 |
| SV *out = sv_2mortal(newSVpvn(PUNK_BUS_CACHE_PREFIX, |
| 395 |
|
| PUNK_BUS_CACHE_PREFIX_LEN)); |
| 396 |
61 |
| sv_catpvn(out, np, nl); |
| 397 |
61 |
| return out; |
| 398 |
|
| } |
| 399 |
|
| |
| 400 |
16 |
| static void punk_bus_cache_cb(pTHX_ const char *topic, STRLEN tlen, |
| 401 |
|
| const char *payload, STRLEN plen, void *ud) { |
| 402 |
16 |
| dSP; |
| 403 |
16 |
| SV *cb = (SV *)ud; |
| 404 |
|
| PERL_UNUSED_ARG(topic); |
| 405 |
|
| PERL_UNUSED_ARG(tlen); |
| 406 |
16 |
▶ | if (!cb || !SvOK(cb)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cb || !SvOK(cb)) return; |
16 |
0 |
16 |
| if (!cb || !SvOK(cb)) return; |
0 |
16 |
16 |
|
| 407 |
|
| |
| 408 |
16 |
| ENTER; SAVETMPS; |
| 409 |
16 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
16 |
16 |
|
| 410 |
16 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
16 |
16 |
|
| 411 |
16 |
| PUSHs(sv_2mortal(newSVpvn(payload, plen))); |
| 412 |
16 |
| PUTBACK; |
| 413 |
|
| /* G_EVAL: reached from the event loop with no Perl frame to unwind into, |
| 414 |
|
| * and an invalidation that died would take the worker with it. */ |
| 415 |
16 |
| (void)call_sv(cb, G_VOID | G_DISCARD | G_EVAL); |
| 416 |
16 |
| SPAGAIN; |
| 417 |
16 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
16 |
0 |
16 |
| if (SvTRUE(ERRSV)) |
0 |
16 |
16 |
|
| 418 |
0 |
▶ | warn("Punk::Cache: an invalidation handler died: %" SVf, |
Branch: 1 branch
| Branch | True | False | Total |
| warn("Punk::Cache: an invalidation handler died: %" SVf, |
0 |
0 |
0 |
|
| 419 |
|
| SVfARG(ERRSV)); |
| 420 |
16 |
| PUTBACK; |
| 421 |
16 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
16 |
0 |
16 |
|
| 422 |
|
| } |
| 423 |
|
| |
| 424 |
|
| /* 1 published to the pool, 0 local only. */ |
| 425 |
22 |
| static int punk_bus_cache_publish(pTHX_ SV *name, SV *key) { |
| 426 |
22 |
| const hm_abi *A = punk_hm(aTHX); |
| 427 |
|
| SV *t; |
| 428 |
|
| STRLEN tl, kl; |
| 429 |
|
| const char *tp, *kp; |
| 430 |
22 |
▶ | if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_publish) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 431 |
22 |
| t = punk_bus_cache_topic(aTHX_ name); |
| 432 |
22 |
▶ | tp = SvPV_const(t, tl); |
Branch: 1 branch
| Branch | True | False | Total |
| tp = SvPV_const(t, tl); |
22 |
0 |
22 |
|
| 433 |
22 |
| kp = SvPV_const(key, kl); |
| 434 |
22 |
| return A->bus_publish(tp, tl, kp, kl) == 1; |
| 435 |
|
| } |
| 436 |
|
| |
| 437 |
39 |
| static int punk_bus_cache_subscribe(pTHX_ SV *name, SV *cb) { |
| 438 |
39 |
| const hm_abi *A = punk_hm(aTHX); |
| 439 |
|
| SV *t, *held; |
| 440 |
|
| STRLEN tl; |
| 441 |
|
| const char *tp; |
| 442 |
|
| int id; |
| 443 |
|
| |
| 444 |
39 |
▶ | if (!A || A->abi_version < PUNK_BUS_ABI_MIN || !A->bus_subscribe) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 445 |
39 |
▶ | if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 446 |
39 |
| t = punk_bus_cache_topic(aTHX_ name); |
| 447 |
39 |
| tp = SvPV_const(t, tl); |
| 448 |
|
| |
| 449 |
39 |
| held = newSVsv(cb); |
| 450 |
39 |
| id = A->bus_subscribe(aTHX_ tp, tl, NULL, 0, punk_bus_cache_cb, |
| 451 |
|
| (void *)held); |
| 452 |
39 |
▶ | if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { |
39 |
0 |
39 |
| if (id < 0 || id >= PUNK_BUS_MAX_SUBS) { |
0 |
39 |
39 |
|
| 453 |
0 |
▶ | if (id >= 0 && A->bus_unsubscribe) (void)A->bus_unsubscribe(aTHX_ id); |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 454 |
0 |
| SvREFCNT_dec(held); |
| 455 |
0 |
| return -1; |
| 456 |
|
| } |
| 457 |
39 |
▶ | if (PUNK_BUS_CACHE_CB[id]) SvREFCNT_dec(PUNK_BUS_CACHE_CB[id]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (PUNK_BUS_CACHE_CB[id]) SvREFCNT_dec(PUNK_BUS_CACHE_CB[id]); |
0 |
39 |
39 |
|
| 458 |
39 |
| PUNK_BUS_CACHE_CB[id] = held; |
| 459 |
39 |
| return id; |
| 460 |
|
| } |
| 461 |
|
| |
| 462 |
|
| #endif /* PUNK_BUS_H */ |