mirror of https://github.com/tidwall/evio.git
Update dependencies
This commit is contained in:
parent
e7a23d23eb
commit
5e8d3032b8
|
@ -0,0 +1,5 @@
|
||||||
|
module github.com/tidwall/evio
|
||||||
|
|
||||||
|
go 1.15
|
||||||
|
|
||||||
|
require github.com/kavu/go_reuseport v1.5.0
|
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/kavu/go_reuseport v1.5.0 h1:UNuiY2OblcqAtVDE8Gsg1kZz8zbBWg907sP1ceBV+bk=
|
||||||
|
github.com/kavu/go_reuseport v1.5.0/go.mod h1:CG8Ee7ceMFSMnx/xr25Vm0qXaj2Z4i5PWoUx+JZ5/CU=
|
|
@ -1 +0,0 @@
|
||||||
// DO NOT REMOVE
|
|
|
@ -1,21 +0,0 @@
|
||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2014 Max Riveiro
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
|
@ -1,9 +0,0 @@
|
||||||
lint:
|
|
||||||
@gometalinter \
|
|
||||||
--disable=errcheck \
|
|
||||||
--disable=dupl \
|
|
||||||
--min-const-length=5 \
|
|
||||||
--min-confidence=0.25 \
|
|
||||||
--cyclo-over=20 \
|
|
||||||
--enable=unused \
|
|
||||||
--deadline=100s
|
|
|
@ -1,48 +0,0 @@
|
||||||
# GO_REUSEPORT
|
|
||||||
|
|
||||||
[![Build Status](https://travis-ci.org/kavu/go_reuseport.png?branch=master)](https://travis-ci.org/kavu/go_reuseport)
|
|
||||||
[![codecov](https://codecov.io/gh/kavu/go_reuseport/branch/master/graph/badge.svg)](https://codecov.io/gh/kavu/go_reuseport)
|
|
||||||
[![GoDoc](https://godoc.org/github.com/kavu/go_reuseport?status.png)](https://godoc.org/github.com/kavu/go_reuseport)
|
|
||||||
|
|
||||||
**GO_REUSEPORT** is a little expirement to create a `net.Listener` that supports [SO_REUSEPORT](http://lwn.net/Articles/542629/) socket option.
|
|
||||||
|
|
||||||
For now, Darwin and Linux (from 3.9) systems are supported. I'll be pleased if you'll test other systems and tell me the results.
|
|
||||||
documentation on [godoc.org](http://godoc.org/github.com/kavu/go_reuseport "go_reuseport documentation").
|
|
||||||
|
|
||||||
## Example ##
|
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"html"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
"github.com/kavu/go_reuseport"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
listener, err := reuseport.Listen("tcp", "localhost:8881")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer listener.Close()
|
|
||||||
|
|
||||||
server := &http.Server{}
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Println(os.Getgid())
|
|
||||||
fmt.Fprintf(w, "Hello, %q\n", html.EscapeString(r.URL.Path))
|
|
||||||
})
|
|
||||||
|
|
||||||
panic(server.Serve(listener))
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Now you can run several instances of this tiny server without `Address already in use` errors.
|
|
||||||
|
|
||||||
## Thanks
|
|
||||||
|
|
||||||
Inspired by [Artur Siekielski](https://github.com/aartur) [post](http://freeprogrammersblog.vhex.net/post/linux-39-introdued-new-way-of-writing-socket-servers/2) about `SO_REUSEPORT`.
|
|
||||||
|
|
|
@ -1,50 +0,0 @@
|
||||||
// +build linux darwin dragonfly freebsd netbsd openbsd
|
|
||||||
|
|
||||||
// Copyright (C) 2017 Max Riveiro
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
|
@ -1,44 +0,0 @@
|
||||||
// +build darwin dragonfly freebsd netbsd openbsd
|
|
||||||
|
|
||||||
// Copyright (C) 2017 Ma Weiwei, Max Riveiro
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
|
@ -1,52 +0,0 @@
|
||||||
// +build linux
|
|
||||||
|
|
||||||
// Copyright (C) 2017 Ma Weiwei, Max Riveiro
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
package reuseport
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
var reusePort = 0x0F
|
|
||||||
|
|
||||||
func maxListenerBacklog() int {
|
|
||||||
fd, err := os.Open("/proc/sys/net/core/somaxconn")
|
|
||||||
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])
|
|
||||||
if err != nil || n == 0 {
|
|
||||||
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 n
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
// +build windows
|
|
||||||
|
|
||||||
// Copyright (C) 2017 Ma Weiwei, Max Riveiro
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
package reuseport
|
|
||||||
|
|
||||||
import "net"
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
|
@ -1,143 +0,0 @@
|
||||||
// +build linux darwin dragonfly freebsd netbsd openbsd
|
|
||||||
|
|
||||||
// Copyright (C) 2017 Max Riveiro
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
package reuseport
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
listenerBacklogMaxSize = maxListenerBacklog()
|
|
||||||
errUnsupportedTCPProtocol = errors.New("only tcp, tcp4, tcp6 are supported")
|
|
||||||
)
|
|
||||||
|
|
||||||
func getTCPSockaddr(proto, addr string) (sa syscall.Sockaddr, soType int, err error) {
|
|
||||||
var tcp *net.TCPAddr
|
|
||||||
|
|
||||||
tcp, err = net.ResolveTCPAddr(proto, addr)
|
|
||||||
if err != nil && tcp.IP != nil {
|
|
||||||
return nil, -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tcpVersion, err := determineTCPProto(proto, tcp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch tcpVersion {
|
|
||||||
case "tcp":
|
|
||||||
return &syscall.SockaddrInet4{Port: tcp.Port}, syscall.AF_INET, nil
|
|
||||||
case "tcp4":
|
|
||||||
sa := &syscall.SockaddrInet4{Port: tcp.Port}
|
|
||||||
|
|
||||||
if tcp.IP != nil {
|
|
||||||
copy(sa.Addr[:], tcp.IP[12:16]) // copy last 4 bytes of slice to array
|
|
||||||
}
|
|
||||||
|
|
||||||
return sa, syscall.AF_INET, nil
|
|
||||||
case "tcp6":
|
|
||||||
sa := &syscall.SockaddrInet6{Port: tcp.Port}
|
|
||||||
|
|
||||||
if tcp.IP != nil {
|
|
||||||
copy(sa.Addr[:], tcp.IP) // copy all bytes of slice to array
|
|
||||||
}
|
|
||||||
|
|
||||||
if tcp.Zone != "" {
|
|
||||||
iface, err := net.InterfaceByName(tcp.Zone)
|
|
||||||
if err != nil {
|
|
||||||
return nil, -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
sa.ZoneId = uint32(iface.Index)
|
|
||||||
}
|
|
||||||
|
|
||||||
return sa, syscall.AF_INET6, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, -1, errUnsupportedProtocol
|
|
||||||
}
|
|
||||||
|
|
||||||
func determineTCPProto(proto string, ip *net.TCPAddr) (string, error) {
|
|
||||||
// If the protocol is set to "tcp", we try to determine the actual protocol
|
|
||||||
// version from the size of the resolved IP address. Otherwise, we simple use
|
|
||||||
// the protcol given to us by the caller.
|
|
||||||
|
|
||||||
if ip.IP.To4() != nil {
|
|
||||||
return "tcp4", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if ip.IP.To16() != nil {
|
|
||||||
return "tcp6", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
switch proto {
|
|
||||||
case "tcp", "tcp4", "tcp6":
|
|
||||||
return proto, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", errUnsupportedTCPProtocol
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewReusablePortListener returns net.FileListener that created from
|
|
||||||
// a file discriptor for a socket with SO_REUSEPORT option.
|
|
||||||
func NewReusablePortListener(proto, addr string) (l net.Listener, err error) {
|
|
||||||
var (
|
|
||||||
soType, fd int
|
|
||||||
file *os.File
|
|
||||||
sockaddr syscall.Sockaddr
|
|
||||||
)
|
|
||||||
|
|
||||||
if sockaddr, soType, err = getSockaddr(proto, addr); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
syscall.ForkLock.RLock()
|
|
||||||
if fd, err = syscall.Socket(soType, syscall.SOCK_STREAM, syscall.IPPROTO_TCP); err != nil {
|
|
||||||
syscall.ForkLock.RUnlock()
|
|
||||||
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
syscall.ForkLock.RUnlock()
|
|
||||||
|
|
||||||
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
|
|
||||||
syscall.Close(fd)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, reusePort, 1); err != nil {
|
|
||||||
syscall.Close(fd)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = syscall.Bind(fd, sockaddr); err != nil {
|
|
||||||
syscall.Close(fd)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set backlog size to the maximum
|
|
||||||
if err = syscall.Listen(fd, listenerBacklogMaxSize); err != nil {
|
|
||||||
syscall.Close(fd)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file = os.NewFile(uintptr(fd), getSocketFileName(proto, addr))
|
|
||||||
if l, err = net.FileListener(file); err != nil {
|
|
||||||
file.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = file.Close(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return l, err
|
|
||||||
}
|
|
|
@ -1,218 +0,0 @@
|
||||||
// +build linux darwin dragonfly freebsd netbsd openbsd
|
|
||||||
|
|
||||||
// Copyright (C) 2017 Max Riveiro
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
package reuseport
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"html"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
httpServerOneResponse = "1"
|
|
||||||
httpServerTwoResponse = "2"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
httpServerOne = NewHTTPServer(httpServerOneResponse)
|
|
||||||
httpServerTwo = NewHTTPServer(httpServerTwoResponse)
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewHTTPServer(resp string) *httptest.Server {
|
|
||||||
return httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Fprint(w, resp)
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
func TestNewReusablePortListener(t *testing.T) {
|
|
||||||
listenerOne, err := NewReusablePortListener("tcp4", "localhost:10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerOne.Close()
|
|
||||||
|
|
||||||
listenerTwo, err := NewReusablePortListener("tcp", "127.0.0.1:10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerTwo.Close()
|
|
||||||
|
|
||||||
listenerThree, err := NewReusablePortListener("tcp6", "[::1]:10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerThree.Close()
|
|
||||||
|
|
||||||
listenerFour, err := NewReusablePortListener("tcp6", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerFour.Close()
|
|
||||||
|
|
||||||
listenerFive, err := NewReusablePortListener("tcp4", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerFive.Close()
|
|
||||||
|
|
||||||
listenerSix, err := NewReusablePortListener("tcp", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerSix.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestListen(t *testing.T) {
|
|
||||||
listenerOne, err := Listen("tcp4", "localhost:10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerOne.Close()
|
|
||||||
|
|
||||||
listenerTwo, err := Listen("tcp", "127.0.0.1:10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerTwo.Close()
|
|
||||||
|
|
||||||
listenerThree, err := Listen("tcp6", "[::1]:10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerThree.Close()
|
|
||||||
|
|
||||||
listenerFour, err := Listen("tcp6", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerFour.Close()
|
|
||||||
|
|
||||||
listenerFive, err := Listen("tcp4", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerFive.Close()
|
|
||||||
|
|
||||||
listenerSix, err := Listen("tcp", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerSix.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewReusablePortServers(t *testing.T) {
|
|
||||||
listenerOne, err := NewReusablePortListener("tcp4", "localhost:10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerOne.Close()
|
|
||||||
|
|
||||||
listenerTwo, err := NewReusablePortListener("tcp6", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerTwo.Close()
|
|
||||||
|
|
||||||
httpServerOne.Listener = listenerOne
|
|
||||||
httpServerTwo.Listener = listenerTwo
|
|
||||||
|
|
||||||
httpServerOne.Start()
|
|
||||||
httpServerTwo.Start()
|
|
||||||
|
|
||||||
// Server One — First Response
|
|
||||||
resp1, err := http.Get(httpServerOne.URL)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
body1, err := ioutil.ReadAll(resp1.Body)
|
|
||||||
resp1.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if string(body1) != httpServerOneResponse && string(body1) != httpServerTwoResponse {
|
|
||||||
t.Errorf("Expected %#v or %#v, got %#v.", httpServerOneResponse, httpServerTwoResponse, string(body1))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server Two — First Response
|
|
||||||
resp2, err := http.Get(httpServerTwo.URL)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
body2, err := ioutil.ReadAll(resp2.Body)
|
|
||||||
resp1.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if string(body2) != httpServerOneResponse && string(body2) != httpServerTwoResponse {
|
|
||||||
t.Errorf("Expected %#v or %#v, got %#v.", httpServerOneResponse, httpServerTwoResponse, string(body2))
|
|
||||||
}
|
|
||||||
|
|
||||||
httpServerTwo.Close()
|
|
||||||
|
|
||||||
// Server One — Second Response
|
|
||||||
resp3, err := http.Get(httpServerOne.URL)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
body3, err := ioutil.ReadAll(resp3.Body)
|
|
||||||
resp1.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if string(body3) != httpServerOneResponse {
|
|
||||||
t.Errorf("Expected %#v, got %#v.", httpServerOneResponse, string(body3))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server One — Third Response
|
|
||||||
resp5, err := http.Get(httpServerOne.URL)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
body5, err := ioutil.ReadAll(resp5.Body)
|
|
||||||
resp1.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if string(body5) != httpServerOneResponse {
|
|
||||||
t.Errorf("Expected %#v, got %#v.", httpServerOneResponse, string(body5))
|
|
||||||
}
|
|
||||||
|
|
||||||
httpServerOne.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkNewReusablePortListener(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
listener, err := NewReusablePortListener("tcp", ":10081")
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
} else {
|
|
||||||
listener.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleNewReusablePortListener() {
|
|
||||||
listener, err := NewReusablePortListener("tcp", ":8881")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer listener.Close()
|
|
||||||
|
|
||||||
server := &http.Server{}
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Println(os.Getgid())
|
|
||||||
fmt.Fprintf(w, "Hello, %q\n", html.EscapeString(r.URL.Path))
|
|
||||||
})
|
|
||||||
|
|
||||||
panic(server.Serve(listener))
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Thanks to IPFS team
|
|
||||||
if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
|
|
||||||
if [[ "$TRAVIS_SUDO" == true ]]; then
|
|
||||||
# Ensure that IPv6 is enabled.
|
|
||||||
# While this is unsupported by TravisCI, it still works for localhost.
|
|
||||||
sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=0
|
|
||||||
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=0
|
|
||||||
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
# OSX has a default file limit of 256, for some tests we need a
|
|
||||||
# maximum of 8192.
|
|
||||||
ulimit -Sn 8192
|
|
||||||
fi
|
|
||||||
|
|
||||||
go test -v -cover ./...
|
|
||||||
go test -v -cover -race ./... -coverprofile=coverage.txt -covermode=atomic
|
|
||||||
go test -v -cover -race -benchmem -benchtime=5s -bench=.
|
|
|
@ -1,139 +0,0 @@
|
||||||
// +build linux darwin dragonfly freebsd netbsd openbsd
|
|
||||||
|
|
||||||
// Copyright (C) 2017 Max Riveiro
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
package reuseport
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
var errUnsupportedUDPProtocol = errors.New("only udp, udp4, udp6 are supported")
|
|
||||||
|
|
||||||
func getUDPSockaddr(proto, addr string) (sa syscall.Sockaddr, soType int, err error) {
|
|
||||||
var udp *net.UDPAddr
|
|
||||||
|
|
||||||
udp, err = net.ResolveUDPAddr(proto, addr)
|
|
||||||
if err != nil && udp.IP != nil {
|
|
||||||
return nil, -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
udpVersion, err := determineUDPProto(proto, udp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch udpVersion {
|
|
||||||
case "udp":
|
|
||||||
return &syscall.SockaddrInet4{Port: udp.Port}, syscall.AF_INET, nil
|
|
||||||
case "udp4":
|
|
||||||
sa := &syscall.SockaddrInet4{Port: udp.Port}
|
|
||||||
|
|
||||||
if udp.IP != nil {
|
|
||||||
copy(sa.Addr[:], udp.IP[12:16]) // copy last 4 bytes of slice to array
|
|
||||||
}
|
|
||||||
|
|
||||||
return sa, syscall.AF_INET, nil
|
|
||||||
case "udp6":
|
|
||||||
sa := &syscall.SockaddrInet6{Port: udp.Port}
|
|
||||||
|
|
||||||
if udp.IP != nil {
|
|
||||||
copy(sa.Addr[:], udp.IP) // copy all bytes of slice to array
|
|
||||||
}
|
|
||||||
|
|
||||||
if udp.Zone != "" {
|
|
||||||
iface, err := net.InterfaceByName(udp.Zone)
|
|
||||||
if err != nil {
|
|
||||||
return nil, -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
sa.ZoneId = uint32(iface.Index)
|
|
||||||
}
|
|
||||||
|
|
||||||
return sa, syscall.AF_INET6, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, -1, errUnsupportedProtocol
|
|
||||||
}
|
|
||||||
|
|
||||||
func determineUDPProto(proto string, ip *net.UDPAddr) (string, error) {
|
|
||||||
// If the protocol is set to "udp", we try to determine the actual protocol
|
|
||||||
// version from the size of the resolved IP address. Otherwise, we simple use
|
|
||||||
// the protcol given to us by the caller.
|
|
||||||
|
|
||||||
if ip.IP.To4() != nil {
|
|
||||||
return "udp4", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if ip.IP.To16() != nil {
|
|
||||||
return "udp6", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
switch proto {
|
|
||||||
case "udp", "udp4", "udp6":
|
|
||||||
return proto, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", errUnsupportedUDPProtocol
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewReusablePortPacketConn returns net.FilePacketConn that created from
|
|
||||||
// a file discriptor for a socket with SO_REUSEPORT option.
|
|
||||||
func NewReusablePortPacketConn(proto, addr string) (l net.PacketConn, err error) {
|
|
||||||
var (
|
|
||||||
soType, fd int
|
|
||||||
file *os.File
|
|
||||||
sockaddr syscall.Sockaddr
|
|
||||||
)
|
|
||||||
|
|
||||||
if sockaddr, soType, err = getSockaddr(proto, addr); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
syscall.ForkLock.RLock()
|
|
||||||
fd, err = syscall.Socket(soType, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
|
|
||||||
if err == nil {
|
|
||||||
syscall.CloseOnExec(fd)
|
|
||||||
}
|
|
||||||
syscall.ForkLock.RUnlock()
|
|
||||||
if err != nil {
|
|
||||||
syscall.Close(fd)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if err != nil {
|
|
||||||
syscall.Close(fd)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, reusePort, 1); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = syscall.Bind(fd, sockaddr); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file = os.NewFile(uintptr(fd), getSocketFileName(proto, addr))
|
|
||||||
if l, err = net.FilePacketConn(file); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = file.Close(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return l, err
|
|
||||||
}
|
|
|
@ -1,99 +0,0 @@
|
||||||
// +build linux darwin dragonfly freebsd netbsd openbsd
|
|
||||||
|
|
||||||
// Copyright (C) 2017 Max Riveiro
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
package reuseport
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestNewReusablePortPacketConn(t *testing.T) {
|
|
||||||
listenerOne, err := NewReusablePortPacketConn("udp4", "localhost:10082")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerOne.Close()
|
|
||||||
|
|
||||||
listenerTwo, err := NewReusablePortPacketConn("udp", "127.0.0.1:10082")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerTwo.Close()
|
|
||||||
|
|
||||||
listenerThree, err := NewReusablePortPacketConn("udp6", "[::1]:10082")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerThree.Close()
|
|
||||||
|
|
||||||
listenerFour, err := NewReusablePortListener("udp6", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerFour.Close()
|
|
||||||
|
|
||||||
listenerFive, err := NewReusablePortListener("udp4", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerFive.Close()
|
|
||||||
|
|
||||||
listenerSix, err := NewReusablePortListener("udp", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerSix.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestListenPacket(t *testing.T) {
|
|
||||||
listenerOne, err := ListenPacket("udp4", "localhost:10082")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerOne.Close()
|
|
||||||
|
|
||||||
listenerTwo, err := ListenPacket("udp", "127.0.0.1:10082")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerTwo.Close()
|
|
||||||
|
|
||||||
listenerThree, err := ListenPacket("udp6", "[::1]:10082")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerThree.Close()
|
|
||||||
|
|
||||||
listenerFour, err := ListenPacket("udp6", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerFour.Close()
|
|
||||||
|
|
||||||
listenerFive, err := ListenPacket("udp4", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerFive.Close()
|
|
||||||
|
|
||||||
listenerSix, err := ListenPacket("udp", ":10081")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
defer listenerSix.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkNewReusableUDPPortListener(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
listener, err := NewReusablePortPacketConn("udp4", "localhost:10082")
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
} else {
|
|
||||||
listener.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue