Skip to content

Commit

Permalink
Remove go_reuseport module dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Leoyoungxh committed Aug 10, 2023
1 parent c160257 commit 0c0e04d
Show file tree
Hide file tree
Showing 20 changed files with 953 additions and 18 deletions.
2 changes: 1 addition & 1 deletion admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"strings"
"sync"

reuseport "trpc.group/trpc-go/go_reuseport"
"trpc.group/trpc-go/trpc-go/internal/reuseport"
trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc"

"trpc.group/trpc-go/trpc-go/config"
Expand Down
9 changes: 2 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ require (
golang.org/x/sys v0.4.0
google.golang.org/protobuf v1.30.0
gopkg.in/yaml.v3 v3.0.1
trpc.group/trpc-go/go_reuseport v1.7.1-0.20230423021710-f5eeff5d87a3
trpc.group/trpc-go/tnet v0.0.12-0.20230423031524-5eb1cc42f225
trpc.group/trpc/trpc-protocol/pb/go/trpc v0.1.2-0.20230530025122-c44533fe44bd
trpc.group/trpc-go/tnet v0.0.0-20230810071536-9d05338021cf
trpc.group/trpc/trpc-protocol/pb/go/trpc v0.0.0-20230803031059-de4168eb5952
)

require (
Expand All @@ -46,7 +45,3 @@ require (
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/text v0.6.0 // indirect
)

// The hash of current code of v0.11.0 does not match with
// the hash stored in sumdb.
retract v0.11.0
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
trpc.group/trpc-go/go_reuseport v1.7.1-0.20230423021710-f5eeff5d87a3 h1:FN4gLJ2xxueQ/LYPxfrAwq6hmdHJaAftFZm8qvqI63o=
trpc.group/trpc-go/go_reuseport v1.7.1-0.20230423021710-f5eeff5d87a3/go.mod h1:9yQwBnMw+3ceJd5MPqP18t6y/EAi7fEh59MiFcqeS78=
trpc.group/trpc-go/tnet v0.0.12-0.20230423031524-5eb1cc42f225 h1:sExJeNiDhhQ7dP6P0afYZ+HU1MnnMYhlnDApSmCLu9U=
trpc.group/trpc-go/tnet v0.0.12-0.20230423031524-5eb1cc42f225/go.mod h1:YUGAw+mutXH1ILO6UzoZYeT7G/9l5xmEDXmp/CvaP/E=
trpc.group/trpc/trpc-protocol/pb/go/trpc v0.1.2-0.20230530025122-c44533fe44bd h1:mFEoX5jBSSy/4a8HSGWreuGVaoj8fxc7eDgROTrMc7A=
trpc.group/trpc/trpc-protocol/pb/go/trpc v0.1.2-0.20230530025122-c44533fe44bd/go.mod h1:K+a1K/Gnlcg9BFHWx30vLBIEDhxODhl25gi1JjA54CQ=
trpc.group/trpc-go/tnet v0.0.0-20230810071536-9d05338021cf h1:Qo0p6ZJV60Qd5XajiIDidVgx1NDM9UHL7DzDKc2gqns=
trpc.group/trpc-go/tnet v0.0.0-20230810071536-9d05338021cf/go.mod h1:s/webUFYWEFBHErKyFmj7LYC7XfC2LTLCcwfSnJ04M0=
trpc.group/trpc/trpc-protocol/pb/go/trpc v0.0.0-20230803031059-de4168eb5952 h1:AhjP72IKa1YKnSIayk1X5xSzKrem0EanjZ7oMc2HYOw=
trpc.group/trpc/trpc-protocol/pb/go/trpc v0.0.0-20230803031059-de4168eb5952/go.mod h1:K+a1K/Gnlcg9BFHWx30vLBIEDhxODhl25gi1JjA54CQ=
2 changes: 1 addition & 1 deletion http/restful_server_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"

"github.com/valyala/fasthttp"
reuseport "trpc.group/trpc-go/go_reuseport"
"trpc.group/trpc-go/trpc-go/internal/reuseport"
trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc"

"trpc.group/trpc-go/trpc-go/codec"
Expand Down
2 changes: 1 addition & 1 deletion http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
reuseport "trpc.group/trpc-go/go_reuseport"
"trpc.group/trpc-go/trpc-go/internal/reuseport"
trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc"

"trpc.group/trpc-go/trpc-go/codec"
Expand Down
45 changes: 45 additions & 0 deletions internal/reuseport/reuseport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd
// +build linux darwin dragonfly freebsd netbsd openbsd

// Package reuseport provides a function that returns a net.Listener powered
// by a net.FileListener with a SO_REUSEPORT option set to the socket.
package reuseport

import (
"errors"
"fmt"
"net"
"os"
"syscall"
)

const fileNameTemplate = "reuseport.%d.%s.%s"

var errUnsupportedProtocol = errors.New("only tcp, tcp4, tcp6, udp, udp4, udp6 are supported")

// getSockaddr parses protocol and address and returns implementor
// of syscall.Sockaddr: syscall.SockaddrInet4 or syscall.SockaddrInet6.
func getSockaddr(proto, addr string) (sa syscall.Sockaddr, soType int, err error) {
switch proto {
case "tcp", "tcp4", "tcp6":
return getTCPSockaddr(proto, addr)
case "udp", "udp4", "udp6":
return getUDPSockaddr(proto, addr)
default:
return nil, -1, errUnsupportedProtocol
}
}

func getSocketFileName(proto, addr string) string {
return fmt.Sprintf(fileNameTemplate, os.Getpid(), proto, addr)
}

// Listen function is an alias for NewReusablePortListener.
func Listen(proto, addr string) (l net.Listener, err error) {
return NewReusablePortListener(proto, addr)
}

// ListenPacket is an alias for NewReusablePortPacketConn.
func ListenPacket(proto, addr string) (l net.PacketConn, err error) {
return NewReusablePortPacketConn(proto, addr)
}
44 changes: 44 additions & 0 deletions internal/reuseport/reuseport_bsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package reuseport

import (
"runtime"
"syscall"
)

var reusePort = syscall.SO_REUSEPORT

func maxListenerBacklog() int {
var (
n uint32
err error
)

switch runtime.GOOS {
case "darwin", "freebsd":
n, err = syscall.SysctlUint32("kern.ipc.somaxconn")
case "netbsd":
// NOTE: NetBSD has no somaxconn-like kernel state so far
case "openbsd":
n, err = syscall.SysctlUint32("kern.somaxconn")
default:
}

return defaultBacklog(n, err)
}

func defaultBacklog(n uint32, err error) int {
if n == 0 || err != nil {
return syscall.SOMAXCONN
}

// FreeBSD stores the backlog in a uint16, as does Linux.
// Assume the other BSDs do too. Truncate number to avoid wrapping.
// See issue 5030.
if n > 1<<16-1 {
n = 1<<16 - 1
}
return int(n)
}
51 changes: 51 additions & 0 deletions internal/reuseport/reuseport_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//go:build linux
// +build linux

package reuseport

import (
"bufio"
"os"
"strconv"
"strings"
"syscall"
)

var reusePort = 0x0F
var maxConnFileName = "/proc/sys/net/core/somaxconn"

func maxListenerBacklog() int {
fd, err := os.Open(maxConnFileName)
if err != nil {
return syscall.SOMAXCONN
}
defer fd.Close()

rd := bufio.NewReader(fd)
line, err := rd.ReadString('\n')
if err != nil {
return syscall.SOMAXCONN
}

f := strings.Fields(line)
if len(f) < 1 {
return syscall.SOMAXCONN
}

n, err := strconv.Atoi(f[0])
return defaultBacklog(uint32(n), err)
}

func defaultBacklog(n uint32, err error) int {
if n == 0 || err != nil {
return syscall.SOMAXCONN
}

// Linux stores the backlog in a uint16.
// Truncate number to avoid wrapping.
// See issue 5030.
if n > 1<<16-1 {
n = 1<<16 - 1
}
return int(n)
}
33 changes: 33 additions & 0 deletions internal/reuseport/reuseport_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build windows
// +build windows

package reuseport

import (
"net"
"syscall"
)

var ListenerBacklogMaxSize = maxListenerBacklog()

func maxListenerBacklog() int {
return syscall.SOMAXCONN
}

func NewReusablePortListener(proto, addr string) (net.Listener, error) {
return net.Listen(proto, addr)
}

func NewReusablePortPacketConn(proto, addr string) (net.PacketConn, error) {
return net.ListenPacket(proto, addr)
}

// Listen function is an alias for NewReusablePortListener.
func Listen(proto, addr string) (l net.Listener, err error) {
return NewReusablePortListener(proto, addr)
}

// ListenPacket is an alias for NewReusablePortPacketConn.
func ListenPacket(proto, addr string) (l net.PacketConn, err error) {
return NewReusablePortPacketConn(proto, addr)
}
Loading

0 comments on commit 0c0e04d

Please sign in to comment.