Merge pull request #10174 from dcantah/simplify-errs

Chore: Simplify some syscall error checks
This commit is contained in:
Maksym Pavlenko 2024-05-07 03:32:49 +00:00 committed by GitHub
commit c0e34fb7c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 26 deletions

View File

@ -19,9 +19,9 @@
package dialer
import (
"errors"
"fmt"
"net"
"os"
"strings"
"syscall"
"time"
@ -34,16 +34,7 @@ func DialAddress(address string) string {
}
func isNoent(err error) bool {
if err != nil {
if nerr, ok := err.(*net.OpError); ok {
if serr, ok := nerr.Err.(*os.SyscallError); ok {
if serr.Err == syscall.ENOENT {
return true
}
}
}
}
return false
return errors.Is(err, syscall.ENOENT)
}
func dialer(address string, timeout time.Duration) (net.Conn, error) {

View File

@ -22,6 +22,7 @@ import (
"bufio"
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
"math"
@ -173,22 +174,14 @@ func RemoveSocket(address string) error {
// SocketEaddrinuse returns true if the provided error is caused by the
// EADDRINUSE error number
func SocketEaddrinuse(err error) bool {
netErr, ok := err.(*net.OpError)
if !ok {
return false
var netErr *net.OpError
if errors.As(err, &netErr) {
if netErr.Op != "listen" {
return false
}
return errors.Is(err, syscall.EADDRINUSE)
}
if netErr.Op != "listen" {
return false
}
syscallErr, ok := netErr.Err.(*os.SyscallError)
if !ok {
return false
}
errno, ok := syscallErr.Err.(syscall.Errno)
if !ok {
return false
}
return errno == syscall.EADDRINUSE
return false
}
// CanConnect returns true if the socket provided at the address