diff options
author | 2019-11-17 02:02:46 +0000 | |
---|---|---|
committer | 2019-11-16 18:02:46 -0800 | |
commit | e14e053d3df3e607dc0e9fd7bb43d64eaf581162 (patch) | |
tree | 3d5772eb9cdef71199a2cb38ed76e2e662a10d44 /plugin | |
parent | 4831e7f9477dcdc7847b0a76fab09fcd17447bb2 (diff) | |
download | coredns-e14e053d3df3e607dc0e9fd7bb43d64eaf581162.tar.gz coredns-e14e053d3df3e607dc0e9fd7bb43d64eaf581162.tar.zst coredns-e14e053d3df3e607dc0e9fd7bb43d64eaf581162.zip |
create pkg/reuseport (#3455)
* create pkg/reuseport
Move the core server listening functions to a new package so plugins can
use them.
Also make *all* servers use the functions here; as only the udp/tcp
listeners where using SO_REUSEPORT (if available). This is the only
actual change in this PR; in it's core it's just a move of 2 files.
This can also be used to cleanup the dance we're doing now for
re-acquiring the sockets in e.g. the metrics plugins and the ready
plugin.
Signed-off-by: Miek Gieben <miek@miek.nl>
* Also push a small doc update
Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/pkg/reuseport/listen_go111.go | 37 | ||||
-rw-r--r-- | plugin/pkg/reuseport/listen_go_not111.go | 13 |
2 files changed, 50 insertions, 0 deletions
diff --git a/plugin/pkg/reuseport/listen_go111.go b/plugin/pkg/reuseport/listen_go111.go new file mode 100644 index 000000000..fa6f365d6 --- /dev/null +++ b/plugin/pkg/reuseport/listen_go111.go @@ -0,0 +1,37 @@ +// +build go1.11 +// +build aix darwin dragonfly freebsd linux netbsd openbsd + +package reuseport + +import ( + "context" + "net" + "syscall" + + "github.com/coredns/coredns/plugin/pkg/log" + + "golang.org/x/sys/unix" +) + +func control(network, address string, c syscall.RawConn) error { + c.Control(func(fd uintptr) { + if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil { + log.Warningf("Failed to set SO_REUSEPORT on socket: %s", err) + } + }) + return nil +} + +// Listen announces on the local network address. See net.Listen for more information. +// If SO_REUSEPORT is available it will be set on the socket. +func Listen(network, addr string) (net.Listener, error) { + lc := net.ListenConfig{Control: control} + return lc.Listen(context.Background(), network, addr) +} + +// ListenPacket announces on the local network address. See net.ListenPacket for more information. +// If SO_REUSEPORT is available it will be set on the socket. +func ListenPacket(network, addr string) (net.PacketConn, error) { + lc := net.ListenConfig{Control: control} + return lc.ListenPacket(context.Background(), network, addr) +} diff --git a/plugin/pkg/reuseport/listen_go_not111.go b/plugin/pkg/reuseport/listen_go_not111.go new file mode 100644 index 000000000..e3bdfb906 --- /dev/null +++ b/plugin/pkg/reuseport/listen_go_not111.go @@ -0,0 +1,13 @@ +// +build !go1.11 !aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd + +package reuseport + +import "net" + +// Listen is a wrapper around net.Listen. +func Listen(network, addr string) (net.Listener, error) { return net.Listen(network, addr) } + +// ListenPacket is a wrapper around net.ListenPacket. +func ListenPacket(network, addr string) (net.PacketConn, error) { + return net.ListenPacket(network, addr) +} |