Skip to content

Commit 69b9074

Browse files
committed
util/uniq,types/lazy,*: delete code that's now in Go std
sync.OnceValue and slices.Compact were both added in Go 1.21. cmp.Or was added in Go 1.22. Updates tailscale#8632 Updates tailscale#11058 Change-Id: I89ba4c404f40188e1f8a9566c8aaa049be377754 Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent 5fdb4f8 commit 69b9074

File tree

13 files changed

+18
-271
lines changed

13 files changed

+18
-271
lines changed

cmd/k8s-operator/depaware.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,6 @@ tailscale.com/cmd/k8s-operator dependencies: (generated by github.com/tailscale/
817817
tailscale.com/util/systemd from tailscale.com/control/controlclient+
818818
tailscale.com/util/testenv from tailscale.com/control/controlclient+
819819
tailscale.com/util/truncate from tailscale.com/logtail
820-
tailscale.com/util/uniq from tailscale.com/ipn/ipnlocal+
821820
tailscale.com/util/usermetric from tailscale.com/health+
822821
tailscale.com/util/vizerror from tailscale.com/tailcfg+
823822
💣 tailscale.com/util/winutil from tailscale.com/clientupdate+

cmd/tailscaled/depaware.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
406406
tailscale.com/util/systemd from tailscale.com/control/controlclient+
407407
tailscale.com/util/testenv from tailscale.com/ipn/ipnlocal+
408408
tailscale.com/util/truncate from tailscale.com/logtail
409-
tailscale.com/util/uniq from tailscale.com/ipn/ipnlocal+
410409
tailscale.com/util/usermetric from tailscale.com/health+
411410
tailscale.com/util/vizerror from tailscale.com/tailcfg+
412411
💣 tailscale.com/util/winutil from tailscale.com/clientupdate+

ipn/ipnlocal/local.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ import (
111111
"tailscale.com/util/syspolicy/rsop"
112112
"tailscale.com/util/systemd"
113113
"tailscale.com/util/testenv"
114-
"tailscale.com/util/uniq"
115114
"tailscale.com/util/usermetric"
116115
"tailscale.com/version"
117116
"tailscale.com/version/distro"
@@ -3346,7 +3345,7 @@ func (b *LocalBackend) clearMachineKeyLocked() error {
33463345
// incoming packet.
33473346
func (b *LocalBackend) setTCPPortsIntercepted(ports []uint16) {
33483347
slices.Sort(ports)
3349-
uniq.ModifySlice(&ports)
3348+
ports = slices.Compact(ports)
33503349
var f func(uint16) bool
33513350
switch len(ports) {
33523351
case 0:

ipn/localapi/debugderp.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package localapi
55

66
import (
7+
"cmp"
78
"context"
89
"crypto/tls"
910
"encoding/json"
@@ -81,15 +82,15 @@ func (h *Handler) serveDebugDERPRegion(w http.ResponseWriter, r *http.Request) {
8182
client *http.Client = http.DefaultClient
8283
)
8384
checkConn := func(derpNode *tailcfg.DERPNode) bool {
84-
port := firstNonzero(derpNode.DERPPort, 443)
85+
port := cmp.Or(derpNode.DERPPort, 443)
8586

8687
var (
8788
hasIPv4 bool
8889
hasIPv6 bool
8990
)
9091

9192
// Check IPv4 first
92-
addr := net.JoinHostPort(firstNonzero(derpNode.IPv4, derpNode.HostName), strconv.Itoa(port))
93+
addr := net.JoinHostPort(cmp.Or(derpNode.IPv4, derpNode.HostName), strconv.Itoa(port))
9394
conn, err := dialer.DialContext(ctx, "tcp4", addr)
9495
if err != nil {
9596
st.Errors = append(st.Errors, fmt.Sprintf("Error connecting to node %q @ %q over IPv4: %v", derpNode.HostName, addr, err))
@@ -98,7 +99,7 @@ func (h *Handler) serveDebugDERPRegion(w http.ResponseWriter, r *http.Request) {
9899

99100
// Upgrade to TLS and verify that works properly.
100101
tlsConn := tls.Client(conn, &tls.Config{
101-
ServerName: firstNonzero(derpNode.CertName, derpNode.HostName),
102+
ServerName: cmp.Or(derpNode.CertName, derpNode.HostName),
102103
})
103104
if err := tlsConn.HandshakeContext(ctx); err != nil {
104105
st.Errors = append(st.Errors, fmt.Sprintf("Error upgrading connection to node %q @ %q to TLS over IPv4: %v", derpNode.HostName, addr, err))
@@ -108,7 +109,7 @@ func (h *Handler) serveDebugDERPRegion(w http.ResponseWriter, r *http.Request) {
108109
}
109110

110111
// Check IPv6
111-
addr = net.JoinHostPort(firstNonzero(derpNode.IPv6, derpNode.HostName), strconv.Itoa(port))
112+
addr = net.JoinHostPort(cmp.Or(derpNode.IPv6, derpNode.HostName), strconv.Itoa(port))
112113
conn, err = dialer.DialContext(ctx, "tcp6", addr)
113114
if err != nil {
114115
st.Errors = append(st.Errors, fmt.Sprintf("Error connecting to node %q @ %q over IPv6: %v", derpNode.HostName, addr, err))
@@ -117,7 +118,7 @@ func (h *Handler) serveDebugDERPRegion(w http.ResponseWriter, r *http.Request) {
117118

118119
// Upgrade to TLS and verify that works properly.
119120
tlsConn := tls.Client(conn, &tls.Config{
120-
ServerName: firstNonzero(derpNode.CertName, derpNode.HostName),
121+
ServerName: cmp.Or(derpNode.CertName, derpNode.HostName),
121122
// TODO(andrew-d): we should print more
122123
// detailed failure information on if/why TLS
123124
// verification fails
@@ -166,7 +167,7 @@ func (h *Handler) serveDebugDERPRegion(w http.ResponseWriter, r *http.Request) {
166167
addr = addrs[0]
167168
}
168169

169-
addrPort := netip.AddrPortFrom(addr, uint16(firstNonzero(derpNode.STUNPort, 3478)))
170+
addrPort := netip.AddrPortFrom(addr, uint16(cmp.Or(derpNode.STUNPort, 3478)))
170171

171172
txID := stun.NewTxID()
172173
req := stun.Request(txID)
@@ -292,13 +293,3 @@ func (h *Handler) serveDebugDERPRegion(w http.ResponseWriter, r *http.Request) {
292293
// issued in the first place, tell them specifically that the
293294
// cert is bad not just that the connection failed.
294295
}
295-
296-
func firstNonzero[T comparable](items ...T) T {
297-
var zero T
298-
for _, item := range items {
299-
if item != zero {
300-
return item
301-
}
302-
}
303-
return zero
304-
}

types/lazy/lazy.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -120,41 +120,6 @@ func (z *SyncValue[T]) PeekErr() (v T, err error, ok bool) {
120120
return zero, nil, false
121121
}
122122

123-
// SyncFunc wraps a function to make it lazy.
124-
//
125-
// The returned function calls fill the first time it's called, and returns
126-
// fill's result on every subsequent call.
127-
//
128-
// The returned function is safe for concurrent use.
129-
func SyncFunc[T any](fill func() T) func() T {
130-
var (
131-
once sync.Once
132-
v T
133-
)
134-
return func() T {
135-
once.Do(func() { v = fill() })
136-
return v
137-
}
138-
}
139-
140-
// SyncFuncErr wraps a function to make it lazy.
141-
//
142-
// The returned function calls fill the first time it's called, and returns
143-
// fill's results on every subsequent call.
144-
//
145-
// The returned function is safe for concurrent use.
146-
func SyncFuncErr[T any](fill func() (T, error)) func() (T, error) {
147-
var (
148-
once sync.Once
149-
v T
150-
err error
151-
)
152-
return func() (T, error) {
153-
once.Do(func() { v, err = fill() })
154-
return v, err
155-
}
156-
}
157-
158123
// TB is a subset of testing.TB that we use to set up test helpers.
159124
// It's defined here to avoid pulling in the testing package.
160125
type TB interface {

types/lazy/sync_test.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -354,46 +354,3 @@ func TestSyncValueSetForTest(t *testing.T) {
354354
})
355355
}
356356
}
357-
358-
func TestSyncFunc(t *testing.T) {
359-
f := SyncFunc(fortyTwo)
360-
361-
n := int(testing.AllocsPerRun(1000, func() {
362-
got := f()
363-
if got != 42 {
364-
t.Fatalf("got %v; want 42", got)
365-
}
366-
}))
367-
if n != 0 {
368-
t.Errorf("allocs = %v; want 0", n)
369-
}
370-
}
371-
372-
func TestSyncFuncErr(t *testing.T) {
373-
f := SyncFuncErr(func() (int, error) {
374-
return 42, nil
375-
})
376-
n := int(testing.AllocsPerRun(1000, func() {
377-
got, err := f()
378-
if got != 42 || err != nil {
379-
t.Fatalf("got %v, %v; want 42, nil", got, err)
380-
}
381-
}))
382-
if n != 0 {
383-
t.Errorf("allocs = %v; want 0", n)
384-
}
385-
386-
wantErr := errors.New("test error")
387-
f = SyncFuncErr(func() (int, error) {
388-
return 0, wantErr
389-
})
390-
n = int(testing.AllocsPerRun(1000, func() {
391-
got, err := f()
392-
if got != 0 || err != wantErr {
393-
t.Fatalf("got %v, %v; want 0, %v", got, err, wantErr)
394-
}
395-
}))
396-
if n != 0 {
397-
t.Errorf("allocs = %v; want 0", n)
398-
}
399-
}

util/uniq/slice.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

util/uniq/slice_test.go

Lines changed: 0 additions & 102 deletions
This file was deleted.

version/print.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import (
77
"fmt"
88
"runtime"
99
"strings"
10-
11-
"tailscale.com/types/lazy"
10+
"sync"
1211
)
1312

14-
var stringLazy = lazy.SyncFunc(func() string {
13+
var stringLazy = sync.OnceValue(func() string {
1514
var ret strings.Builder
1615
ret.WriteString(Short())
1716
ret.WriteByte('\n')

version/prop.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"runtime"
1010
"strconv"
1111
"strings"
12+
"sync"
1213

1314
"tailscale.com/tailcfg"
1415
"tailscale.com/types/lazy"
@@ -174,7 +175,7 @@ func IsUnstableBuild() bool {
174175
})
175176
}
176177

177-
var isDev = lazy.SyncFunc(func() bool {
178+
var isDev = sync.OnceValue(func() bool {
178179
return strings.Contains(Short(), "-dev")
179180
})
180181

0 commit comments

Comments
 (0)