Преглед изворни кода

Add TCP_NOTSENT_LOWAT setting

pull/455/head
9seconds пре 4 недеља
родитељ
комит
21b51d5ae5
4 измењених фајлова са 36 додато и 0 уклоњено
  1. 8
    0
      network/v2/init.go
  2. 1
    0
      network/v2/sockopts.go
  3. 20
    0
      network/v2/sockopts_lowat.go
  4. 7
    0
      network/v2/sockopts_lowat_stub.go

+ 8
- 0
network/v2/init.go Прегледај датотеку

@@ -55,6 +55,14 @@ const (
55 55
 	// tcpLingerTimeout defines a number of seconds to wait for sending
56 56
 	// unacknowledged data.
57 57
 	tcpLingerTimeout = 1
58
+
59
+	// tcpNotSentLowat limits the amount of unsent data queued in the
60
+	// kernel write buffer per socket. When the unsent data drops below
61
+	// this threshold, the socket becomes writable again. This reduces
62
+	// per-connection memory usage and bufferbloat by applying
63
+	// back-pressure to the relay loop instead of piling up data in
64
+	// kernel buffers.
65
+	tcpNotSentLowat = 128 * 1024
58 66
 )
59 67
 
60 68
 var (

+ 1
- 0
network/v2/sockopts.go Прегледај датотеку

@@ -25,6 +25,7 @@ func setCommonSocketOptions(conn *net.TCPConn, keepAliveConfig net.KeepAliveConf
25 25
 
26 26
 	setCongestionControl(rawConn)
27 27
 	setTCPUserTimeout(rawConn, keepAliveConfig)
28
+	setNotSentLowat(rawConn)
28 29
 
29 30
 	return nil
30 31
 }

+ 20
- 0
network/v2/sockopts_lowat.go Прегледај датотеку

@@ -0,0 +1,20 @@
1
+//go:build linux || darwin
2
+
3
+package network
4
+
5
+import (
6
+	"syscall"
7
+
8
+	"golang.org/x/sys/unix"
9
+)
10
+
11
+// setNotSentLowat sets TCP_NOTSENT_LOWAT which limits the amount of
12
+// unsent data queued in the kernel write buffer. Once unsent data drops
13
+// below this threshold the socket becomes writable again, applying
14
+// back-pressure to the relay loop instead of piling up data in kernel
15
+// buffers. This reduces per-connection memory and bufferbloat.
16
+func setNotSentLowat(conn syscall.RawConn) {
17
+	conn.Control(func(fd uintptr) { //nolint: errcheck
18
+		unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_NOTSENT_LOWAT, tcpNotSentLowat) //nolint: errcheck
19
+	})
20
+}

+ 7
- 0
network/v2/sockopts_lowat_stub.go Прегледај датотеку

@@ -0,0 +1,7 @@
1
+//go:build !linux && !darwin
2
+
3
+package network
4
+
5
+import "syscall"
6
+
7
+func setNotSentLowat(conn syscall.RawConn) {}

Loading…
Откажи
Сачувај