Skip to content

Commit 1668d8b

Browse files
maxmoehlhoffmaenSoha-Albaghdady
committed
refactor: move route insertion log
Co-Authored-By: Clemens Hoffmann <[email protected]> Co-Authored-By: Soha Alboghdady <[email protected]>
1 parent fe28d7b commit 1668d8b

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/code.cloudfoundry.org/gorouter/registry/registry.go

+30-23
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,20 @@ func (r *RouteRegistry) Register(uri route.Uri, endpoint *route.Endpoint) {
8888
return
8989
}
9090

91-
poolPutResult := r.register(uri, endpoint)
91+
poolPutResult, routeAdded := r.register(uri, endpoint)
9292

9393
r.reporter.CaptureRegistryMessage(endpoint, poolPutResult.String())
9494

9595
if poolPutResult == route.EndpointAdded && !endpoint.UpdatedAt.IsZero() {
9696
r.reporter.CaptureRouteRegistrationLatency(time.Since(endpoint.UpdatedAt))
9797
}
9898

99+
if routeAdded {
100+
r.logger.Info("route-registered", slog.Any("uri", uri))
101+
// for backward compatibility:
102+
r.logger.Debug("uri-added", slog.Any("uri", uri))
103+
}
104+
99105
switch poolPutResult {
100106
case route.EndpointAdded:
101107
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
@@ -116,7 +122,7 @@ func (r *RouteRegistry) Register(uri route.Uri, endpoint *route.Endpoint) {
116122
}
117123
}
118124

119-
func (r *RouteRegistry) register(uri route.Uri, endpoint *route.Endpoint) route.PoolPutResult {
125+
func (r *RouteRegistry) register(uri route.Uri, endpoint *route.Endpoint) (putResult route.PoolPutResult, routeAdded bool) {
120126
r.RLock()
121127
defer r.RUnlock()
122128

@@ -127,44 +133,45 @@ func (r *RouteRegistry) register(uri route.Uri, endpoint *route.Endpoint) route.
127133
if pool == nil {
128134
// release read lock, insertRouteKey() will acquire a write lock.
129135
r.RUnlock()
130-
pool = r.insertRouteKey(routekey, uri)
136+
pool, routeAdded = r.insertRouteKey(routekey, uri)
131137
r.RLock()
132138
}
133139

134140
if endpoint.StaleThreshold > r.dropletStaleThreshold || endpoint.StaleThreshold == 0 {
135141
endpoint.StaleThreshold = r.dropletStaleThreshold
136142
}
137143

138-
poolPutResult := pool.Put(endpoint)
144+
putResult = pool.Put(endpoint)
139145
// Overwrites the load balancing algorithm of a pool by that of a specified endpoint, if that is valid.
140146
r.SetTimeOfLastUpdate(t)
141147

142-
return poolPutResult
148+
return putResult, routeAdded
143149
}
144150

145-
// insertRouteKey acquires a write lock, inserts the route key into the registry and releases the write lock.
146-
func (r *RouteRegistry) insertRouteKey(routekey route.Uri, uri route.Uri) *route.EndpointPool {
151+
// insertRouteKey acquires a write lock, inserts the route key into the registry and releases the
152+
// write lock. If a pool already exists it returns that instead.
153+
func (r *RouteRegistry) insertRouteKey(routekey route.Uri, uri route.Uri) (pool *route.EndpointPool, poolAdded bool) {
147154
r.Lock()
148155
defer r.Unlock()
149156

150157
// double check that the route key is still not found, now with the write lock.
151-
pool := r.byURI.Find(routekey)
152-
if pool == nil {
153-
host, contextPath := splitHostAndContextPath(uri)
154-
pool = route.NewPool(&route.PoolOpts{
155-
Logger: r.logger,
156-
RetryAfterFailure: r.dropletStaleThreshold / 4,
157-
Host: host,
158-
ContextPath: contextPath,
159-
MaxConnsPerBackend: r.maxConnsPerBackend,
160-
LoadBalancingAlgorithm: r.DefaultLoadBalancingAlgorithm,
161-
})
162-
r.byURI.Insert(routekey, pool)
163-
r.logger.Info("route-registered", slog.Any("uri", routekey))
164-
// for backward compatibility:
165-
r.logger.Debug("uri-added", slog.Any("uri", routekey))
158+
pool = r.byURI.Find(routekey)
159+
if pool != nil {
160+
return pool, false
166161
}
167-
return pool
162+
163+
host, contextPath := splitHostAndContextPath(uri)
164+
pool = route.NewPool(&route.PoolOpts{
165+
Logger: r.logger,
166+
RetryAfterFailure: r.dropletStaleThreshold / 4,
167+
Host: host,
168+
ContextPath: contextPath,
169+
MaxConnsPerBackend: r.maxConnsPerBackend,
170+
LoadBalancingAlgorithm: r.DefaultLoadBalancingAlgorithm,
171+
})
172+
r.byURI.Insert(routekey, pool)
173+
174+
return pool, true
168175
}
169176

170177
func (r *RouteRegistry) Unregister(uri route.Uri, endpoint *route.Endpoint) {

0 commit comments

Comments
 (0)