Skip to content

Commit a474b80

Browse files
authored
fix: enable SO_REUSEPORT in listener config (#1936)
## What kind of change does this PR introduce? * Enables `SO_REUSEPORT` which allows multiple sockets to bind to the same address and port - this is useful when the auth service needs to be restarted and the port is still being held by a reverse proxy (i.e. envoy) until all the connections are drained
1 parent bc57c1c commit a474b80

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

cmd/serve_cmd.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net"
66
"net/http"
77
"sync"
8+
"syscall"
89
"time"
910

1011
"github.com/pkg/errors"
@@ -105,7 +106,23 @@ func serve(ctx context.Context) {
105106
}
106107
}()
107108

108-
if err := httpSrv.ListenAndServe(); err != http.ErrServerClosed {
109+
lc := net.ListenConfig{
110+
Control: func(network, address string, c syscall.RawConn) error {
111+
var serr error
112+
if err := c.Control(func(fd uintptr) {
113+
// hard-coded syscall.SO_REUSEPORT since it doesn't seem to be defined in different environments
114+
serr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, 0x200, 1)
115+
}); err != nil {
116+
return err
117+
}
118+
return serr
119+
},
120+
}
121+
listener, err := lc.Listen(ctx, "tcp", addr)
122+
if err != nil {
109123
log.WithError(err).Fatal("http server listen failed")
110124
}
125+
if err := httpSrv.Serve(listener); err != nil {
126+
log.WithError(err).Fatal("http server serve failed")
127+
}
111128
}

0 commit comments

Comments
 (0)