fix:设置缓冲区buffer,设置足够大的 oob buffer 来接收控制消息,调用 recvmsg 从 errqueue 读一条消息
This commit is contained in:
@@ -15,7 +15,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
linuxTimestampControlBufferSize = 256 // 控制消息缓冲区。
|
linuxTimestampControlBufferSize = 2560 // 控制消息缓冲区。
|
||||||
|
linuxSocketWriteBufferSize = 10 * 1024 * 1024 // 请求把 socket 发送缓冲区调到 10 MiB。
|
||||||
linuxTXTimestampWaitTimeout = 250 * time.Millisecond // 等待 TX 时间戳的上限。
|
linuxTXTimestampWaitTimeout = 250 * time.Millisecond // 等待 TX 时间戳的上限。
|
||||||
linuxTXTimestampPollInterval = time.Millisecond // 轮询 errqueue 的间隔。
|
linuxTXTimestampPollInterval = time.Millisecond // 轮询 errqueue 的间隔。
|
||||||
linuxDataPollInterval = time.Millisecond // 轮询普通收发的间隔。
|
linuxDataPollInterval = time.Millisecond // 轮询普通收发的间隔。
|
||||||
@@ -88,6 +89,10 @@ func (c *TCPConn) initLinuxTimestamping() error {
|
|||||||
return fmt.Errorf("transport: missing syscall conn")
|
return fmt.Errorf("transport: missing syscall conn")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := configureLinuxSocketWriteBuffer(rawConn); err != nil {
|
||||||
|
return fmt.Errorf("transport: configure socket write buffer: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
//socket是否可以成功打开 timestamping 取决于内核版本和配置,尝试多个 flag 组合直到成功或遇到非 EINVAL 错误。
|
//socket是否可以成功打开 timestamping 取决于内核版本和配置,尝试多个 flag 组合直到成功或遇到非 EINVAL 错误。
|
||||||
if err := enableLinuxTimestamping(rawConn); err != nil {
|
if err := enableLinuxTimestamping(rawConn); err != nil {
|
||||||
return fmt.Errorf("transport: enable linux timestamping: %w", err)
|
return fmt.Errorf("transport: enable linux timestamping: %w", err)
|
||||||
@@ -97,6 +102,20 @@ func (c *TCPConn) initLinuxTimestamping() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 设置 TCP缓冲区buffer size
|
||||||
|
func configureLinuxSocketWriteBuffer(rawConn syscall.RawConn) error {
|
||||||
|
var lastErr error
|
||||||
|
|
||||||
|
err := rawConn.Control(func(fd uintptr) {
|
||||||
|
lastErr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_SNDBUF, linuxSocketWriteBufferSize)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastErr
|
||||||
|
}
|
||||||
|
|
||||||
// 给 socket开权限打开TX software timestamping。
|
// 给 socket开权限打开TX software timestamping。
|
||||||
func enableLinuxTimestamping(rawConn syscall.RawConn) error {
|
func enableLinuxTimestamping(rawConn syscall.RawConn) error {
|
||||||
flagCandidates := []int{ //不同linux版本可能支持不同的 flag 组合,尝试多个组合直到成功。
|
flagCandidates := []int{ //不同linux版本可能支持不同的 flag 组合,尝试多个组合直到成功。
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ func TestSelectTXTimestampEventsFallsBackToHighestObservedID(t *testing.T) {
|
|||||||
|
|
||||||
func TestLinuxTimestampingDebugLoggerCapturesChunkAndErrqueueEvents(t *testing.T) {
|
func TestLinuxTimestampingDebugLoggerCapturesChunkAndErrqueueEvents(t *testing.T) {
|
||||||
clientConn, serverConn := newTCPPair(t)
|
clientConn, serverConn := newTCPPair(t)
|
||||||
setTCPWriteBuffer(t, clientConn, 4096)
|
setTCPWriteBuffer(t, clientConn, 10*1024*1024)
|
||||||
|
|
||||||
debugLogger := &recordingTXTimestampDebugLogger{}
|
debugLogger := &recordingTXTimestampDebugLogger{}
|
||||||
senderLogger := &recordingLogger{}
|
senderLogger := &recordingLogger{}
|
||||||
|
|||||||
Reference in New Issue
Block a user