util/slicesx: add package for generic slice functions, use
Now that we're using rand.Shuffle in a few locations, create a generic shuffle function and use it instead. While we're at it, move the interleaveSlices function to the same package for use. Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: I0b00920e5b3eea846b6cedc30bd34d978a049fd3
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"tailscale.com/types/logger"
|
||||
"tailscale.com/util/cloudenv"
|
||||
"tailscale.com/util/singleflight"
|
||||
"tailscale.com/util/slicesx"
|
||||
)
|
||||
|
||||
var zaddr netip.Addr
|
||||
@@ -577,7 +578,7 @@ func (dc *dialCall) raceDial(ctx context.Context, ips []netip.Addr) (net.Conn, e
|
||||
iv4 = append(iv4, ip)
|
||||
}
|
||||
}
|
||||
ips = interleaveSlices(iv6, iv4)
|
||||
ips = slicesx.Interleave(iv6, iv4)
|
||||
|
||||
go func() {
|
||||
for i, ip := range ips {
|
||||
@@ -636,21 +637,6 @@ func (dc *dialCall) raceDial(ctx context.Context, ips []netip.Addr) (net.Conn, e
|
||||
}
|
||||
}
|
||||
|
||||
// interleaveSlices combines two slices of the form [a, b, c] and [x, y, z]
|
||||
// into a slice with elements interleaved; i.e. [a, x, b, y, c, z].
|
||||
func interleaveSlices[T any](a, b []T) []T {
|
||||
var (
|
||||
i int
|
||||
ret = make([]T, 0, len(a)+len(b))
|
||||
)
|
||||
for i = 0; i < len(a) && i < len(b); i++ {
|
||||
ret = append(ret, a[i], b[i])
|
||||
}
|
||||
ret = append(ret, a[i:]...)
|
||||
ret = append(ret, b[i:]...)
|
||||
return ret
|
||||
}
|
||||
|
||||
func v4addrs(aa []netip.Addr) (ret []netip.Addr) {
|
||||
for _, a := range aa {
|
||||
a = a.Unmap()
|
||||
|
||||
@@ -141,30 +141,6 @@ func TestResolverAllHostStaticResult(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInterleaveSlices(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
a, b []int
|
||||
want []int
|
||||
}{
|
||||
{name: "equal", a: []int{1, 3, 5}, b: []int{2, 4, 6}, want: []int{1, 2, 3, 4, 5, 6}},
|
||||
{name: "short_b", a: []int{1, 3, 5}, b: []int{2, 4}, want: []int{1, 2, 3, 4, 5}},
|
||||
{name: "short_a", a: []int{1, 3}, b: []int{2, 4, 6}, want: []int{1, 2, 3, 4, 6}},
|
||||
{name: "len_1", a: []int{1}, b: []int{2, 4, 6}, want: []int{1, 2, 4, 6}},
|
||||
{name: "nil_a", a: nil, b: []int{2, 4, 6}, want: []int{2, 4, 6}},
|
||||
{name: "nil_all", a: nil, b: nil, want: []int{}},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
merged := interleaveSlices(tc.a, tc.b)
|
||||
if !reflect.DeepEqual(merged, tc.want) {
|
||||
t.Errorf("got %v; want %v", merged, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldTryBootstrap(t *testing.T) {
|
||||
oldDebug := debug
|
||||
t.Cleanup(func() { debug = oldDebug })
|
||||
|
||||
Reference in New Issue
Block a user