Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
Add new --max-conn config
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov authored and palkan committed May 31, 2021
1 parent a8a93e9 commit a06b824
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Added `--max-conn` option to limit simultaneous server connections. ([@skryukov][])

- Added `data_sent_bytes_total` and `data_rcvd_bytes_total` metrics. ([@palkan][])

- Add `--allowed_origins` option to enable Origin check during the WebSocket upgrade. ([@skryukov][])
Expand Down
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewRunner(name string, config *config.Config) *Runner {
// Set global HTTP params as early as possible to make sure all servers use them
server.SSL = &config.SSL
server.Host = config.Host
server.MaxConn = config.MaxConn

return &Runner{name: name, config: config, shutdownables: []Shutdownable{}, errChan: make(chan error)}
}
Expand Down
2 changes: 2 additions & 0 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func init() {
// Config vars
fs.StringVar(&defaults.Host, "host", "localhost", "")
fs.IntVar(&defaults.Port, "port", portDefault, "")
fs.IntVar(&defaults.MaxConn, "max-conn", 0, "")
fs.StringVar(&defaults.Path, "path", "/cable", "")
fs.StringVar(&defaults.HealthPath, "health-path", "/health", "")

Expand Down Expand Up @@ -133,6 +134,7 @@ USAGE
OPTIONS
--host Server host, default: localhost, env: ANYCABLE_HOST
--port Server port, default: 8080, env: ANYCABLE_PORT, PORT
--max-conn Limit simultaneous server connections (0 – without limit), default: 0, env: ANYCABLE_MAX_CONN
--path WebSocket endpoint path, default: /cable, env: ANYCABLE_PATH
--health-path HTTP health endpoint path, default: /health, env: ANYCABLE_HEALTH_PATH
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config struct {
HTTPPubSub pubsub.HTTPConfig
Host string
Port int
MaxConn int
BroadcastAdapter string
Path string
HealthPath string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/pkg/errors v0.8.1 // indirect
github.com/stretchr/testify v1.7.0
github.com/syossan27/tebata v0.0.0-20180602121909-b283fe4bc5ba
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f // indirect
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988 // indirect
google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380 // indirect
google.golang.org/grpc v1.29.1
Expand Down
2 changes: 1 addition & 1 deletion metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func FromConfig(config *Config) (*Metrics, error) {

if config.HTTPEnabled() {
if config.Host != "" && config.Host != server.Host {
srv, err := server.NewServer(config.Host, strconv.Itoa(config.Port), server.SSL)
srv, err := server.NewServer(config.Host, strconv.Itoa(config.Port), server.SSL, 0)
if err != nil {
return nil, err
}
Expand Down
23 changes: 19 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"

"github.com/apex/log"
"golang.org/x/net/netutil"
)

// HTTPServer is wrapper over http.Server
Expand All @@ -19,6 +20,7 @@ type HTTPServer struct {
secured bool
shutdown bool
started bool
maxConn int
mu sync.Mutex
log *log.Entry

Expand All @@ -31,12 +33,14 @@ var (
Host string = "localhost"
// SSL is a default configuration for HTTP servers
SSL *SSLConfig
// MaxConn is a default configuration for maximum connections
MaxConn int
)

// ForPort creates new or returns the existing server for the specified port
func ForPort(port string) (*HTTPServer, error) {
if _, ok := allServers[port]; !ok {
server, err := NewServer(Host, port, SSL)
server, err := NewServer(Host, port, SSL, MaxConn)
if err != nil {
return nil, err
}
Expand All @@ -47,7 +51,7 @@ func ForPort(port string) (*HTTPServer, error) {
}

// NewServer builds HTTPServer from config params
func NewServer(host string, port string, ssl *SSLConfig) (*HTTPServer, error) {
func NewServer(host string, port string, ssl *SSLConfig, maxConn int) (*HTTPServer, error) {
mux := http.NewServeMux()
addr := net.JoinHostPort(host, port)

Expand All @@ -72,6 +76,7 @@ func NewServer(host string, port string, ssl *SSLConfig) (*HTTPServer, error) {
secured: secured,
shutdown: false,
started: false,
maxConn: maxConn,
log: log.WithField("context", "http"),
}, nil
}
Expand All @@ -87,11 +92,21 @@ func (s *HTTPServer) Start() error {
s.started = true
s.mu.Unlock()

ln, err := net.Listen("tcp", s.addr)
if err != nil {
return err
}
defer ln.Close()

if s.maxConn > 0 {
ln = netutil.LimitListener(ln, s.maxConn)
}

if s.secured {
return s.server.ListenAndServeTLS("", "")
return s.server.ServeTLS(ln, "", "")
}

return s.server.ListenAndServe()
return s.server.Serve(ln)
}

// StartAndAnnounce prints server info and starts server
Expand Down
74 changes: 74 additions & 0 deletions vendor/golang.org/x/net/netutil/listen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ golang.org/x/net/http2
golang.org/x/net/http2/hpack
golang.org/x/net/idna
golang.org/x/net/internal/timeseries
golang.org/x/net/netutil
golang.org/x/net/trace
# golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988
## explicit
Expand Down

0 comments on commit a06b824

Please sign in to comment.