Skip to content

Commit 9b3230e

Browse files
committed
keepalive: windows fix
1 parent 735484a commit 9b3230e

4 files changed

Lines changed: 53 additions & 15 deletions

File tree

keepalive.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (C) 2017 Michał Matczuk
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !windows
6+
7+
package tunnel
8+
9+
import (
10+
"net"
11+
"time"
12+
13+
"github.com/felixge/tcpkeepalive"
14+
)
15+
16+
var (
17+
// DefaultKeepAliveIdleTime specifies how long connection can be idle
18+
// before sending keepalive message.
19+
DefaultKeepAliveIdleTime = 15 * time.Minute
20+
// DefaultKeepAliveCount specifies maximal number of keepalive messages
21+
// sent before marking connection as dead.
22+
DefaultKeepAliveCount = 8
23+
// DefaultKeepAliveInterval specifies how often retry sending keepalive
24+
// messages when no response is received.
25+
DefaultKeepAliveInterval = 5 * time.Second
26+
)
27+
28+
func keepAlive(conn net.Conn) error {
29+
return tcpkeepalive.SetKeepAlive(conn, DefaultKeepAliveIdleTime, DefaultKeepAliveCount, DefaultKeepAliveInterval)
30+
}

keepalive_windows.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (C) 2017 Michał Matczuk
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package tunnel
6+
7+
import (
8+
"fmt"
9+
"net"
10+
)
11+
12+
func keepAlive(conn net.Conn) error {
13+
c, ok := conn.(*net.TCPConn)
14+
if !ok {
15+
return fmt.Errorf("Bad connection type: %T", c)
16+
}
17+
18+
if err := c.SetKeepAlive(true); err != nil {
19+
return err
20+
}
21+
22+
return nil
23+
}

tunnel.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,4 @@ var (
1111
DefaultTimeout = 10 * time.Second
1212
// DefaultPingTimeout specifies a ping timeout.
1313
DefaultPingTimeout = 500 * time.Millisecond
14-
15-
// DefaultKeepAliveIdleTime specifies how long connection can be idle
16-
// before sending keepalive message.
17-
DefaultKeepAliveIdleTime = 15 * time.Minute
18-
// DefaultKeepAliveCount specifies maximal number of keepalive messages
19-
// sent before marking connection as dead.
20-
DefaultKeepAliveCount = 8
21-
// DefaultKeepAliveInterval specifies how often retry sending keepalive
22-
// messages when no response is received.
23-
DefaultKeepAliveInterval = 5 * time.Second
2414
)

utils.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ import (
1010
"net/http"
1111
"strings"
1212

13-
"github.com/felixge/tcpkeepalive"
1413
"github.com/mmatczuk/go-http-tunnel/log"
1514
)
1615

17-
func keepAlive(conn net.Conn) error {
18-
return tcpkeepalive.SetKeepAlive(conn, DefaultKeepAliveIdleTime, DefaultKeepAliveCount, DefaultKeepAliveInterval)
19-
}
20-
2116
func transfer(dst io.Writer, src io.Reader, logger log.Logger) {
2217
n, err := io.Copy(dst, src)
2318
if err != nil {

0 commit comments

Comments
 (0)