README ¶
go-reuseport
This package enables listening and dialing from the same TCP or UDP port. This means that the following sockopts are set:
SO_REUSEADDR
SO_REUSEPORT
This is a simple package to get around the problem of reusing addresses.
The go net
package (to my knowledge) does not allow setting socket options.
This is particularly problematic when attempting to do TCP NAT holepunching,
which requires a process to both Listen and Dial on the same TCP port.
This package makes this possible for me. It is a pretty narrow use case, but
perhaps this package can grow to be more general over time.
Examples
// listen on the same port. oh yeah.
l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
l2, _ := reuse.Listen("tcp", "127.0.0.1:1234")
// dial from the same port. oh yeah.
l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
l2, _ := reuse.Listen("tcp", "127.0.0.1:1235")
c, _ := reuse.Dial("tcp", "127.0.0.1:1234", "127.0.0.1:1235")
Note: cant dial self because tcp/ip stacks use 4-tuples to identify connections, and doing so would clash.
Tested
Tested on darwin
and linux
.
Documentation ¶
Overview ¶
Package reuseport provides Listen and Dial functions that set socket options in order to be able to reuse ports. You should only use this package if you know what SO_REUSEADDR and SO_REUSEPORT are.
For example:
// listen on the same port. oh yeah. l1, _ := reuse.Listen("tcp", "127.0.0.1:1234") l2, _ := reuse.Listen("tcp", "127.0.0.1:1234") // dial from the same port. oh yeah. l1, _ := reuse.Listen("tcp", "127.0.0.1:1234") l2, _ := reuse.Listen("tcp", "127.0.0.1:1235") c, _ := reuse.Dial("tcp", "127.0.0.1:1234", "127.0.0.1:1235")
Note: cant dial self because tcp/ip stacks use 4-tuples to identify connections, and doing so would clash.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrReuseFailed = errors.New("reuse failed")
ErrReuseFailed is returned if a reuse attempt was unsuccessful.
var ErrUnsupportedProtocol = errors.New("protocol not yet supported")
ErrUnsuportedProtocol signals that the protocol is not currently supported by this package. This package currently only supports TCP.
Functions ¶
func Available ¶
func Available() bool
Available returns whether or not SO_REUSEPORT is available in the OS. It does so by attepting to open a tcp listener, setting the option, and checking ENOPROTOOPT on error. After checking, the decision is cached for the rest of the process run.
func Dial ¶
Dial dials the given network and address. see net.Dialer.Dial Returns a net.Conn created from a file discriptor for a socket with SO_REUSEPORT and SO_REUSEADDR option set.
func Listen ¶
Listen listens at the given network and address. see net.Listen Returns a net.Listener created from a file discriptor for a socket with SO_REUSEPORT and SO_REUSEADDR option set.
func ListenPacket ¶
func ListenPacket(network, address string) (net.PacketConn, error)
ListenPacket listens at the given network and address. see net.ListenPacket Returns a net.Listener created from a file discriptor for a socket with SO_REUSEPORT and SO_REUSEADDR option set.