Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(context): http.CloseNotify is deprecated, use context.Done instead #3994

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,10 +1137,9 @@ func (c *Context) SSEvent(name string, message any) {
// indicates "Is client disconnected in middle of stream"
func (c *Context) Stream(step func(w io.Writer) bool) bool {
w := c.Writer
clientGone := w.CloseNotify()
for {
select {
case <-clientGone:
case <-c.Request.Context().Done():
return true
default:
keepOpen := step(w)
Expand Down
5 changes: 4 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,7 @@ func CreateTestResponseRecorder() *TestResponseRecorder {
func TestContextStream(t *testing.T) {
w := CreateTestResponseRecorder()
c, _ := CreateTestContext(w)
c.Request, _ = http.NewRequest(http.MethodGet, "", nil)

stopStream := true
c.Stream(func(w io.Writer) bool {
Expand All @@ -2550,10 +2551,12 @@ func TestContextStream(t *testing.T) {
func TestContextStreamWithClientGone(t *testing.T) {
w := CreateTestResponseRecorder()
c, _ := CreateTestContext(w)
done, cancel := context.WithCancel(context.Background())
c.Request, _ = http.NewRequestWithContext(done, http.MethodGet, "", nil)

c.Stream(func(writer io.Writer) bool {
defer func() {
w.closeClient()
cancel()
}()

_, err := writer.Write([]byte("test"))
Expand Down