Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit ef4bebb

Browse files
committed
fix: Refactor Register, fix log levels
1 parent e14d71e commit ef4bebb

File tree

4 files changed

+84
-51
lines changed

4 files changed

+84
-51
lines changed

metrics/metricsreporter.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,11 @@ func (m *MetricsReporter) CaptureRegistryMessage(msg ComponentTagged, action str
146146
}
147147

148148
func (m *MetricsReporter) CaptureUnregistryMessage(msg ComponentTagged, action string) {
149-
var componentName string
150-
if msg.Component() == "" {
151-
componentName = "unregistry_message." + action
152-
} else {
153-
componentName = "unregistry_message." + action + "." + msg.Component()
149+
unregisterMsg := "unregistry_message." + action
150+
if msg.Component() != "" {
151+
unregisterMsg = unregisterMsg + "." + msg.Component()
154152
}
155-
m.Batcher.BatchIncrementCounter(componentName)
153+
m.Batcher.BatchIncrementCounter(unregisterMsg)
156154
}
157155

158156
func (m *MetricsReporter) CaptureWebSocketUpdate() {

registry/registry.go

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,42 @@ func (r *RouteRegistry) Register(uri route.Uri, endpoint *route.Endpoint) {
8787
return
8888
}
8989

90-
endpointPutResult := r.register(uri, endpoint)
90+
r.RLock()
91+
defer r.RUnlock()
92+
93+
t := time.Now()
94+
registerRouteResult, pool := r.registerRoute(uri)
95+
if registerRouteResult == route.RouteRegistered {
96+
r.reporter.CaptureRegistryMessage(endpoint, string(route.RouteRegistered))
97+
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
98+
r.logger.Info(string(route.RouteRegistered), buildSlogAttrs(uri, endpoint)...)
99+
}
100+
}
101+
102+
endpointPutResult := r.registerEndpoint(endpoint, pool)
91103

92104
if endpointPutResult == route.EndpointAdded && !endpoint.UpdatedAt.IsZero() {
93105
r.reporter.CaptureRouteRegistrationLatency(time.Since(endpoint.UpdatedAt))
94106
}
95107

96108
r.reporter.CaptureRegistryMessage(endpoint, string(endpointPutResult))
97-
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
98-
r.logger.Info(string(endpointPutResult), buildSlogAttrs(uri, endpoint)...)
109+
110+
switch endpointPutResult {
111+
case route.EndpointAdded:
112+
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
113+
r.logger.Info(string(endpointPutResult), buildSlogAttrs(uri, endpoint)...)
114+
}
115+
default:
116+
if r.logger.Enabled(context.Background(), slog.LevelDebug) {
117+
r.logger.Debug(string(endpointPutResult), buildSlogAttrs(uri, endpoint)...)
118+
}
99119
}
100-
}
101120

102-
func (r *RouteRegistry) register(uri route.Uri, endpoint *route.Endpoint) route.PoolPutResult {
103-
r.RLock()
104-
defer r.RUnlock()
121+
r.SetTimeOfLastUpdate(t)
122+
}
105123

106-
t := time.Now()
124+
func (r *RouteRegistry) registerRoute(uri route.Uri) (route.PoolRegisterRouteResult, *route.EndpointPool) {
125+
poolRegisterRouteResult := route.RouteAlreadyExists
107126
routekey := uri.RouteKey()
108127
pool := r.byURI.Find(routekey)
109128

@@ -112,18 +131,21 @@ func (r *RouteRegistry) register(uri route.Uri, endpoint *route.Endpoint) route.
112131
r.RUnlock()
113132
pool = r.insertRouteKey(routekey, uri)
114133
r.RLock()
134+
poolRegisterRouteResult = route.RouteRegistered
115135
}
136+
return poolRegisterRouteResult, pool
137+
}
116138

