|
|
@@ -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
|
+}
|