projects / polyhook
polyhook
One OpenAPI 3.1 webhook API, built twice: once in Go, once in Node, behind a single SvelteKit and Svelte 5 frontend.
- 10 rps Per-IP ingest limit, burst 30 Rate limiter config
Overview
Problem
Comparing Go and Node usually means comparing two different programs.
Approach
The same contract implemented twice, with SSE fan-out, cursor pagination, ETags, replay with backoff and a 24 hour sweeper in both. Only the base URL changes between backends.
Result
Same behaviour in two idioms, with the code for each concern side by side: a goroutine worker pool against a promise drain loop, sync.RWMutex against a callback set.
One frontend, two backends, one OpenAPI 3.1 contract: the comparison itself is the deliverable.
On the design
Decisions · why this stack
-
Contract first
One OpenAPI 3.1 spec, committed identically to both repos, so the same UI and EventSource code work against either runtime unmodified.
-
Non-blocking SSE fan-out
Each bin keeps its own subscriber set and publishes without blocking, so a slow client never stalls ingest.
-
Cursor pagination with ETags
A composite (received_at, id) cursor stays stable under concurrent writes, and If-None-Match makes polling cheap.
-
Replay with backoff and dead-letter
Failed deliveries retry after 1, 2, 4, 8 and 16 seconds, then land in a dead-letter queue.
-
Only measured numbers
Only line counts are on the page. Image size, latency and memory stay off it until a k6 run and a clean Docker build measure them.
Stack
- Go
- chi
- modernc.org/sqlite
- Node.js 22
- Fastify
- node:sqlite
- TypeScript
- OpenAPI 3.1
- SvelteKit 2
- Svelte 5
- Docker
- Railway
- Vercel
Demo
chi · log/slog · modernc.org/sqlite
POST /v1/binsbin created with an ingest URL and a 24 h expiryGET /v1/bins/:id/streamSSE feed open, resumable with Last-Event-ID per-bin subscriber map behind sync.RWMutexPOST /v1/bins/:id/inrequest captured, raw body preservedevent: requestpushed to every subscriber without blocking ingestGET /v1/bins/:id/requestscursor page on (received_at, id) with an ETagGET /v1/bins/:id/requests If-None-Matchnot modified, nothing resentPOST /v1/bins/:id/requests/:rid/replayreplay queued to the target URL 4-goroutine worker pool on a bounded channelGET /v1/replays/:idretrying after 1, 2, 4, 8 and 16 s, then dead-lettersweeperbins older than 24 h removed sweeper goroutine
Scripted from the shared OpenAPI 3.1 contract. The Railway backends are offline, so this is a walkthrough of the API, not live traffic.
Side-by-side: same problem, two languages
Per-bin subscriber set. Publish is non-blocking, so a slow client gets dropped events but cannot stall ingest. The single most important design choice in the project.
type broadcaster struct {
mu sync.RWMutex
subs map[string]map[chan sseEvent]struct{}
}
func (b *broadcaster) subscribe(binID string) (<-chan sseEvent, func()) {
ch := make(chan sseEvent, 16)
b.mu.Lock()
if _, ok := b.subs[binID]; !ok {
b.subs[binID] = make(map[chan sseEvent]struct{})
}
b.subs[binID][ch] = struct{}{}
b.mu.Unlock()
cleanup := func() {
b.mu.Lock()
delete(b.subs[binID], ch)
if len(b.subs[binID]) == 0 {
delete(b.subs, binID)
}
b.mu.Unlock()
close(ch)
}
return ch, cleanup
}
func (b *broadcaster) publish(binID string, ev sseEvent) {
b.mu.RLock()
defer b.mu.RUnlock()
for ch := range b.subs[binID] {
// Non-blocking send: slow subscribers must not stall ingest.
select {
case ch <- ev:
default:
}
}
}class Broadcaster {
private subs = new Map<string, Set<(e: SSEEvent) => void>>();
subscribe(binId: string, cb: (e: SSEEvent) => void): () => void {
let set = this.subs.get(binId);
if (!set) {
set = new Set();
this.subs.set(binId, set);
}
set.add(cb);
return () => {
const s = this.subs.get(binId);
if (!s) return;
s.delete(cb);
if (s.size === 0) this.subs.delete(binId);
};
}
publish(binId: string, ev: SSEEvent): void {
const set = this.subs.get(binId);
if (!set) return;
for (const cb of set) {
try {
cb(ev);
} catch {
// One bad subscriber must not stall ingest.
}
}
}
}Takeaway. Same shape, different idioms. Go uses a buffered channel + non-blocking select; Node uses a callback Set + try/catch. Both deliver bounded latency in the face of slow consumers without backpressure on the producer.