Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: only enable SO_REUSEPORT on Linux and FreeBSD #580

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"context"
"io"
"net"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -463,6 +464,23 @@
options.WriteBufferCap = math.CeilToPowerOfTwo(wbc)
}

// SO_REUSEPORT enables duplicate address and port bindings across various
// Unix-like OSs, whereas there is platform-specific inconsistency:
// Linux implemented SO_REUSEPORT with load balancing for incoming connections
// while *BSD implemented it for only binding to the same address and port, which
// makes it pointless to enable SO_REUSEPORT on *BSD and Darwin for gnet with
// multiple event-loops because only the first event-loop will be constantly woken
// up to accept incoming connections and handle I/O events while the rest of event
// loops remain idle.
// Thus, we disable SO_REUSEPORT on *BSD and Darwin by default.
//
// Note that FreeBSD 12 introduced a new socket option named SO_REUSEPORT_LB
// with the capability of load balancing, it's the equivalent of Linux's SO_REUSEPORT.
goos := runtime.GOOS
if (options.Multicore || options.NumEventLoop > 1) && options.ReusePort && goos != "linux" && goos != "freebsd" {
options.ReusePort = false

Check warning on line 481 in gnet.go

View check run for this annotation

Codecov / codecov/patch

gnet.go#L481

Added line #L481 was not covered by tests
}

// If there is UDP listener in the list, enable SO_REUSEPORT and disable edge-triggered I/O by default.
for i := 0; (!options.ReusePort || options.EdgeTriggeredIO) && i < len(addrs); i++ {
proto, _, err := parseProtoAddr(addrs[i])
Expand Down
29 changes: 29 additions & 0 deletions internal/socket/sockopts_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 The Gnet Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package socket

import (
"os"

"golang.org/x/sys/unix"
)

// SetReuseport enables SO_REUSEPORT_LB option on socket.
func SetReuseport(fd, reusePort int) error {
return os.NewSyscallError("setsockopt", unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT_LB, reusePort))
}
5 changes: 0 additions & 5 deletions internal/socket/sockopts_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ func SetSendBuffer(fd, size int) error {
return unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_SNDBUF, size)
}

// SetReuseport enables SO_REUSEPORT option on socket.
func SetReuseport(fd, reusePort int) error {
return os.NewSyscallError("setsockopt", unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, reusePort))
}

// SetReuseAddr enables SO_REUSEADDR option on socket.
func SetReuseAddr(fd, reuseAddr int) error {
return os.NewSyscallError("setsockopt", unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, reuseAddr))
Expand Down
29 changes: 29 additions & 0 deletions internal/socket/sockopts_unix1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2021 The Gnet Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || dragonfly || netbsd || openbsd || darwin
// +build linux dragonfly netbsd openbsd darwin

package socket

import (
"os"

"golang.org/x/sys/unix"
)

// SetReuseport enables SO_REUSEPORT option on socket.
func SetReuseport(fd, reusePort int) error {
return os.NewSyscallError("setsockopt", unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, reusePort))
}
Loading