Skip to content

Commit a87bea8

Browse files
authored
replace Flusher interface with http.ResponseController Flush method (#3773)
Signed-off-by: John Zhang <johnzhanghua@gmail.com>
1 parent da70933 commit a87bea8

17 files changed

Lines changed: 48 additions & 93 deletions

http/codegen/templates/server_handler_init.go.tpl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ func {{ .HandlerInit }}(
117117
errhandler(ctx, w, err)
118118
}
119119
} else {
120-
if f, ok := w.(http.Flusher); ok {
121-
f.Flush()
122-
}
120+
http.NewResponseController(w).Flush()
123121
panic(http.ErrAbortHandler) // too late to write an error
124122
}
125123
}
@@ -140,9 +138,7 @@ func {{ .HandlerInit }}(
140138
return
141139
}
142140
if _, err := io.Copy(w, buf); err != nil {
143-
if f, ok := w.(http.Flusher); ok {
144-
f.Flush()
145-
}
141+
http.NewResponseController(w).Flush()
146142
panic(http.ErrAbortHandler) // too late to write an error
147143
}
148144
{{- else }}
@@ -255,9 +251,7 @@ func {{ .HandlerInit }}(
255251
errhandler(ctx, w, err)
256252
}
257253
} else {
258-
if f, ok := w.(http.Flusher); ok {
259-
f.Flush()
260-
}
254+
http.NewResponseController(w).Flush()
261255
panic(http.ErrAbortHandler) // too late to write an error
262256
}
263257
}
@@ -284,9 +278,7 @@ func {{ .HandlerInit }}(
284278
{{- end }}
285279
{{- if .Method.SkipResponseBodyEncodeDecode }}
286280
if _, err := io.Copy(w, buf); err != nil {
287-
if f, ok := w.(http.Flusher); ok {
288-
f.Flush()
289-
}
281+
http.NewResponseController(w).Flush()
290282
panic(http.ErrAbortHandler) // too late to write an error
291283
}
292284
{{- end }}

http/codegen/templates/server_sse.go.tpl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (s *{{ .SSE.StructName }}) {{ .SSE.SendWithContextName }}(ctx context.Conte
3838
{{- else }}
3939
res := v
4040
{{- end }}
41-
41+
4242
{{ if .SSE.IDField }}
4343
if id := res.{{ .SSE.IDField }}; id != "" {
4444
fmt.Fprintf(s.w, "id: %s\n", id)
@@ -66,10 +66,8 @@ func (s *{{ .SSE.StructName }}) {{ .SSE.SendWithContextName }}(ctx context.Conte
6666
{{- end }}
6767
fmt.Fprintf(s.w, "data: %s\n\n", data)
6868

69-
if f, ok := s.w.(http.Flusher); ok {
70-
f.Flush()
71-
}
72-
return nil
69+
http.NewResponseController(s.w).Flush()
70+
return nil
7371
}
7472

7573
{{ comment "Close is a no-op for SSE. We keep the method for compatibility with other stream types." }}

http/codegen/testdata/golden/handler_skip response body encode decode.go.golden

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ func NewMethodSkipResponseBodyEncodeDecodeHandler(
4141
errhandler(ctx, w, err)
4242
}
4343
} else {
44-
if f, ok := w.(http.Flusher); ok {
45-
f.Flush()
46-
}
44+
http.NewResponseController(w).Flush()
4745
panic(http.ErrAbortHandler) // too late to write an error
4846
}
4947
}
@@ -64,9 +62,7 @@ func NewMethodSkipResponseBodyEncodeDecodeHandler(
6462
return
6563
}
6664
if _, err := io.Copy(w, buf); err != nil {
67-
if f, ok := w.(http.Flusher); ok {
68-
f.Flush()
69-
}
65+
http.NewResponseController(w).Flush()
7066
panic(http.ErrAbortHandler) // too late to write an error
7167
}
7268
})

http/codegen/testdata/golden/sse-all-fields.golden

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ func (s *SSEAllFieldsMethodServerStream) SendWithContext(ctx context.Context, v
5555
data = string(byts)
5656
fmt.Fprintf(s.w, "data: %s\n\n", data)
5757

58-
if f, ok := s.w.(http.Flusher); ok {
59-
f.Flush()
60-
}
58+
http.NewResponseController(s.w).Flush()
6159
return nil
6260
}
6361

http/codegen/testdata/golden/sse-bool.golden

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ func (s *SSEBoolMethodServerStream) SendWithContext(ctx context.Context, v bool)
4141
data = string(byts)
4242
fmt.Fprintf(s.w, "data: %s\n\n", data)
4343

44-
if f, ok := s.w.(http.Flusher); ok {
45-
f.Flush()
46-
}
44+
http.NewResponseController(s.w).Flush()
4745
return nil
4846
}
4947

http/codegen/testdata/golden/sse-data-field.golden

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ func (s *SSEDataFieldMethodServerStream) SendWithContext(ctx context.Context, v
4141
data = dataField
4242
fmt.Fprintf(s.w, "data: %s\n\n", data)
4343

44-
if f, ok := s.w.(http.Flusher); ok {
45-
f.Flush()
46-
}
44+
http.NewResponseController(s.w).Flush()
4745
return nil
4846
}
4947

http/codegen/testdata/golden/sse-data-id-field.golden

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ func (s *SSEDataIDFieldMethodServerStream) SendWithContext(ctx context.Context,
4545
data = dataField
4646
fmt.Fprintf(s.w, "data: %s\n\n", data)
4747

48-
if f, ok := s.w.(http.Flusher); ok {
49-
f.Flush()
50-
}
48+
http.NewResponseController(s.w).Flush()
5149
return nil
5250
}
5351

http/codegen/testdata/golden/sse-int.golden

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ func (s *SSEIntMethodServerStream) SendWithContext(ctx context.Context, v int) e
3737
data = fmt.Sprintf("%d", res)
3838
fmt.Fprintf(s.w, "data: %s\n\n", data)
3939

40-
if f, ok := s.w.(http.Flusher); ok {
41-
f.Flush()
42-
}
40+
http.NewResponseController(s.w).Flush()
4341
return nil
4442
}
4543

http/codegen/testdata/golden/sse-object.golden

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ func (s *SSEObjectMethodServerStream) SendWithContext(ctx context.Context, v *ss
4343
data = string(byts)
4444
fmt.Fprintf(s.w, "data: %s\n\n", data)
4545

46-
if f, ok := s.w.(http.Flusher); ok {
47-
f.Flush()
48-
}
46+
http.NewResponseController(s.w).Flush()
4947
return nil
5048
}
5149

http/codegen/testdata/golden/sse-request-id.golden

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ func (s *SSERequestIDMethodServerStream) SendWithContext(ctx context.Context, v
3838
data = res
3939
fmt.Fprintf(s.w, "data: %s\n\n", data)
4040

41-
if f, ok := s.w.(http.Flusher); ok {
42-
f.Flush()
43-
}
41+
http.NewResponseController(s.w).Flush()
4442
return nil
4543
}
4644

0 commit comments

Comments
 (0)