Add SO_REUSEPORT support for EntryPoints

This commit is contained in:
Aofei Sheng 2024-01-30 21:56:05 +08:00 committed by GitHub
parent 40de310927
commit d02be003ab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 279 additions and 43 deletions

View file

@ -1,7 +1,9 @@
package udp
import (
"context"
"errors"
"fmt"
"io"
"net"
"sync"
@ -33,18 +35,22 @@ type Listener struct {
}
// Listen creates a new listener.
func Listen(network string, laddr *net.UDPAddr, timeout time.Duration) (*Listener, error) {
func Listen(listenConfig net.ListenConfig, network, address string, timeout time.Duration) (*Listener, error) {
if timeout <= 0 {
return nil, errors.New("timeout should be greater than zero")
}
conn, err := net.ListenUDP(network, laddr)
packetConn, err := listenConfig.ListenPacket(context.Background(), network, address)
if err != nil {
return nil, err
return nil, fmt.Errorf("listen packet: %w", err)
}
pConn, ok := packetConn.(*net.UDPConn)
if !ok {
return nil, errors.New("packet conn is not an UDPConn")
}
l := &Listener{
pConn: conn,
pConn: pConn,
acceptCh: make(chan *Conn),
conns: make(map[string]*Conn),
accepting: true,