Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #83 from libp2p/fix/noisy-log
Browse files Browse the repository at this point in the history
fix: avoid logging "invalid argument" errors when setting keepalive
  • Loading branch information
Stebalien committed Jul 14, 2021
2 parents 1b96803 + c22be72 commit f27215f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tcp.go
Expand Up @@ -2,7 +2,9 @@ package tcp

import (
"context"
"errors"
"net"
"os"
"runtime"
"time"

Expand Down Expand Up @@ -39,13 +41,22 @@ func tryKeepAlive(conn net.Conn, keepAlive bool) {
return
}
if err := keepAliveConn.SetKeepAlive(keepAlive); err != nil {
log.Errorf("Failed to enable TCP keepalive: %s", err)
// Sometimes we seem to get "invalid argument" results from this function on Darwin.
// This might be due to a closed connection, but I can't reproduce that on Linux.
//
// But there's nothing we can do about invalid arguments, so we'll drop this to a
// debug.
if errors.Is(err, os.ErrInvalid) {
log.Debugw("failed to enable TCP keepalive", "error", err)
} else {
log.Errorw("failed to enable TCP keepalive", "error", err)
}
return
}

if runtime.GOOS != "openbsd" {
if err := keepAliveConn.SetKeepAlivePeriod(keepAlivePeriod); err != nil {
log.Errorf("Failed set keepalive period: %s", err)
log.Errorw("failed set keepalive period", "error", err)
}
}
}
Expand Down

0 comments on commit f27215f

Please sign in to comment.