Skip to content

Commit 64da2bc

Browse files
feat: address Evan's comment to require Equal method on cached results
1 parent 8fc2a4c commit 64da2bc

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

nmagent/equality.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package nmagent
2+
3+
// Equal compares two Interfaces objects for equality.
4+
func (i Interfaces) Equal(other Interfaces) bool {
5+
if len(i.Entries) != len(other.Entries) {
6+
return false
7+
}
8+
for idx, entry := range i.Entries {
9+
if !entry.Equal(other.Entries[idx]) {
10+
return false
11+
}
12+
}
13+
return true
14+
}
15+
16+
// Equal compares two Interface objects for equality.
17+
func (i Interface) Equal(other Interface) bool {
18+
if len(i.InterfaceSubnets) != len(other.InterfaceSubnets) {
19+
return false
20+
}
21+
for idx, subnet := range i.InterfaceSubnets {
22+
if !subnet.Equal(other.InterfaceSubnets[idx]) {
23+
return false
24+
}
25+
}
26+
if i.IsPrimary != other.IsPrimary || !i.MacAddress.Equal(other.MacAddress) {
27+
return false
28+
}
29+
return true
30+
}
31+
32+
// Equal compares two InterfaceSubnet objects for equality.
33+
func (s InterfaceSubnet) Equal(other InterfaceSubnet) bool {
34+
if len(s.IPAddress) != len(other.IPAddress) {
35+
return false
36+
}
37+
if s.Prefix != other.Prefix {
38+
return false
39+
}
40+
for idx, ip := range s.IPAddress {
41+
if !ip.Equal(other.IPAddress[idx]) {
42+
return false
43+
}
44+
}
45+
return true
46+
}
47+
48+
// Equal compares two NodeIP objects for equality.
49+
func (ip NodeIP) Equal(other NodeIP) bool {
50+
return ip.IsPrimary == other.IsPrimary && ip.Address.Equal(other.Address)
51+
}

nmagent/macaddress.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ const (
1414

1515
type MACAddress net.HardwareAddr
1616

17+
func (h MACAddress) Equal(other MACAddress) bool {
18+
if len(h) != len(other) {
19+
return false
20+
}
21+
for i := range h {
22+
if h[i] != other[i] {
23+
return false
24+
}
25+
}
26+
return true
27+
}
28+
1729
func (h *MACAddress) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
1830
var macStr string
1931
if err := d.DecodeElement(&macStr, &start); err != nil {

refresh/fetcher.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package refresh
33
import (
44
"context"
55
"time"
6-
7-
"github.com/google/go-cmp/cmp"
86
)
97

108
const (
@@ -17,7 +15,7 @@ const (
1715
// When a diff is observed, the interval resets to the minimum. The interval can be made unchanging by setting
1816
// minInterval and maxInterval to the same desired value.
1917

20-
type Fetcher[T any] struct {
18+
type Fetcher[T Equaler[T]] struct {
2119
fetchFunc func(context.Context) (T, error)
2220
cache T
2321
minInterval time.Duration
@@ -29,7 +27,7 @@ type Fetcher[T any] struct {
2927
}
3028

3129
// NewFetcher creates a new Fetcher. If minInterval is 0, it will default to 4 seconds.
32-
func NewFetcher[T any](
30+
func NewFetcher[T Equaler[T]](
3331
fetchFunc func(context.Context) (T, error),
3432
minInterval time.Duration,
3533
maxInterval time.Duration,
@@ -87,7 +85,7 @@ func (f *Fetcher[T]) Start(ctx context.Context) {
8785
if err != nil {
8886
f.logger.Errorf("Error fetching data: %v", err)
8987
} else {
90-
if cmp.Equal(result, f.cache) {
88+
if result.Equal(f.cache) {
9189
f.updateFetchIntervalForNoObservedDiff()
9290
f.logger.Printf("No diff observed in fetch, not invoking the consumer")
9391
} else {

0 commit comments

Comments
 (0)