Skip to content

fix: add http.Flusher to metrics responseWriter for SSE streaming#247

Merged
umputun merged 1 commit intomasterfrom
fix/metrics-flusher-sse-streaming
Mar 3, 2026
Merged

fix: add http.Flusher to metrics responseWriter for SSE streaming#247
umputun merged 1 commit intomasterfrom
fix/metrics-flusher-sse-streaming

Conversation

@umputun
Copy link
Owner

@umputun umputun commented Mar 3, 2026

Metrics middleware wraps http.ResponseWriter to capture status codes, but the wrapper did not implement http.Flusher. This caused httputil.ReverseProxy to fall back to buffered mode — all SSE events were buffered until the backend response completed, then delivered in a single burst when MGMT_ENABLED=true.

Changes:

  • Add Flush() method to responseWriter in app/mgmt/metrics.go that delegates to the underlying writer, following the same pattern as the existing Hijack() method
  • Fix wrong error variable in app/main.go:303sizeParse error was wrapped with err (from a previous call) instead of perr
  • Add tests for flush delegation, no-op on non-flushable writers

metrics middleware wraps http.ResponseWriter to capture status codes,
but the wrapper did not implement http.Flusher. This caused
httputil.ReverseProxy to fall back to buffered mode, breaking SSE
streaming when MGMT_ENABLED=true.

Also fix wrong error variable in MaxSize parse error wrapping
(err → perr) in main.go.
Copilot AI review requested due to automatic review settings March 3, 2026 21:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes SSE streaming behavior when the management metrics middleware wraps http.ResponseWriter by ensuring the wrapper supports http.Flusher, and corrects an unrelated error-wrapping bug in run().

Changes:

  • Add Flush() to the metrics responseWriter wrapper (delegating to the underlying writer when supported).
  • Fix incorrect error variable used when wrapping sizeParse(opts.MaxSize) failures.
  • Add unit tests covering flush delegation and safe behavior for non-flushable writers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
app/mgmt/metrics.go Add Flush() to the responseWriter wrapper to preserve streaming behavior through middleware.
app/mgmt/server_test.go Add tests validating Flush() delegation and no-op behavior when flush isn’t supported.
app/main.go Fix error-wrapping to use perr from sizeParse instead of a stale err.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@umputun umputun merged commit 8cd52c8 into master Mar 3, 2026
8 checks passed
@umputun umputun deleted the fix/metrics-flusher-sse-streaming branch March 3, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants