mirror of
https://github.com/binwiederhier/ntfy.git
synced 2026-11-10 14:44:04 +08:00
Do not set TCP_USER_TIMEOUT on Unix sockets
The option only exists for TCP, so every connection over a socket (host=/var/run/postgresql) failed with "operation not supported". Claude-Session: https://claude.ai/code/session_01DeXuhSjq5Gta5qPJgSceFe
This commit is contained in:
1 parent
f6483d536a
commit
f7d106a73c
2 files changed
+19
No files matched your search
@@ -2,6 +2,7 @@ package pg
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
@@ -14,6 +15,9 @@ import (
|
|||||||
func newDialer() *net.Dialer {
|
func newDialer() *net.Dialer {
|
||||||
return &net.Dialer{
|
return &net.Dialer{
|
||||||
Control: func(network, address string, c syscall.RawConn) error {
|
Control: func(network, address string, c syscall.RawConn) error {
|
||||||
|
if !strings.HasPrefix(network, "tcp") {
|
||||||
|
return nil // Unix sockets (host=/var/run/postgresql) have no TCP_USER_TIMEOUT
|
||||||
|
}
|
||||||
var sockErr error
|
var sockErr error
|
||||||
err := c.Control(func(fd uintptr) {
|
err := c.Control(func(fd uintptr) {
|
||||||
sockErr = unix.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(tcpUserTimeout.Milliseconds()))
|
sockErr = unix.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(tcpUserTimeout.Milliseconds()))
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package pg
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -53,3 +54,17 @@ func TestParseConnConfig_DialsWithTCPUserTimeout(t *testing.T) {
|
|||||||
require.Nil(t, sockErr)
|
require.Nil(t, sockErr)
|
||||||
require.Equal(t, int(tcpUserTimeout.Milliseconds()), timeout)
|
require.Equal(t, int(tcpUserTimeout.Milliseconds()), timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseConnConfig_DialsUnixSocket(t *testing.T) {
|
||||||
|
// TCP_USER_TIMEOUT does not exist for Unix sockets (host=/var/run/postgresql), so the dialer
|
||||||
|
// must not set it there, or every connection fails with "operation not supported"
|
||||||
|
path := filepath.Join(t.TempDir(), ".s.PGSQL.5432")
|
||||||
|
listener, err := net.Listen("unix", path)
|
||||||
|
require.Nil(t, err)
|
||||||
|
defer listener.Close()
|
||||||
|
config, err := parseConnConfig("postgres://user:pass@/db?host=" + filepath.Dir(path))
|
||||||
|
require.Nil(t, err)
|
||||||
|
conn, err := config.DialFunc(context.Background(), "unix", path)
|
||||||
|
require.Nil(t, err)
|
||||||
|
require.Nil(t, conn.Close())
|
||||||
|
}
|
||||||
Reference in new issue
Block a user