Skip to content
This repository has been archived by the owner on Mar 22, 2019. It is now read-only.

Adds ListenerLimit option, to limit simultaneous connections #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions gracehttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/facebookgo/grace/gracenet"
"github.com/facebookgo/httpdown"
"golang.org/x/net/netutil"
)

var (
Expand All @@ -35,6 +36,7 @@ type app struct {
sds []httpdown.Server
preStartProcess func() error
errors chan error
listenerLimit int
}

func newApp(servers []*http.Server) *app {
Expand All @@ -46,6 +48,7 @@ func newApp(servers []*http.Server) *app {
sds: make([]httpdown.Server, 0, len(servers)),

preStartProcess: func() error { return nil },
listenerLimit: 0,
// 2x num servers for possible Close or Stop errors + 1 for possible
// StartProcess error.
errors: make(chan error, 1+(len(servers)*2)),
Expand All @@ -59,6 +62,9 @@ func (a *app) listen() error {
if err != nil {
return err
}
if a.listenerLimit > 0 {
l = netutil.LimitListener(l, a.listenerLimit)
}
if s.TLSConfig != nil {
l = tls.NewListener(l, s.TLSConfig)
}
Expand Down Expand Up @@ -202,6 +208,12 @@ func PreStartProcess(hook func() error) option {
}
}

func ListenerLimit(limit int) option {
return func(a *app) {
a.listenerLimit = limit
}
}

// Used for pretty printing addresses.
func pprintAddr(listeners []net.Listener) []byte {
var out bytes.Buffer
Expand Down