139+
func (r *RouteRegistry) registerEndpoint(endpoint *route.Endpoint, pool *route.EndpointPool) route.PoolRegisterEndpointResult {
117140
if endpoint.StaleThreshold > r.dropletStaleThreshold || endpoint.StaleThreshold == 0 {
118141
endpoint.StaleThreshold = r.dropletStaleThreshold
119142
}
120143

121-
endpointAdded := pool.Put(endpoint)
144+
endpointAddedResult := pool.Put(endpoint)
122145
// Overwrites the load balancing algorithm of a pool by that of a specified endpoint, if that is valid.
123146
pool.SetPoolLoadBalancingAlgorithm(endpoint)
124-
r.SetTimeOfLastUpdate(t)
125147

126-
return endpointAdded
148+
return endpointAddedResult
127149
}
128150

129151
// insertRouteKey acquires a write lock, inserts the route key into the registry and releases the write lock.
@@ -144,7 +166,7 @@ func (r *RouteRegistry) insertRouteKey(routekey route.Uri, uri route.Uri) *route
144166
LoadBalancingAlgorithm: r.DefaultLoadBalancingAlgorithm,
145167
})
146168
r.byURI.Insert(routekey, pool)
147-
r.logger.Info("route-registered", slog.Any("uri", routekey))
169+
r.logger.Info(string(route.RouteRegistered), slog.Any("uri", routekey))
148170
// for backward compatibility:
149171
r.logger.Debug("uri-added", slog.Any("uri", routekey))
150172
}
@@ -156,6 +178,9 @@ func (r *RouteRegistry) Unregister(uri route.Uri, endpoint *route.Endpoint) {
156178
return
157179
}
158180

