From 463f16f2569f3c623302878c6948593a7ac3cbe7 Mon Sep 17 00:00:00 2001 From: Scott Mansfield Date: Wed, 20 Dec 2017 15:57:14 -0800 Subject: [PATCH] Updating interface for server.Listener to support wrapped connections --- server/listen.go | 18 +++++++++++------- server/types.go | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/server/listen.go b/server/listen.go index bb5b996..0f5b7c1 100644 --- a/server/listen.go +++ b/server/listen.go @@ -37,14 +37,18 @@ func (l *tcpListener) Accept() (net.Conn, error) { return l.listener.Accept() } -func (l *tcpListener) ModifyConnSettings(conn net.Conn) error { +func (l *tcpListener) Configure(conn net.Conn) (net.Conn, error) { tcpRemote := conn.(*net.TCPConn) if err := tcpRemote.SetKeepAlive(true); err != nil { - return err + return conn, err } - return tcpRemote.SetKeepAlivePeriod(30 * time.Second) + if err := tcpRemote.SetKeepAlivePeriod(30 * time.Second); err != nil { + return conn, err + } + + return conn, nil } // TCPListener is a ListenConst that returns a tcp listener for the given port @@ -66,8 +70,8 @@ func (l *unixListener) Accept() (net.Conn, error) { return l.listener.Accept() } -func (l *unixListener) ModifyConnSettings(conn net.Conn) error { - return nil +func (l *unixListener) Configure(conn net.Conn) (net.Conn, error) { + return conn, nil } // UnixListener is a ListenConst that returns a unix domain socket listener for the given path @@ -123,9 +127,9 @@ func ListenAndServe(l ListenConst, ps []protocol.Components, s ServerConst, o or } metrics.IncCounter(MetricConnectionsEstablishedExt) - err = listener.ModifyConnSettings(remote) + remote, err = listener.Configure(remote) if err != nil { - log.Println("Error modifying connection settings after accept:", err.Error()) + log.Println("Error configuring connection after accept:", err.Error()) remote.Close() continue } diff --git a/server/types.go b/server/types.go index fd30ff7..2485a6c 100644 --- a/server/types.go +++ b/server/types.go @@ -35,11 +35,13 @@ type Server interface { Loop() } +// ListenConst is a constructor function for listener implementations type ListenConst func() (Listener, error) +// Listener is a type to accept and configure new connections type Listener interface { Accept() (net.Conn, error) - ModifyConnSettings(net.Conn) error + Configure(net.Conn) (net.Conn, error) } var (