Skip to content

Commit

Permalink
chore(context): http.CloseNotify is deprecated, use context.Done instead
Browse files Browse the repository at this point in the history
Signed-off-by: 0xff-dev <stevenshuang521@gmail.com>
  • Loading branch information
0xff-dev committed Jun 16, 2024
1 parent 64ead9e commit 13c2bd9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
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

0 comments on commit 13c2bd9

Please sign in to comment.