Skip to content

Commit

Permalink
keepalive: windows fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed Nov 24, 2017
1 parent 735484a commit 9b3230e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
30 changes: 30 additions & 0 deletions keepalive.go
Original file line number Diff line number Diff line change
@@ -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)
}
23 changes: 23 additions & 0 deletions keepalive_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 0 additions & 10 deletions tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
5 changes: 0 additions & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9b3230e

Please sign in to comment.