-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit sets TCP_USER_TIMEOUT socket option for tcp connection so that channel write doesn't block indefinitely on network disconnect. Signed-off-by: Periyasamy Palanisamy <pepalani@redhat.com>
- Loading branch information
1 parent
06c1f43
commit 304a102
Showing
4 changed files
with
54 additions
and
0 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
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 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"syscall" | ||
"time" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// SetTCPUserTimeout sets the TCP user timeout on a connection's socket | ||
func SetTCPUserTimeout(conn net.Conn, timeout time.Duration) error { | ||
tcpconn, ok := conn.(*net.TCPConn) | ||
if !ok { | ||
// not a TCP connection. exit early | ||
return nil | ||
} | ||
rawConn, err := tcpconn.SyscallConn() | ||
if err != nil { | ||
return fmt.Errorf("error getting raw connection: %v", err) | ||
} | ||
err = rawConn.Control(func(fd uintptr) { | ||
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond)) | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error setting option on socket: %v", err) | ||
} | ||
|
||
return nil | ||
} |
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,14 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package internal | ||
|
||
import ( | ||
"net" | ||
"time" | ||
) | ||
|
||
// SetTCPUserTimeout is a no-op function under non-linux environments. | ||
func SetTCPUserTimeout(conn net.Conn, timeout time.Duration) error { | ||
return nil | ||
} |