diff --git a/app/internal/sockopts/sockopts_linux.go b/app/internal/sockopts/sockopts_linux.go index e51e75a..ce188d9 100644 --- a/app/internal/sockopts/sockopts_linux.go +++ b/app/internal/sockopts/sockopts_linux.go @@ -7,6 +7,8 @@ import ( "net" "time" + "golang.org/x/exp/constraints" + "golang.org/x/sys/unix" ) @@ -58,7 +60,11 @@ func fdControlUnixSocketImpl(c *net.UDPConn, path string) error { } defer unix.Close(socketFd) - timeout := unixTimeval() + var timeout unix.Timeval + timeUsec := fdControlUnixTimeout.Microseconds() + castAssignInteger(timeUsec/1e6, &timeout.Sec) + // Specifying the type explicitly is not necessary here, but it makes GoLand happy. + castAssignInteger[int64](timeUsec%1e6, &timeout.Usec) _ = unix.SetsockoptTimeval(socketFd, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &timeout) _ = unix.SetsockoptTimeval(socketFd, unix.SOL_SOCKET, unix.SO_SNDTIMEO, &timeout) @@ -85,3 +91,7 @@ func fdControlUnixSocketImpl(c *net.UDPConn, path string) error { return nil }) } + +func castAssignInteger[F, T constraints.Integer](from F, to *T) { + *to = T(from) +} diff --git a/app/internal/sockopts/timeval_linux_32.go b/app/internal/sockopts/timeval_linux_32.go deleted file mode 100644 index af8c262..0000000 --- a/app/internal/sockopts/timeval_linux_32.go +++ /dev/null @@ -1,15 +0,0 @@ -//go:build linux && (386 || arm || mips || mipsle || ppc) - -package sockopts - -import ( - "golang.org/x/sys/unix" -) - -func unixTimeval() unix.Timeval { - timeUsec := fdControlUnixTimeout.Microseconds() - return unix.Timeval{ - Sec: int32(timeUsec / 1e6), - Usec: int32(timeUsec % 1e6), - } -} diff --git a/app/internal/sockopts/timeval_linux_64.go b/app/internal/sockopts/timeval_linux_64.go deleted file mode 100644 index 407438c..0000000 --- a/app/internal/sockopts/timeval_linux_64.go +++ /dev/null @@ -1,15 +0,0 @@ -//go:build linux && (amd64 || arm64 || loong64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x || sparc64) - -package sockopts - -import ( - "golang.org/x/sys/unix" -) - -func unixTimeval() unix.Timeval { - timeUsec := fdControlUnixTimeout.Microseconds() - return unix.Timeval{ - Sec: timeUsec / 1e6, - Usec: timeUsec % 1e6, - } -}