From 7d6d7645c020236c3b4366b47df7dfad5f778529 Mon Sep 17 00:00:00 2001 From: Michael Bang Date: Mon, 8 Jul 2024 15:34:19 +0200 Subject: [PATCH] cmd/seb http-server: add configurable limit for concurrent http clients --- cmd/seb/app/serve.go | 21 +++++++++++++++++---- go.mod | 3 ++- go.sum | 9 ++++----- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cmd/seb/app/serve.go b/cmd/seb/app/serve.go index 2e82970..50af280 100644 --- a/cmd/seb/app/serve.go +++ b/cmd/seb/app/serve.go @@ -3,9 +3,11 @@ package app import ( "context" "fmt" + "net" "net/http" "os" "path" + "runtime" "time" "github.com/aws/aws-sdk-go-v2/config" @@ -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 @@ -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") @@ -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 { @@ -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 diff --git a/go.mod b/go.mod index 6f0c75d..fb14231 100644 --- a/go.mod +++ b/go.mod @@ -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 ( @@ -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 ) diff --git a/go.sum b/go.sum index 622f026..8b00b83 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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=