mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-25 11:11:18 -08:00
lanscaping: use sync.OnceValue and not types/lazy versions that predate it
Change-Id: I42e2627fc731645e9462ea36afe6a57faa6eb73f Signed-off-by: Brad Fitzpatrick <[email protected]>
This commit is contained in:
1 parent
718e30c4b9
commit
f2e7d13b7c
5 files changed
+7
-120
No files matched your search
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
+2
-3
@@ -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')
|
||||
|
||||
+2
-1
@@ -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")
|
||||
})
|
||||
|
||||
|
||||
+2
-1
@@ -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{}
|
||||
|
||||
+1
-45
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user