Skip to content

Commit

Permalink
fix: enable SO_REUSEPORT in listener config
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Feb 6, 2025
1 parent 8078cdc commit 4a32d00
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cmd/serve_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"net/http"
"sync"
"syscall"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -105,7 +106,23 @@ func serve(ctx context.Context) {
}
}()

if err := httpSrv.ListenAndServe(); err != http.ErrServerClosed {
lc := net.ListenConfig{
Control: func(network, address string, c syscall.RawConn) error {
var serr error
if err := c.Control(func(fd uintptr) {
// hard-coded syscall.SO_REUSEPORT since it doesn't seem to be defined in different environments
serr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, 0x200, 1)
}); err != nil {
return err
}
return serr
},
}
listener, err := lc.Listen(ctx, "tcp", addr)
if err != nil {
log.WithError(err).Fatal("http server listen failed")
}
if err := httpSrv.Serve(listener); err != nil {
log.WithError(err).Fatal("http server serve failed")
}
}

0 comments on commit 4a32d00

Please sign in to comment.