From f2e7d13b7c724fcfd48d3afd85fc4226dc5383ee Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 11 Jan 2025 11:39:33 -0800 Subject: [PATCH] lanscaping: use sync.OnceValue and not types/lazy versions that predate it Change-Id: I42e2627fc731645e9462ea36afe6a57faa6eb73f Signed-off-by: Brad Fitzpatrick --- types/lazy/lazy.go | 70 -------------------------------------------- version/print.go | 5 ++-- version/prop.go | 3 +- version/version.go | 3 +- wgengine/pendopen.go | 46 +---------------------------- 5 files changed, 7 insertions(+), 120 deletions(-) diff --git a/types/lazy/lazy.go b/types/lazy/lazy.go index 43325512d..4b292af94 100644 --- a/types/lazy/lazy.go +++ b/types/lazy/lazy.go @@ -119,73 +119,3 @@ func (z *SyncValue[T]) PeekErr() (v T, err error, ok bool) { var zero T return zero, nil, false } - -// SyncFunc wraps a function to make it lazy. -// -// The returned function calls fill the first time it's called, and returns -// fill's result on every subsequent call. -// -// The returned function is safe for concurrent use. -func SyncFunc[T any](fill func() T) func() T { - var ( - once sync.Once - v T - ) - return func() T { - once.Do(func() { v = fill() }) - return v - } -} - -// SyncFuncErr wraps a function to make it lazy. -// -// The returned function calls fill the first time it's called, and returns -// fill's results on every subsequent call. -// -// The returned function is safe for concurrent use. -func SyncFuncErr[T any](fill func() (T, error)) func() (T, error) { - var ( - once sync.Once - v T - err error - ) - return func() (T, error) { - once.Do(func() { v, err = fill() }) - return v, err - } -} - -// TB is a subset of testing.TB that we use to set up test helpers. -// It's defined here to avoid pulling in the testing package. -type TB interface { - Helper() - Cleanup(func()) -} - -// SetForTest sets z's value and error. -// It's used in tests only and reverts z's state back when tb and all its -// subtests complete. -// It is not safe for concurrent use and must not be called concurrently with -// any SyncValue methods, including another call to itself. -func (z *SyncValue[T]) SetForTest(tb TB, val T, err error) { - tb.Helper() - - oldErr, oldVal := z.err.Load(), z.v - z.once.Do(func() {}) - - z.v = val - if err != nil { - z.err.Store(ptr.To(err)) - } else { - z.err.Store(nilErrPtr) - } - - tb.Cleanup(func() { - if oldErr == nil { - *z = SyncValue[T]{} - } else { - z.v = oldVal - z.err.Store(oldErr) - } - }) -} diff --git a/version/print.go b/version/print.go index 7d8554279..be90432cc 100644 --- a/version/print.go +++ b/version/print.go @@ -7,11 +7,10 @@ import ( "fmt" "runtime" "strings" - - "tailscale.com/types/lazy" + "sync" ) -var stringLazy = lazy.SyncFunc(func() string { +var stringLazy = sync.OnceValue(func() string { var ret strings.Builder ret.WriteString(Short()) ret.WriteByte('\n') diff --git a/version/prop.go b/version/prop.go index fee76c65f..6026d1179 100644 --- a/version/prop.go +++ b/version/prop.go @@ -9,6 +9,7 @@ import ( "runtime" "strconv" "strings" + "sync" "tailscale.com/tailcfg" "tailscale.com/types/lazy" @@ -174,7 +175,7 @@ func IsUnstableBuild() bool { }) } -var isDev = lazy.SyncFunc(func() bool { +var isDev = sync.OnceValue(func() bool { return strings.Contains(Short(), "-dev") }) diff --git a/version/version.go b/version/version.go index 5edea22ca..2add25689 100644 --- a/version/version.go +++ b/version/version.go @@ -9,6 +9,7 @@ import ( "runtime/debug" "strconv" "strings" + "sync" tailscaleroot "tailscale.com" "tailscale.com/types/lazy" @@ -117,7 +118,7 @@ func (i embeddedInfo) commitAbbrev() string { return i.commit } -var getEmbeddedInfo = lazy.SyncFunc(func() embeddedInfo { +var getEmbeddedInfo = sync.OnceValue(func() embeddedInfo { bi, ok := debug.ReadBuildInfo() if !ok { return embeddedInfo{} diff --git a/wgengine/pendopen.go b/wgengine/pendopen.go index 7db07c685..c7c028d06 100644 --- a/wgengine/pendopen.go +++ b/wgengine/pendopen.go @@ -6,16 +6,12 @@ package wgengine import ( "fmt" "net/netip" - "runtime" - "strings" "time" - "github.com/gaissmai/bart" "tailscale.com/net/flowtrack" "tailscale.com/net/packet" "tailscale.com/net/tstun" "tailscale.com/types/ipproto" - "tailscale.com/types/lazy" "tailscale.com/util/mak" "tailscale.com/wgengine/filter" ) @@ -89,33 +85,6 @@ func (e *userspaceEngine) trackOpenPreFilterIn(pp *packet.Parsed, t *tstun.Wrapp return } -var ( - appleIPRange = netip.MustParsePrefix("17.0.0.0/8") - canonicalIPs = lazy.SyncFunc(func() (checkIPFunc func(netip.Addr) bool) { - // https://bgp.he.net/AS41231#_prefixes - t := &bart.Table[bool]{} - for _, s := range strings.Fields(` - 91.189.89.0/24 - 91.189.91.0/24 - 91.189.92.0/24 - 91.189.93.0/24 - 91.189.94.0/24 - 91.189.95.0/24 - 162.213.32.0/24 - 162.213.34.0/24 - 162.213.35.0/24 - 185.125.188.0/23 - 185.125.190.0/24 - 194.169.254.0/24`) { - t.Insert(netip.MustParsePrefix(s), true) - } - return func(ip netip.Addr) bool { - v, _ := t.Lookup(ip) - return v - } - }) -) - // isOSNetworkProbe reports whether the target is likely a network // connectivity probe target from e.g. iOS or Ubuntu network-manager. // @@ -123,20 +92,7 @@ var ( // Don't start timers tracking those. They won't succeed anyway. Avoids log // spam like: func (e *userspaceEngine) isOSNetworkProbe(dst netip.AddrPort) bool { - // iOS had log spam like: - // open-conn-track: timeout opening (100.115.73.60:52501 => 17.125.252.5:443); no associated peer node - if runtime.GOOS == "ios" && dst.Port() == 443 && appleIPRange.Contains(dst.Addr()) { - if _, ok := e.PeerForIP(dst.Addr()); !ok { - return true - } - } - // NetworkManager; https://github.com/tailscale/tailscale/issues/13687 - // open-conn-track: timeout opening (TCP 100.96.229.119:42798 => 185.125.190.49:80); no associated peer node - if runtime.GOOS == "linux" && dst.Port() == 80 && canonicalIPs()(dst.Addr()) { - if _, ok := e.PeerForIP(dst.Addr()); !ok { - return true - } - } + return false }