perf(markers): batch SSE flushes + serve /js/ from embed.FS#125
Merged
Conversation
streamMarkersHandler called flusher.Flush() once per marker, producing
~70k chunked-write/syscalls per viewport request. Wrap the response in a
64KB bufio.Writer and flush every 256 markers or 50ms instead. Also send
X-Accel-Buffering: no so nginx forwards chunks immediately.
/js/ was being served via http.Dir("public_html/"), a relative path that
resolves against the runtime cwd. In production (binary in /usr/local/bin)
no such dir exists next to it, so /js/marker-worker.js 404'd and JSON
parsing of the 12MB SSE stream fell back to the main thread. Serve /js/
from the same embedded StaticFS used by /static/ instead.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
streamMarkersHandlerflushed once per marker (~70k flushes per viewport request). Wrap the response in a 64KBbufio.Writerand flush every 256 markers or 50ms. Also sendX-Accel-Buffering: noso nginx stops buffering the SSE stream./js/was served viahttp.Dir("public_html/")— a relative path resolved against runtime cwd. In production the binary lives in/usr/local/bin, nopublic_html/next to it, so/js/marker-worker.js404'd and marker JSON parsing fell back to the main thread. Serve/js/from the embeddedStaticFSinstead.Why
WebPagetest comparison (simplemap.safecast.org vs pelora.org, same viewport): simplemap took ~29s for
/stream_markersvs pelora's ~7.5s for a similar payload, plus a reproducible404 /js/marker-worker.js. Both come from this code path.Test plan
go build -tags duckdb -o safecast-new-map ./cmd/unified-server/go vet -tags duckdb ./cmd/unified-server/ ./pkg/httpapi/go test -tags duckdb ./pkg/httpapi/ -run TestRegisterStaticRoutesstream_markerswall-clock drops and/js/marker-worker.jsreturns 200🤖 Generated with Claude Code