From 80fe2e6e37120e665cd523e1edf7eb1401b9b73b Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 2 Aug 2018 19:15:25 +0000 Subject: [PATCH] net: lazily look up the listenerBacklog value on first use Don't open files or do sysctls in init. Updates #26775 Change-Id: I017bed6c24ef1e4bc30040120349fb779f203225 Reviewed-on: https://go-review.googlesource.com/127655 Reviewed-by: Ian Lance Taylor --- src/net/net.go | 11 ++++++++++- src/net/sock_posix.go | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/net/net.go b/src/net/net.go index c9099862699068..77b8f69074e39c 100644 --- a/src/net/net.go +++ b/src/net/net.go @@ -357,7 +357,16 @@ type PacketConn interface { SetWriteDeadline(t time.Time) error } -var listenerBacklog = maxListenerBacklog() +var listenerBacklogCache struct { + sync.Once + val int +} + +// listenerBacklog is a caching wrapper around maxListenerBacklog. +func listenerBacklog() int { + listenerBacklogCache.Do(func() { listenerBacklogCache.val = maxListenerBacklog() }) + return listenerBacklogCache.val +} // A Listener is a generic network listener for stream-oriented protocols. // diff --git a/src/net/sock_posix.go b/src/net/sock_posix.go index 677e423ffaff2b..1cfd8a58c604bc 100644 --- a/src/net/sock_posix.go +++ b/src/net/sock_posix.go @@ -54,7 +54,7 @@ func socket(ctx context.Context, net string, family, sotype, proto int, ipv6only if laddr != nil && raddr == nil { switch sotype { case syscall.SOCK_STREAM, syscall.SOCK_SEQPACKET: - if err := fd.listenStream(laddr, listenerBacklog, ctrlFn); err != nil { + if err := fd.listenStream(laddr, listenerBacklog(), ctrlFn); err != nil { fd.Close() return nil, err }