181+
r.Lock()
182+
defer r.Unlock()
183+
159184
routeKey := uri.RouteKey()
160185
endpointUnregisteredResult, pool := r.unregisterEndpoint(routeKey, endpoint)
161186
if pool == nil {
@@ -178,9 +203,6 @@ func (r *RouteRegistry) Unregister(uri route.Uri, endpoint *route.Endpoint) {
178203
}
179204

180205
func (r *RouteRegistry) unregisterEndpoint(routeKey route.Uri, endpoint *route.Endpoint) (route.PoolRemoveEndpointResult, *route.EndpointPool) {
181-
r.Lock()
182-
defer r.Unlock()
183-
184206
pool := r.byURI.Find(routeKey)
185207
if pool == nil {
186208
return route.EndpointNotUnregistered, nil
@@ -189,16 +211,9 @@ func (r *RouteRegistry) unregisterEndpoint(routeKey route.Uri, endpoint *route.E
189211
}
190212

191213
func (r *RouteRegistry) deleteRouteWithoutEndpoint(routeKey route.Uri, pool *route.EndpointPool) route.PoolRemoveRouteResult {
192-
r.Lock()
193-
defer r.Unlock()
194-
195214
if pool.IsEmpty() {
196-
if r.EmptyPoolResponseCode503 && r.EmptyPoolTimeout > 0 {
197-
if time.Since(pool.LastUpdated()) > r.EmptyPoolTimeout {
198-
r.byURI.Delete(routeKey)
199-
return route.RouteUnregistered
200-
}
201-
} else {
215+
if !(r.EmptyPoolResponseCode503 && r.EmptyPoolTimeout > 0) ||
216+
(r.EmptyPoolResponseCode503 && r.EmptyPoolTimeout > 0 && time.Since(pool.LastUpdated()) > r.EmptyPoolTimeout) {
202217
r.byURI.Delete(routeKey)
203218
return route.RouteUnregistered
204219
}

registry/registry_test.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ var _ = Describe("RouteRegistry", func() {
7373
Context("when a new endpoint is registered", func() {
7474
It("emits endpoint-registered message_count metrics", func() {
7575
r.Register("foo", fooEndpoint)
76-
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(1))
76+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
7777
endpoint1, action1 := reporter.CaptureRegistryMessageArgsForCall(0)
7878
Expect(endpoint1).To(Equal(fooEndpoint))
79-
Expect(action1).To(Equal("endpoint-added"))
79+
Expect(action1).To(Equal(string(route.RouteRegistered)))
80+
endpoint2, action2 := reporter.CaptureRegistryMessageArgsForCall(1)
81+
Expect(endpoint2).To(Equal(fooEndpoint))
82+
Expect(action2).To(Equal(string(route.EndpointAdded)))
8083
})
8184
})
8285

@@ -88,13 +91,16 @@ var _ = Describe("RouteRegistry", func() {
8891
endpoint2 := route.NewEndpoint(&route.EndpointOpts{ModificationTag: modTag2})
8992
r.Register("foo", endpoint1)
9093
r.Register("foo", endpoint2)
91-
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
94+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(3))
9295
endpointR1, action1 := reporter.CaptureRegistryMessageArgsForCall(0)
9396
Expect(endpointR1).To(Equal(endpoint1))
94-
Expect(action1).To(Equal("endpoint-added"))
97+
Expect(action1).To(Equal(string(route.RouteRegistered)))
9598
endpointR2, action2 := reporter.CaptureRegistryMessageArgsForCall(1)
96-
Expect(endpointR2).To(Equal(endpoint2))
97-
Expect(action2).To(Equal("endpoint-updated"))
99+
Expect(endpointR2).To(Equal(endpoint1))
100+
Expect(action2).To(Equal(string(route.EndpointAdded)))
101+
endpointR3, action3 := reporter.CaptureRegistryMessageArgsForCall(2)
102+
Expect(endpointR3).To(Equal(endpoint2))
103+
Expect(action3).To(Equal(string(route.EndpointUpdated)))
98104
})
99105
})
100106

@@ -106,13 +112,16 @@ var _ = Describe("RouteRegistry", func() {
106112
endpoint2 := route.NewEndpoint(&route.EndpointOpts{ModificationTag: modTag2})
107113
r.Register("foo", endpoint1)
108114
r.Register("foo", endpoint2)
109-
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
115+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(3))
110116
endpointR1, action1 := reporter.CaptureRegistryMessageArgsForCall(0)
111117
Expect(endpointR1).To(Equal(endpoint1))
112-
Expect(action1).To(Equal("endpoint-added"))
118+
Expect(action1).To(Equal(string(route.RouteRegistered)))
113119
endpointR2, action2 := reporter.CaptureRegistryMessageArgsForCall(1)
114-
Expect(endpointR2).To(Equal(endpoint2))
115-
Expect(action2).To(Equal("endpoint-not-updated"))
120+
Expect(endpointR2).To(Equal(endpoint1))
121+
Expect(action2).To(Equal(string(route.EndpointAdded)))
122+
endpointR3, action3 := reporter.CaptureRegistryMessageArgsForCall(2)
123+
Expect(endpointR3).To(Equal(endpoint2))
124+
Expect(action3).To(Equal(string(route.EndpointNotUpdated)))
116125
})
117126
})
118127

@@ -562,33 +571,36 @@ var _ = Describe("RouteRegistry", func() {
562571
BeforeEach(func() {
563572
fooEndpoint.Tags = map[string]string{"component": "oauth-server"}
564573
r.Register("foo", fooEndpoint)
574+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
565575
})
576+
566577
It("emits counter metrics for unregister endpoint and route", func() {
567578
r.Unregister("foo", fooEndpoint)
568579
Expect(reporter.CaptureUnregistryMessageCallCount()).To(Equal(2))
569580
endpoint1, action1 := reporter.CaptureUnregistryMessageArgsForCall(0)
570581
Expect(endpoint1).To(Equal(fooEndpoint))
571-
Expect(action1).To(Equal("endpoint-unregistered"))
582+
Expect(action1).To(Equal(string(route.EndpointUnregistered)))
572583
endpoint2, action2 := reporter.CaptureUnregistryMessageArgsForCall(1)
573584
Expect(endpoint2).To(Equal(fooEndpoint))
574-
Expect(action2).To(Equal("route-unregistered"))
585+
Expect(action2).To(Equal(string(route.RouteUnregistered)))
575586
})
576587
})
577588

578589
Context("when endpoint does not have component tag", func() {
579590
BeforeEach(func() {
580591
fooEndpoint.Tags = map[string]string{}
581592
r.Register("foo", fooEndpoint)
593+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
582594
})
583595
It("emits counter metrics for unregister endpoint and route", func() {
584596
r.Unregister("foo", fooEndpoint)
585597
Expect(reporter.CaptureUnregistryMessageCallCount()).To(Equal(2))
586598
endpoint1, action1 := reporter.CaptureUnregistryMessageArgsForCall(0)
587599
Expect(endpoint1).To(Equal(fooEndpoint))
588-
Expect(action1).To(Equal("endpoint-unregistered"))
600+
Expect(action1).To(Equal(string(route.EndpointUnregistered)))
589601
endpoint2, action2 := reporter.CaptureUnregistryMessageArgsForCall(1)
590602
Expect(endpoint2).To(Equal(fooEndpoint))
591-
Expect(action2).To(Equal("route-unregistered"))
603+
Expect(action2).To(Equal(string(route.RouteUnregistered)))
592604
})
593605
})
594606
})
@@ -604,13 +616,14 @@ var _ = Describe("RouteRegistry", func() {
604616

605617
r.Register("foo", fooEndpoint)
606618
r.Register("foo", fooEndpoint2)
619+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(3))
607620
})
608621
It("emits counter metrics for unregister endpoint only", func() {
609622
r.Unregister("foo", fooEndpoint)
610623
Expect(reporter.CaptureUnregistryMessageCallCount()).To(Equal(1))
611624
endpoint1, action1 := reporter.CaptureUnregistryMessageArgsForCall(0)
612625
Expect(endpoint1).To(Equal(fooEndpoint))
613-
Expect(action1).To(Equal("endpoint-unregistered"))
626+
Expect(action1).To(Equal(string(route.EndpointUnregistered)))
614627
})
615628
})
616629
Context("when route is not registered", func() {

route/pool.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ type Counter struct {
2020
value int64
2121
}
2222

23-
type PoolPutResult string
23+
type PoolRegisterEndpointResult string
2424

2525
const (
26-
EndpointNotUpdated PoolPutResult = "endpoint-not-updated"
27-
EndpointUpdated PoolPutResult = "endpoint-updated"
28-
EndpointAdded PoolPutResult = "endpoint-added"
26+
EndpointNotUpdated PoolRegisterEndpointResult = "endpoint-not-updated"
27+
EndpointUpdated PoolRegisterEndpointResult = "endpoint-updated"
28+
EndpointAdded PoolRegisterEndpointResult = "endpoint-added"
29+
)
30+
31+
type PoolRegisterRouteResult string
32+
33+
const (
34+
RouteRegistered PoolRegisterRouteResult = "route-registered"
35+
RouteAlreadyExists PoolRegisterRouteResult = "route-already-exists"
2936
)
3037

3138
type PoolRemoveEndpointResult string
@@ -268,11 +275,11 @@ func (p *EndpointPool) Update() {
268275
p.updatedAt = time.Now()
269276
}
270277

271-
func (p *EndpointPool) Put(endpoint *Endpoint) PoolPutResult {
278+
func (p *EndpointPool) Put(endpoint *Endpoint) PoolRegisterEndpointResult {
272279
p.Lock()
273280
defer p.Unlock()
274281

275-
var result PoolPutResult
282+
var result PoolRegisterEndpointResult
276283
e, found := p.index[endpoint.CanonicalAddr()]
277284
if found {
278285
result = EndpointUpdated

0 commit comments

Comments
 (0)