Skip to content

Commit

Permalink
Make SO_LINGER configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
blaubaer committed Sep 10, 2020
1 parent 96e54f0 commit 882afa4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 5 additions & 3 deletions server/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import (
)

type limitedListener struct {
linger int16
sync.Mutex
net.Listener
sem chan bool
}

func newLimitedListener(count uint16, l net.Listener) *limitedListener {
func newLimitedListener(count uint16, linger int16, l net.Listener) *limitedListener {
sem := make(chan bool, count)
for i := uint16(0); i < count; i++ {
sem <- true
}
return &limitedListener{
Listener: l,
linger: linger,
sem: sem,
}
}
Expand All @@ -35,8 +37,8 @@ func (instance *limitedListener) Accept() (net.Conn, error) {
if c, err := instance.Listener.Accept(); err != nil {
return nil, err
} else {
if err := c.(*net.TCPConn).SetLinger(0); err != nil {
return nil, fmt.Errorf("cannot set the SO_LINGER to 0")
if err := c.(*net.TCPConn).SetLinger(int(instance.linger)); err != nil {
return nil, fmt.Errorf("cannot set the SO_LINGER to %d", instance.linger)
}
result := &limitedConn{
Conn: c,
Expand Down
8 changes: 7 additions & 1 deletion server/connector_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type HttpConnector struct {
Id ConnectorId
Handler ConnectorHandler

SoLinger int16
MaxConnections uint16

Server http.Server
Expand All @@ -25,6 +26,7 @@ func NewHttpConnector(id ConnectorId) (*HttpConnector, error) {
result := HttpConnector{
Id: id,
MaxConnections: 512,
SoLinger: -1,

Server: http.Server{
Addr: ":8080",
Expand Down Expand Up @@ -53,7 +55,7 @@ func (instance *HttpConnector) Serve(stop support.Channel) error {
if err != nil {
return err
}
ln = newLimitedListener(instance.MaxConnections, ln)
ln = newLimitedListener(instance.MaxConnections, instance.SoLinger, ln)

var serve func() error
if tlsConfig := instance.Server.TLSConfig; tlsConfig != nil {
Expand Down Expand Up @@ -121,6 +123,10 @@ func (instance *HttpConnector) RegisterFlag(fe support.FlagEnabled, appPrefix st
PlaceHolder(fmt.Sprint(instance.MaxConnections)).
Envar(instance.serverFlagEnvVar(appPrefix, "MAX_CONNECTIONS")).
Uint16Var(&instance.MaxConnections)
fe.Flag(instance.serverFlagName("soLinger"), "Set the behavior of SO_LINGER.").
PlaceHolder(fmt.Sprint(instance.SoLinger)).
Envar(instance.serverFlagEnvVar(appPrefix, "SO_LINGER")).
Int16Var(&instance.SoLinger)

fe.Flag(instance.clientFlagName("maxHeaderBytes"), "Maximum number of bytes the server will read parsing the request header's keys and values, including the request line. It does not limit the size of the request body.").
PlaceHolder(fmt.Sprint(instance.Server.MaxHeaderBytes)).
Expand Down

0 comments on commit 882afa4

Please sign in to comment.