From 2a984abce6abee26f91292ad33336e782ff5cf0a Mon Sep 17 00:00:00 2001 From: awais-ur-rehman Date: Wed, 4 Nov 2026 08:54:59 +0500 Subject: [PATCH] fix(render): stop after channel timeout --- responder.go | 17 ++++++---- responder_channel_test.go | 70 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 responder_channel_test.go diff --git a/responder.go b/responder.go index f38807d..2a7bf1f 100644 --- a/responder.go +++ b/responder.go @@ -43,7 +43,12 @@ func DefaultResponder(w http.ResponseWriter, r *http.Request, v interface{}) { channelEventStream(w, r, v) return default: - v = channelIntoSlice(w, r, v) + items, ok := channelIntoSlice(w, r, v) + if !ok { + http.Error(w, "Server Timeout", http.StatusGatewayTimeout) + return + } + v = items } } } @@ -198,8 +203,9 @@ func channelEventStream(w http.ResponseWriter, r *http.Request, v interface{}) { } } -// channelIntoSlice buffers channel data into a slice. -func channelIntoSlice(w http.ResponseWriter, r *http.Request, from interface{}) interface{} { +// channelIntoSlice buffers channel data into a slice. It reports false if +// the request context cancels the collection. +func channelIntoSlice(w http.ResponseWriter, r *http.Request, from interface{}) ([]interface{}, bool) { ctx := r.Context() var to []interface{} @@ -209,12 +215,11 @@ func channelIntoSlice(w http.ResponseWriter, r *http.Request, from interface{}) {Dir: reflect.SelectRecv, Chan: reflect.ValueOf(from)}, }); chosen { case 0: // equivalent to: case <-ctx.Done() - http.Error(w, "Server Timeout", http.StatusGatewayTimeout) - return nil + return nil, false default: // equivalent to: case v, ok := <-stream if !ok { - return to + return to, true } v := recv.Interface() diff --git a/responder_channel_test.go b/responder_channel_test.go new file mode 100644 index 0000000..1f47ea4 --- /dev/null +++ b/responder_channel_test.go @@ -0,0 +1,70 @@ +package render_test + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/render" +) + +func TestDefaultResponderChannelTimeout(t *testing.T) { + for _, accept := range []string{"application/json", "application/xml"} { + t.Run(accept, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx) + req.Header.Set("Accept", accept) + render.Status(req, http.StatusAccepted) + rec := httptest.NewRecorder() + + render.DefaultResponder(rec, req, make(chan string)) + + resp := rec.Result() + defer resp.Body.Close() + if resp.StatusCode != http.StatusGatewayTimeout { + t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusGatewayTimeout) + } + if got := resp.Header.Get("Content-Type"); got != "text/plain; charset=utf-8" { + t.Errorf("Content-Type = %q, want text/plain; charset=utf-8", got) + } + if got := rec.Body.String(); got != "Server Timeout\n" { + t.Errorf("body = %q, want %q", got, "Server Timeout\n") + } + }) + } +} + +func TestDefaultResponderCompletedChannel(t *testing.T) { + for _, tt := range []struct { + name string + values []string + body string + }{ + {name: "empty", body: "null\n"}, + {name: "buffered", values: []string{"ready", "done"}, body: "[\"ready\",\"done\"]\n"}, + } { + t.Run(tt.name, func(t *testing.T) { + stream := make(chan string, len(tt.values)) + for _, value := range tt.values { + stream <- value + } + close(stream) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set("Accept", "application/json") + rec := httptest.NewRecorder() + + render.DefaultResponder(rec, req, stream) + + resp := rec.Result() + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusOK) + } + if got := rec.Body.String(); got != tt.body { + t.Errorf("body = %q, want %q", got, tt.body) + } + }) + } +}