Skip to content

Commit

Permalink
cmd/seb http-server: add configurable limit for concurrent http clients
Browse files Browse the repository at this point in the history
  • Loading branch information
micvbang committed Jul 8, 2024
1 parent 724686d commit 7d6d764
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
21 changes: 17 additions & 4 deletions cmd/seb/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package app
import (
"context"
"fmt"
"net"
"net/http"
"os"
"path"
"runtime"
"time"

"github.com/aws/aws-sdk-go-v2/config"
Expand All @@ -16,6 +18,7 @@ import (
"github.com/micvbang/simple-event-broker/internal/sebbroker"
"github.com/micvbang/simple-event-broker/internal/sebcache"
"github.com/spf13/cobra"
"golang.org/x/net/netutil"
)

var serveFlags ServeFlags
Expand All @@ -29,6 +32,7 @@ func init() {
fs.StringVar(&serveFlags.httpListenAddress, "http-address", "127.0.0.1", "Address to listen for HTTP traffic")
fs.IntVar(&serveFlags.httpListenPort, "http-port", 51313, "Port to listen for HTTP traffic")
fs.StringVar(&serveFlags.httpAPIKey, "http-api-key", "api-key", "API key for authorizing HTTP requests (this is not safe and needs to be changed)")
fs.IntVar(&serveFlags.httpConnectionsMax, "http-connections", runtime.NumCPU()*64, "Maximum number of concurrent incoming HTTP connections to be handled")

// http debug
fs.BoolVar(&serveFlags.httpEnableDebug, "http-debug-enable", false, "Whether to enable DEBUG endpoints")
Expand Down Expand Up @@ -82,7 +86,15 @@ var serveCmd = &cobra.Command{
go func() {
addr := fmt.Sprintf("%s:%d", flags.httpListenAddress, flags.httpListenPort)
log.Infof("Listening on %s", addr)
errs <- http.ListenAndServe(addr, mux)

l, err := net.Listen("tcp", addr)
if err != nil {
errs <- fmt.Errorf("listening on %s: %w", addr, err)
}
defer l.Close()

l = netutil.LimitListener(l, flags.httpConnectionsMax)
errs <- http.Serve(l, mux)
}()

if flags.httpEnableDebug {
Expand Down Expand Up @@ -120,9 +132,10 @@ type ServeFlags struct {

s3BucketName string

httpListenAddress string
httpListenPort int
httpAPIKey string
httpListenAddress string
httpListenPort int
httpConnectionsMax int
httpAPIKey string

httpEnableDebug bool
httpDebugListenAddress string
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.27.0
)

require (
Expand All @@ -33,6 +34,6 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/sys v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/micvbang/go-helpy v0.1.20 h1:VcagiZCSEYFw/MuDojNwfNlPIjQooh0xl7AIIN0p9OU=
github.com/micvbang/go-helpy v0.1.20/go.mod h1:9JyNGzneXfG1D3KFGfYXZ4woZa9SgqY3sM0NFOfAMYM=
github.com/micvbang/go-helpy v0.1.23 h1:S8nNnFxE5SLDeIQhHlqORBP5oLhkACpOyfw11h00Z/Y=
github.com/micvbang/go-helpy v0.1.23/go.mod h1:gtP/AempujwEhkS03IQJNbpPyyIXU882l6mI9bhmkGo=
github.com/micvbang/go-helpy v0.1.24 h1:OgePYzKwefftuiimoM91Gp1tl9V4HsRLQVtxMRdLEhQ=
github.com/micvbang/go-helpy v0.1.24/go.mod h1:gtP/AempujwEhkS03IQJNbpPyyIXU882l6mI9bhmkGo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -61,8 +57,11 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down

0 comments on commit 7d6d764

Please sign in to comment.