From e875e816e42b8d7f53e25299eff1e83fbc3c0698 Mon Sep 17 00:00:00 2001 From: KevRiver Date: Sat, 2 Nov 2024 19:10:35 +0900 Subject: [PATCH] make cross-platform EADDRINUSE check --- cmd/humanlog/localhost.go | 4 ++-- internal/errors/errors_unix.go | 13 +++++++++++++ internal/errors/errors_windows.go | 13 +++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 internal/errors/errors_unix.go create mode 100644 internal/errors/errors_windows.go diff --git a/cmd/humanlog/localhost.go b/cmd/humanlog/localhost.go index 6ef3a36f..7e005edd 100644 --- a/cmd/humanlog/localhost.go +++ b/cmd/humanlog/localhost.go @@ -29,9 +29,9 @@ import ( "github.com/rs/cors" "golang.org/x/net/http2" "golang.org/x/net/http2/h2c" - "golang.org/x/sys/unix" // imported for side-effect + internal_errors "github.com/humanlogio/humanlog/internal/errors" _ "github.com/humanlogio/humanlog/internal/memstorage" ) @@ -51,7 +51,7 @@ func isEADDRINUSE(err error) bool { if !ok { return false } - return nserrno == unix.EADDRINUSE + return internal_errors.IsSocketInUse(nserrno) } func startLocalhostServer( diff --git a/internal/errors/errors_unix.go b/internal/errors/errors_unix.go new file mode 100644 index 00000000..8d641e5c --- /dev/null +++ b/internal/errors/errors_unix.go @@ -0,0 +1,13 @@ +//go:build !windows + +package errors + +import ( + "syscall" + + "golang.org/x/sys/unix" +) + +func IsSocketInUse(errno syscall.Errno) bool { + return errno == unix.EADDRINUSE +} diff --git a/internal/errors/errors_windows.go b/internal/errors/errors_windows.go new file mode 100644 index 00000000..da90b928 --- /dev/null +++ b/internal/errors/errors_windows.go @@ -0,0 +1,13 @@ +//go:build windows + +package errors + +import ( + "syscall" + + "golang.org/x/sys/windows" +) + +func IsSocketInUse(errno syscall.Errno) bool { + return errno == windows.WSAEADDRINUSE +}