From a204ed53d907c3b325e3c2bdd6f847a8f97e90d9 Mon Sep 17 00:00:00 2001 From: 1911860538 Date: Fri, 18 Apr 2025 14:11:29 +0000 Subject: [PATCH] net: simplify readProtocols via sync.OnceFunc In this case, using sync.OnceFunc is a better choice. Change-Id: I52d27b9741265c90300a04a03537020e1aaaaaa7 GitHub-Last-Rev: a281daea255f1508a9042e8c8c7eb7ca1cef2430 GitHub-Pull-Request: golang/go#73434 Reviewed-on: https://go-review.googlesource.com/c/go/+/666635 Reviewed-by: David Chase Auto-Submit: Jorropo Reviewed-by: Damien Neil LUCI-TryBot-Result: Go LUCI Reviewed-by: Jorropo --- src/net/lookup_unix.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/net/lookup_unix.go b/src/net/lookup_unix.go index 382a2d44bb..7416cb01f8 100644 --- a/src/net/lookup_unix.go +++ b/src/net/lookup_unix.go @@ -12,11 +12,9 @@ import ( "sync" ) -var onceReadProtocols sync.Once - -// readProtocols loads contents of /etc/protocols into protocols map +// readProtocolsOnce loads contents of /etc/protocols into protocols map // for quick access. -func readProtocols() { +var readProtocolsOnce = sync.OnceFunc(func() { file, err := open("/etc/protocols") if err != nil { return @@ -43,12 +41,12 @@ func readProtocols() { } } } -} +}) // lookupProtocol looks up IP protocol name in /etc/protocols and // returns correspondent protocol number. func lookupProtocol(_ context.Context, name string) (int, error) { - onceReadProtocols.Do(readProtocols) + readProtocolsOnce() return lookupProtocolMap(name) } -- 2.51.0