-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for a connection to check if it's sending bytes
- Loading branch information
1 parent
8f91506
commit 9a288f7
Showing
3 changed files
with
111 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//go:build linux | ||
|
||
package sockopt | ||
|
||
import ( | ||
"net" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func isSocketFdSendingBytes(fd int) (bool, error) { | ||
tcpInfo, err := unix.GetsockoptTCPInfo(int(fd), unix.IPPROTO_TCP, unix.TCP_INFO) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// 1 == TCP_ESTABLISHED, but for some reason not available in the package | ||
if tcpInfo.State != 1 { | ||
// If the connection is not established, the socket is not sending bytes | ||
return false, nil | ||
} | ||
|
||
return tcpInfo.Notsent_bytes != 0, nil | ||
} | ||
|
||
func isConnectionSendingBytesImplemented() bool { | ||
return true | ||
} | ||
|
||
func isConnectionSendingBytes(conn *net.TCPConn) (bool, error) { | ||
syscallConn, err := conn.SyscallConn() | ||
if err != nil { | ||
return false, err | ||
} | ||
var result bool | ||
syscallConn.Control(func(fd uintptr) { | ||
innerResult, innerErr := isSocketFdSendingBytes(int(fd)) | ||
result = innerResult | ||
err = innerErr | ||
}) | ||
return result, 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,17 @@ | ||
//go:build !linux | ||
|
||
package sockopt | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
) | ||
|
||
func isConnectionSendingBytesImplemented() bool { | ||
return false | ||
} | ||
|
||
func isConnectionSendingBytes(_ *net.TCPConn) (bool, error) { | ||
return false, fmt.Errorf("%w: checking if socket is sending bytes is not implemented on this platform", errors.ErrUnsupported) | ||
} |
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