@@ -244,7 +244,7 @@ func (c *http2Conn) Send(ctx context.Context, frame Frame) error {
244244 }
245245
246246 err := c .codec .EncodeTo (c .writer , frame )
247- if err != nil && isStreamClosedError (err ) {
247+ if err != nil && isHTTP2Error (err ) {
248248 return ErrConnClosed
249249 } else if err != nil {
250250 return fmt .Errorf ("write frame: %w" , err )
@@ -253,7 +253,7 @@ func (c *http2Conn) Send(ctx context.Context, frame Frame) error {
253253 // Flush after each frame to ensure immediate delivery
254254 if c .responseController != nil {
255255 err := c .responseController .Flush ()
256- if err != nil && isStreamClosedError (err ) {
256+ if err != nil && isHTTP2Error (err ) {
257257 return ErrConnClosed
258258 } else if err != nil {
259259 return fmt .Errorf ("flush response: %w" , err )
@@ -383,11 +383,19 @@ func (d *HTTP2Dialer) Dial(ctx context.Context, from, to net.Addr) (Conn, error)
383383 return conn , nil
384384}
385385
386- // isStreamClosedError returns true if err represents an `http2: stream closed`
387- // error.
388- func isStreamClosedError (err error ) bool {
386+ // https://github.com/golang/net/blob/master/http2/server.go#L68-L73
387+ var http2Errors = map [string ]struct {}{
388+ "http2: stream closed" : {},
389+ "body closed by handler" : {},
390+ "http2: request body closed due to handler exiting" : {},
391+ "client disconnected" : {},
392+ }
393+
394+ // isHTTP2Error returns true if err represents one of the known http2 errors that leads to a
395+ // broken connection.
396+ func isHTTP2Error (err error ) bool {
389397 // NOTE(rfratto): At the time of writing, the http2 package doesn't have a
390- // guaranteed way to check if an error is a stream closed error , so we look
398+ // guaranteed way to check its errors , so we look
391399 // at the error message instead.
392400 for {
393401 next := errors .Unwrap (err )
@@ -396,5 +404,6 @@ func isStreamClosedError(err error) bool {
396404 }
397405 err = next
398406 }
399- return err .Error () == "http2: stream closed"
407+ _ , res := http2Errors [err .Error ()]
408+ return res
400409}
0 commit comments