mcp: use http.ResponseController to ensure writes are flushed (#870)
If the ResponseWriter is a wrapped one (which is quite common when using middleware to log things like response sizes), the direct cast to `http.Flusher` doesn't find the flush capability. Instead, use `http.ResponseController` as introduced in go1.20 which handles unwrapping to find the base response writer.
This commit is contained in:
parent
638d404e0d
commit
d3fd25b68a
1 changed files with 10 additions and 9 deletions
|
|
@ -41,7 +41,7 @@ func (e Event) Empty() bool {
|
|||
}
|
||||
|
||||
// writeEvent writes the event to w, and flushes.
|
||||
func writeEvent(w io.Writer, evt Event) (int, error) {
|
||||
func writeEvent(w http.ResponseWriter, evt Event) (int, error) {
|
||||
var b bytes.Buffer
|
||||
if evt.Name != "" {
|
||||
fmt.Fprintf(&b, "event: %s\n", evt.Name)
|
||||
|
|
@ -54,9 +54,9 @@ func writeEvent(w io.Writer, evt Event) (int, error) {
|
|||
}
|
||||
fmt.Fprintf(&b, "data: %s\n\n", string(evt.Data))
|
||||
n, err := w.Write(b.Bytes())
|
||||
if f, ok := w.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
rc := http.NewResponseController(w)
|
||||
// Ignore returned error as flushing is best-effort.
|
||||
_ = rc.Flush()
|
||||
return n, err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue