Skip to content

Commit

Permalink
routing: avoid allocating new entry in EndpointRegistry.GetMetrics (#…
Browse files Browse the repository at this point in the history
…2847)

EndpointRegistry.GetMetrics always allocates new entry even when
data for the key already exists.

sync.Map does not provide lazy initialization for the new value,
see golang/go#44159

This change uses suggested best-effort to avoid allocating
a new value on each load.

Follow up on #2795

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Jan 12, 2024
1 parent 4d76a05 commit d238294
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions routing/endpointregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ func newEntry() *entry {
type EndpointRegistry struct {
lastSeenTimeout time.Duration
now func() time.Time

// map[string]*entry
data sync.Map
data sync.Map // map[string]*entry
}

var _ PostProcessor = &EndpointRegistry{}
Expand Down Expand Up @@ -129,7 +127,11 @@ func NewEndpointRegistry(o RegistryOptions) *EndpointRegistry {
}
}

func (r *EndpointRegistry) GetMetrics(key string) Metrics {
e, _ := r.data.LoadOrStore(key, newEntry())
func (r *EndpointRegistry) GetMetrics(hostPort string) Metrics {
// https://github.com/golang/go/issues/44159#issuecomment-780774977
e, ok := r.data.Load(hostPort)
if !ok {
e, _ = r.data.LoadOrStore(hostPort, newEntry())
}
return e.(*entry)
}

0 comments on commit d238294

Please sign in to comment.