Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Fix typo and optimize cache overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri Shkuro committed Mar 3, 2017
1 parent e1df9f2 commit adc742b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
15 changes: 12 additions & 3 deletions rpcmetrics/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@ func newNormalizedEndpoints(maxSize int, normalizer NameNormalizer) *normalizedE
// names it returns "" for all other names beyond those already cached.
func (n *normalizedEndpoints) normalize(name string) string {
n.mux.RLock()
norm := n.names[name]
norm, ok := n.names[name]
l := len(n.names)
n.mux.RUnlock()
if norm != "" {
if ok {
return norm
}
norm = n.normalizer.Normalize(name)
if l >= n.maxSize {
return ""
}
return n.normalizeWithLock(name)
}

func (n *normalizedEndpoints) normalizeWithLock(name string) string {
norm := n.normalizer.Normalize(name)
n.mux.Lock()
defer n.mux.Unlock()
// cache may have grown while we were not holding the lock
if len(n.names) >= n.maxSize {
return ""
}
Expand Down
7 changes: 7 additions & 0 deletions rpcmetrics/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ func TestNormalizedEndpoints(t *testing.T) {
assert.Equal(t, "ab-cd", n.normalize("ab^cd"), "cache hit")
assertLen(1)
assert.Equal(t, "", n.normalize("xys"), "cache overflow")
assertLen(1)
}

func TestNormalizedEndpointsDoubleLocking(t *testing.T) {
n := newNormalizedEndpoints(1, DefaultNameNormalizer)
assert.Equal(t, "ab-cd", n.normalize("ab^cd"), "fill out the cache")
assert.Equal(t, "", n.normalizeWithLock("xys"), "cache overflow")
}
2 changes: 1 addition & 1 deletion rpcmetrics/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Observer struct {
metricsByEndpoint *MetricsByEndpoint
}

// NewObserver creates a new observer that can emit RPC metricso.
// NewObserver creates a new observer that can emit RPC metrics.
func NewObserver(metricsFactory metrics.Factory, normalizer NameNormalizer) *Observer {
return &Observer{
metricsByEndpoint: newMetricsByEndpoint(
Expand Down

0 comments on commit adc742b

Please sign in to comment.