From 6cd956fa512bd0b0e1e92afd596f38a75fe4159c Mon Sep 17 00:00:00 2001 From: a <a@tuxpa.in> Date: Tue, 3 Sep 2024 14:37:46 -0500 Subject: [PATCH] noot --- contrib/codecs/http/codecs.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/contrib/codecs/http/codecs.go b/contrib/codecs/http/codecs.go index 97d6fb3..ec9e01a 100644 --- a/contrib/codecs/http/codecs.go +++ b/contrib/codecs/http/codecs.go @@ -4,9 +4,11 @@ import ( "context" "encoding/base64" "errors" + "fmt" "io" "net/http" "net/url" + "runtime/debug" "strings" "gfx.cafe/open/jrpc/pkg/jsonrpc" @@ -96,7 +98,7 @@ func NewPostCodec(w http.ResponseWriter, r *http.Request) (*HttpCodec, error) { } c.ctx, c.cn = context.WithCancel(r.Context()) flusher, ok := w.(http.Flusher) - if ok { + if ok && flusher != nil { c.f = flusher } @@ -137,19 +139,33 @@ func (c *HttpCodec) Write(p []byte) (n int, err error) { } func (c *HttpCodec) Flush() error { - c.w.Write([]byte{'\n'}) - if c.f != nil { - c.f.Flush() + _, err := c.w.Write([]byte{'\n'}) + if err != nil { + return err } + c._flush() return nil } func (c *HttpCodec) Close() error { + select { + case <-c.ctx.Done(): + default: + c._flush() + c.cn() + } + return nil +} + +func (c *HttpCodec) _flush() { + defer func() { + if err := recover(); err != nil { + fmt.Println("err", err, "stack: \n"+string(debug.Stack())) + } + }() if c.f != nil { c.f.Flush() } - c.cn() - return nil } // Closed returns a channel which is closed when the connection is closed. -- GitLab