File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -240,3 +240,41 @@ func (fd *netFD) listenStream(laddr sockaddr, backlog int) error {
240240```
241241
242242Go 的 listenTCP 一个函数就把 c 网络编程中 ` socket() ` ,` bind() ` ,` listen() ` 三步都完成了。大大减小了用户的心智负担。
243+
244+ 这里有一点需要注意,listenStream 虽然提供了 backlog 的参数,但用户层是没有办法通过 Go 的代码来修改 listen 的 backlog 的。
245+
246+ ``` go
247+ func maxListenerBacklog () int {
248+ fd , err := open (" /proc/sys/net/core/somaxconn" )
249+ if err != nil {
250+ return syscall.SOMAXCONN
251+ }
252+ defer fd.close ()
253+ l , ok := fd.readLine ()
254+ if !ok {
255+ return syscall.SOMAXCONN
256+ }
257+ f := getFields (l)
258+ n , _ , ok := dtoi (f[0 ])
259+ if n == 0 || !ok {
260+ return syscall.SOMAXCONN
261+ }
262+ // Linux stores the backlog in a uint16.
263+ // Truncate number to avoid wrapping.
264+ // See issue 5030.
265+ if n > 1 <<16 -1 {
266+ n = 1 <<16 - 1
267+ }
268+ return n
269+ }
270+ ```
271+
272+ 如上,在 linux 中,如果配置了 /proc/sys/net/core/somaxconn,那么就用这个值,如果没有配置,那么就使用 syscall 中的 SOMAXCONN:
273+
274+ ``` go
275+ const (
276+ SOMAXCONN = 0x80 // 128
277+ )
278+ ```
279+
280+ 社区里有很多人吐槽,希望能有手段能修改这个值,不过看起来官方并不打算支持。所以现阶段只能通过修改 /proc/sys/net/core/somaxconn 来修改 listen 的 backlog。
You can’t perform that action at this time.
0 commit comments