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 }