go-reuseport

repository·master·Indexed 21 days ago

https://github.com/libp2p/go-reuseport

A utility package for Go that enables TCP and UDP address reuse by setting SO_REUSEADDR and SO_REUSEPORT socket options. It allows a single process to both listen on and dial from the same port, which is a requirement for TCP NAT holepunching. The package provides functions such as reuse.Listen, reuse.ListenPacket, reuse.Dial, and reuse.DialTimeout, as well as an Available() function to check for OS support.

Tokens
841
Snippets
4
Records
8
Agent score
24%

What's inside go-reuseport

  1. Use go-reuseport to enable address reuse

    master
    The go-reuseport package allows a process to both listen on and dial from the same TCP or UDP port by enabling SO_REUSEADDR and SO_REUSEPORT socket options. This is essential for scenarios like TCP NAT holepunching, where a single process must maintain a listener and a dialer on the same local port.
  2. Listen on the same port multiple times

    master

    You can use reuse.Listen to create multiple listeners on the exact same network address and port. This is useful for distributing incoming connections across multiple listener instances.

    // 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")
  3. Dial from a specific listening port

    master

    You can use reuse.Dial to initiate a connection from a specific local port that is already being used by a listener.

    Important Note: You cannot dial yourself (connecting to the same port you are listening on) because TCP/IP stacks use 4-tuples to identify connections, and doing so would cause a clash.

    // 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")
  4. ListenPacket for packet-oriented networks with port reuse

    master
    Use ListenPacket to create a net.PacketConn (for protocols like UDP) that has SO_REUSEPORT and SO_REUSEADDR options set. This allows multiple packet-based listeners to share the same address and port.
  5. Dial from a specific local address with port reuse

    master

    Use Dial or DialTimeout to establish a connection where the local source port is explicitly set. This allows you to dial from the same local port used by other connections, provided the remote address/port (the other half of the 4-tuple) is different.

    Note: You cannot dial your own listening address because TCP/IP stacks use 4-tuples to identify connections, and doing so would cause a clash.

    Example of dialing from a specific local port:

    l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
    l2, _ := reuse.Listen("tcp", "127.0.0.1:1235")
    // Dial from local address 127.0.0.1:1234 to remote address 127.0.0.1:1235
    c, _ := reuse.Dial("tcp", "127.0.0.1:1234", "127.0.0.1:1235")
    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")
  6. Listen on a network address with port reuse

    master

    Use Listen to create a net.Listener that allows multiple processes or listeners to bind to the same port by setting the SO_REUSEPORT and SO_REUSEADDR socket options. This is useful for high-availability services or load balancing at the OS level.

    Example of listening on the same port twice:

    l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
    l2, _ := reuse.Listen("tcp", "127.0.0.1:1234")