Skip to content

Commit

Permalink
Handle net.Error Temporary() deprecation
Browse files Browse the repository at this point in the history
See https://go.dev/issue/45729.

- Remove hideTempError

  The hideTempError function was used to prevent connection methods from
  returning a net.Error with Temporary() == true.

  Connection methods do not support retry after returning any net.Error,
  including errors with Temporary() == true.

  Presumably hideTempError was used to prevent applications from
  futilely retrying connection method calls on temporary errors.

  The hideTempError function is not needed now that net.Error
  Temporary() is deprecated.

- Remove use of the deprecated net.Error Temporary() method in the
  default ping handler by ignoring all errors returned from
  WriteControl. Ignoring errors allows the application to continue
  reading messages after a write error and is consistent with how the
  default close handler calls WriteControl.
  • Loading branch information
tebuka authored and jaitaiwan committed Jul 2, 2024
1 parent 682b25f commit a62d9d2
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ func newMaskKey() [4]byte {
return k
}

func hideTempErr(err error) error {
if e, ok := err.(net.Error); ok && e.Temporary() {
err = &netError{msg: e.Error(), timeout: e.Timeout()}
}
return err
}

func isControl(frameType int) bool {
return frameType == CloseMessage || frameType == PingMessage || frameType == PongMessage
}
Expand Down Expand Up @@ -364,7 +357,6 @@ func (c *Conn) RemoteAddr() net.Addr {
// Write methods

func (c *Conn) writeFatal(err error) error {
err = hideTempErr(err)
c.writeErrMu.Lock()
if c.writeErr == nil {
c.writeErr = err
Expand Down Expand Up @@ -1014,7 +1006,7 @@ func (c *Conn) NextReader() (messageType int, r io.Reader, err error) {
for c.readErr == nil {
frameType, err := c.advanceFrame()
if err != nil {
c.readErr = hideTempErr(err)
c.readErr = err
break
}

Expand Down Expand Up @@ -1054,7 +1046,7 @@ func (r *messageReader) Read(b []byte) (int, error) {
b = b[:c.readRemaining]
}
n, err := c.br.Read(b)
c.readErr = hideTempErr(err)
c.readErr = err
if c.isServer {
c.readMaskPos = maskBytes(c.readMaskKey, c.readMaskPos, b[:n])
}
Expand All @@ -1075,7 +1067,7 @@ func (r *messageReader) Read(b []byte) (int, error) {
frameType, err := c.advanceFrame()
switch {
case err != nil:
c.readErr = hideTempErr(err)
c.readErr = err
case frameType == TextMessage || frameType == BinaryMessage:
c.readErr = errors.New("websocket: internal error, unexpected text or binary in Reader")
}
Expand Down Expand Up @@ -1164,13 +1156,9 @@ func (c *Conn) PingHandler() func(appData string) error {
func (c *Conn) SetPingHandler(h func(appData string) error) {
if h == nil {
h = func(message string) error {
err := c.WriteControl(PongMessage, []byte(message), time.Now().Add(writeWait))
if err == ErrCloseSent {
return nil
} else if e, ok := err.(net.Error); ok && e.Temporary() {
return nil
}
return err
// Make a best effort to send the pong mesage.
_ = c.WriteControl(PongMessage, []byte(message), time.Now().Add(writeWait))
return nil
}
}
c.handlePing = h
Expand Down

0 comments on commit a62d9d2

Please sign in to comment.