-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dial: use OS defaults for TCP keepalive params
Fixes #605
- Loading branch information
Showing
3 changed files
with
75 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2023 Sauce Labs Inc., all rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
//go:build unix | ||
|
||
package forwarder | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func enableTCPKeepAlive(d *net.Dialer) { | ||
d.KeepAlive = -1 | ||
d.Control = func(network, address string, c syscall.RawConn) error { | ||
return c.Control(func(fd uintptr) { | ||
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to set SO_KEEPALIVE: %v\n", err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2023 Sauce Labs Inc., all rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
//go:build windows | ||
|
||
package forwarder | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"syscall" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
func enableTCPKeepAlive(d *net.Dialer) { | ||
d.KeepAlive = -1 | ||
d.Control = func(network, address string, c syscall.RawConn) error { | ||
return c.Control(func(fd uintptr) { | ||
optval := (*byte)(unsafe.Pointer(&[4]byte{1})) | ||
if err := windows.Setsockopt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_KEEPALIVE, optval, 1); err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to set SO_KEEPALIVE: %v\n", err) | ||
} | ||
}) | ||
} | ||
} |