Skip to content

Commit 13c2bd9

Browse files
committed
chore(context): http.CloseNotify is deprecated, use context.Done instead
Signed-off-by: 0xff-dev <[email protected]>
1 parent 64ead9e commit 13c2bd9

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

context.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,10 +1137,9 @@ func (c *Context) SSEvent(name string, message any) {
11371137
// indicates "Is client disconnected in middle of stream"
11381138
func (c *Context) Stream(step func(w io.Writer) bool) bool {
11391139
w := c.Writer
1140-
clientGone := w.CloseNotify()
11411140
for {
11421141
select {
1143-
case <-clientGone:
1142+
case <-c.Request.Context().Done():
11441143
return true
11451144
default:
11461145
keepOpen := step(w)

context_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2531,6 +2531,7 @@ func CreateTestResponseRecorder() *TestResponseRecorder {
25312531
func TestContextStream(t *testing.T) {
25322532
w := CreateTestResponseRecorder()
25332533
c, _ := CreateTestContext(w)
2534+
c.Request, _ = http.NewRequest(http.MethodGet, "", nil)
25342535

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

25542557
c.Stream(func(writer io.Writer) bool {
25552558
defer func() {
2556-
w.closeClient()
2559+
cancel()
25572560
}()
25582561

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

0 commit comments

Comments
 (0)