From 882afa4163789da997bbccada062248f7dd004bd Mon Sep 17 00:00:00 2001 From: Gregor Noczinski Date: Thu, 10 Sep 2020 14:25:59 +0200 Subject: [PATCH] Make SO_LINGER configurable --- server/connections.go | 8 +++++--- server/connector_http.go | 8 +++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/server/connections.go b/server/connections.go index 38a0423..a333c88 100644 --- a/server/connections.go +++ b/server/connections.go @@ -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, } } @@ -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, diff --git a/server/connector_http.go b/server/connector_http.go index 16fcb69..ac14a2b 100644 --- a/server/connector_http.go +++ b/server/connector_http.go @@ -15,6 +15,7 @@ type HttpConnector struct { Id ConnectorId Handler ConnectorHandler + SoLinger int16 MaxConnections uint16 Server http.Server @@ -25,6 +26,7 @@ func NewHttpConnector(id ConnectorId) (*HttpConnector, error) { result := HttpConnector{ Id: id, MaxConnections: 512, + SoLinger: -1, Server: http.Server{ Addr: ":8080", @@ -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 { @@ -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)).