Skip to content

Commit

Permalink
home: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Jan 11, 2024
1 parent 855d320 commit 9412f58
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
27 changes: 13 additions & 14 deletions internal/home/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,31 +188,30 @@ func (c *persistentClient) idsLen() (n int) {
return len(c.IPs) + len(c.Subnets) + len(c.MACs) + len(c.ClientIDs)
}

// compareIDs returns true if the ids of the current and previous clients are
// the same.
func (c *persistentClient) compareIDs(prev *persistentClient) (equal bool) {
n := slices.CompareFunc(c.IPs, prev.IPs, netip.Addr.Compare)
if n != 0 {
// equalIDs returns true if the ids of the current and previous clients are the
// same.
func (c *persistentClient) equalIDs(prev *persistentClient) (equal bool) {
if !slices.Equal(c.IPs, prev.IPs) {
return false
}

n = slices.CompareFunc(c.Subnets, prev.Subnets, func(a, b netip.Prefix) int {
return strings.Compare(a.String(), b.String())
})

if n != 0 {
if !slices.Equal(c.Subnets, prev.Subnets) {
return false
}

n = slices.CompareFunc(c.MACs, prev.MACs, func(a, b net.HardwareAddr) int {
return strings.Compare(a.String(), b.String())
eq := slices.EqualFunc(c.MACs, prev.MACs, func(a, b net.HardwareAddr) bool {
return a.String() == b.String()
})

if n != 0 {
if !eq {
return false
}

if !slices.Equal(c.ClientIDs, prev.ClientIDs) {
return false
}

return slices.Compare(c.ClientIDs, prev.ClientIDs) == 0
return true
}

// shallowClone returns a deep copy of the client, except upstreamConfig,
Expand Down
4 changes: 2 additions & 2 deletions internal/home/client_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestPersistentClient_CompareIDs(t *testing.T) {
func TestPersistentClient_EqualIDs(t *testing.T) {
const (
ip = "0.0.0.0"
ip1 = "1.1.1.1"
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestPersistentClient_CompareIDs(t *testing.T) {
err = prev.setIDs(tc.prevIDs)
require.Nil(t, err)

equal := c.compareIDs(prev)
equal := c.equalIDs(prev)
assert.Equal(t, tc.want, equal)
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ func (clients *clientsContainer) update(prev, c *persistentClient) (err error) {
}
}

if c.compareIDs(prev) {
if c.equalIDs(prev) {
clients.removeLocked(prev)
clients.addLocked(c)

Expand Down

0 comments on commit 9412f58

Please sign in to comment.