diff --git a/keepalive.go b/keepalive.go new file mode 100644 index 0000000..04d7bd8 --- /dev/null +++ b/keepalive.go @@ -0,0 +1,30 @@ +// Copyright (C) 2017 Michał Matczuk +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !windows + +package tunnel + +import ( + "net" + "time" + + "github.com/felixge/tcpkeepalive" +) + +var ( + // DefaultKeepAliveIdleTime specifies how long connection can be idle + // before sending keepalive message. + DefaultKeepAliveIdleTime = 15 * time.Minute + // DefaultKeepAliveCount specifies maximal number of keepalive messages + // sent before marking connection as dead. + DefaultKeepAliveCount = 8 + // DefaultKeepAliveInterval specifies how often retry sending keepalive + // messages when no response is received. + DefaultKeepAliveInterval = 5 * time.Second +) + +func keepAlive(conn net.Conn) error { + return tcpkeepalive.SetKeepAlive(conn, DefaultKeepAliveIdleTime, DefaultKeepAliveCount, DefaultKeepAliveInterval) +} diff --git a/keepalive_windows.go b/keepalive_windows.go new file mode 100644 index 0000000..6dd83be --- /dev/null +++ b/keepalive_windows.go @@ -0,0 +1,23 @@ +// Copyright (C) 2017 Michał Matczuk +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package tunnel + +import ( + "fmt" + "net" +) + +func keepAlive(conn net.Conn) error { + c, ok := conn.(*net.TCPConn) + if !ok { + return fmt.Errorf("Bad connection type: %T", c) + } + + if err := c.SetKeepAlive(true); err != nil { + return err + } + + return nil +} diff --git a/tunnel.go b/tunnel.go index 514785f..8addf05 100644 --- a/tunnel.go +++ b/tunnel.go @@ -11,14 +11,4 @@ var ( DefaultTimeout = 10 * time.Second // DefaultPingTimeout specifies a ping timeout. DefaultPingTimeout = 500 * time.Millisecond - - // DefaultKeepAliveIdleTime specifies how long connection can be idle - // before sending keepalive message. - DefaultKeepAliveIdleTime = 15 * time.Minute - // DefaultKeepAliveCount specifies maximal number of keepalive messages - // sent before marking connection as dead. - DefaultKeepAliveCount = 8 - // DefaultKeepAliveInterval specifies how often retry sending keepalive - // messages when no response is received. - DefaultKeepAliveInterval = 5 * time.Second ) diff --git a/utils.go b/utils.go index 5a0e27f..6340e8d 100644 --- a/utils.go +++ b/utils.go @@ -10,14 +10,9 @@ import ( "net/http" "strings" - "github.com/felixge/tcpkeepalive" "github.com/mmatczuk/go-http-tunnel/log" ) -func keepAlive(conn net.Conn) error { - return tcpkeepalive.SetKeepAlive(conn, DefaultKeepAliveIdleTime, DefaultKeepAliveCount, DefaultKeepAliveInterval) -} - func transfer(dst io.Writer, src io.Reader, logger log.Logger) { n, err := io.Copy(dst, src) if err != nil {