Skip to content

Commit

Permalink
Merge pull request #127 from Netflix/listener-interface
Browse files Browse the repository at this point in the history
Updating interface for server.Listener to support wrapped connections
  • Loading branch information
ScottMansfield authored Dec 21, 2017
2 parents 22dcb0d + 463f16f commit d3db570
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 11 additions & 7 deletions server/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit d3db570

Please sign in to comment.