Skip to content

Commit 271fb26

Browse files
Fabian Hollerfho
authored andcommitted
replace reflect.DeepEqual() with addressesEqual() function
Instead of using reflect.DeepEqual() implement and use a function to compare 2 resolver.Address slices. The function is more specific and simple then reflect.DeepEqual() and therefore faster.
1 parent 6c4a3d6 commit 271fb26

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

consul/resolver.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"reflect"
87
"sort"
98
"sync"
109
"time"
@@ -100,6 +99,8 @@ func (c *consulResolver) query(opts *consul.QueryOptions) ([]resolver.Address, u
10099

101100
result := make([]resolver.Address, 0, len(entries))
102101
for _, e := range entries {
102+
// when additionals fields are set in addr, addressesEqual()
103+
// must be updated to honour them
103104
addr := e.Service.Address
104105
if addr == "" {
105106
addr = e.Node.Address
@@ -114,8 +115,6 @@ func (c *consulResolver) query(opts *consul.QueryOptions) ([]resolver.Address, u
114115
})
115116
}
116117

117-
sortAddresses(result)
118-
119118
if grpclog.V(1) {
120119
grpclog.Infof("grpcconsulresolver: service '%s' resolved to '%+v'", c.service, result)
121120
}
@@ -142,10 +141,26 @@ func filterPreferOnlyHealthy(entries []*consul.ServiceEntry) []*consul.ServiceEn
142141
return entries
143142
}
144143

145-
func sortAddresses(addrs []resolver.Address) {
146-
sort.Slice(addrs, func(i, j int) bool {
147-
return addrs[i].Addr < addrs[j].Addr
148-
})
144+
func addressesEqual(a, b []resolver.Address) bool {
145+
if a == nil && b != nil {
146+
return false
147+
}
148+
149+
if a != nil && b == nil {
150+
return false
151+
}
152+
153+
if len(a) != len(b) {
154+
return false
155+
}
156+
157+
for i := range a {
158+
if a[i].Addr != b[i].Addr {
159+
return false
160+
}
161+
}
162+
163+
return true
149164
}
150165

151166
func (c *consulResolver) watcher() {
@@ -178,6 +193,10 @@ func (c *consulResolver) watcher() {
178193
break
179194
}
180195

196+
sort.Slice(addrs, func(i, j int) bool {
197+
return addrs[i].Addr < addrs[j].Addr
198+
})
199+
181200
// query() blocks until a consul internal timeout expired or
182201
// data newer then the passed opts.WaitIndex is available.
183202
// We check if the returned addrs changed to not call
@@ -187,7 +206,8 @@ func (c *consulResolver) watcher() {
187206
// addresses (addrs is nil), we have to report an empty
188207
// set of resolved addresses. It informs the grpc-balancer that resolution is not
189208
// in progress anymore and grpc calls can failFast.
190-
if reflect.DeepEqual(addrs, lastReportedAddrs) {
209+
// TODO: replace DeepEqual with a custom Cmp function
210+
if addressesEqual(addrs, lastReportedAddrs) {
191211
// If the consul server responds with
192212
// the same data then in the last
193213
// query in less then 50ms, we sleep a

0 commit comments

Comments
 (0)