Skip to content

Commit

Permalink
Allow SO_REUSEPORT
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Aug 5, 2024
1 parent eb878c2 commit 551ff32
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
9 changes: 5 additions & 4 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_go//go:def.bzl", "go_binary", "go_library")
load("@gazelle//:def.bzl", "gazelle")
load("@rules_go//go:def.bzl", "go_binary", "go_library")

gazelle(name = "gazelle")

Expand All @@ -11,6 +11,9 @@ go_library(
],
importpath = "aws-in-a-box",
visibility = ["//visibility:private"],
x_defs = {
"aws-in-a-box.BazelSuffix": " (Bazel)",
},
deps = [
"//arn",
"//http",
Expand All @@ -20,10 +23,8 @@ go_library(
"//services/kms",
"//services/s3",
"//services/sqs",
"@org_golang_x_sys//unix",
],
x_defs = {
"aws-in-a-box.BazelSuffix": " (Bazel)"
},
)

go_binary(
Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ use_repo(
"com_github_google_go_cmp",
"org_golang_x_exp",
"org_golang_x_net",
"org_golang_x_sys",
)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/sys v0.18.0
golang.org/x/text v0.14.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjs
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
31 changes: 29 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package main

import (
"context"
"flag"
"log"
"log/slog"
"net"
"os"
"runtime/debug"
"strings"
"syscall"
"time"

"golang.org/x/sys/unix"

"aws-in-a-box/arn"
"aws-in-a-box/http"
"aws-in-a-box/server"
Expand Down Expand Up @@ -42,6 +47,7 @@ func versionString() string {

func main() {
addr := flag.String("addr", "localhost:4569", "Address to run on")
reusePort := flag.Bool("reuse-port", false, "If true, sets the SO_REUSEPORT socket option")
persistDir := flag.String("persistDir", "", "Directory to persist data to. If empty, data is not persisted.")
logLevel := flag.String("logLevel", "debug", "debug/info/warn/error")

Expand Down Expand Up @@ -172,9 +178,30 @@ func main() {
}

srv := server.NewWithHandlerChain(handlerChain...)
srv.Addr = *addr

err := srv.ListenAndServe()
lc := net.ListenConfig{
Control: func(network, address string, conn syscall.RawConn) error {
if !*reusePort {
return nil
}

var setSockoptErr error
err := conn.Control(func(fd uintptr) {
setSockoptErr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1)
})
if err != nil {
return err
}
return setSockoptErr
},
}

l, err := lc.Listen(context.Background(), "tcp", *addr)
if err != nil {
log.Fatal(err)
}

err = srv.Serve(l)
if err != nil {
panic(err)
}
Expand Down
1 change: 1 addition & 0 deletions services/s3/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ go_library(
"//atomicfile",
"//awserrors",
"@com_github_gofrs_uuid_v5//:uuid",
"@org_golang_x_exp//maps",
],
)

0 comments on commit 551ff32

Please sign in to comment.