Skip to content

Commit e0b4ef6

Browse files
Merge pull request #8993 from smarterclayton/hold_router_lock
o Add locking around ops that change state. Its better to go lockless eventually and use a "shadow" (cloned) copy of the config when we do a reload. o Fixes as per @smarterclayton review comments.
2 parents 03a25f7 + 1406447 commit e0b4ef6

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

pkg/router/template/router.go

+37-7
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ func (r *templateRouter) reloadRouter() error {
312312
}
313313

314314
func (r *templateRouter) FilterNamespaces(namespaces sets.String) {
315+
r.lock.Lock()
316+
defer r.lock.Unlock()
317+
315318
if len(namespaces) == 0 {
316319
r.state = make(map[string]ServiceUnit)
317320
}
@@ -334,34 +337,54 @@ func (r *templateRouter) CreateServiceUnit(id string) {
334337
EndpointTable: []Endpoint{},
335338
}
336339

340+
r.lock.Lock()
341+
defer r.lock.Unlock()
342+
337343
r.state[id] = service
338344
}
339345

340-
// FindServiceUnit finds the service with the given id.
341-
func (r *templateRouter) FindServiceUnit(id string) (ServiceUnit, bool) {
346+
// findMatchingServiceUnit finds the service with the given id - internal
347+
// lockless form, caller needs to ensure lock acquisition [and release].
348+
func (r *templateRouter) findMatchingServiceUnit(id string) (ServiceUnit, bool) {
342349
v, ok := r.state[id]
343350
return v, ok
344351
}
345352

353+
// FindServiceUnit finds the service with the given id.
354+
func (r *templateRouter) FindServiceUnit(id string) (ServiceUnit, bool) {
355+
r.lock.Lock()
356+
defer r.lock.Unlock()
357+
358+
return r.findMatchingServiceUnit(id)
359+
}
360+
346361
// DeleteServiceUnit deletes the service with the given id.
347362
func (r *templateRouter) DeleteServiceUnit(id string) {
348-
svcUnit, ok := r.FindServiceUnit(id)
363+
r.lock.Lock()
364+
defer r.lock.Unlock()
365+
366+
svcUnit, ok := r.findMatchingServiceUnit(id)
349367
if !ok {
350368
return
351369
}
352370

353371
for _, cfg := range svcUnit.ServiceAliasConfigs {
354372
r.cleanUpServiceAliasConfig(&cfg)
355373
}
374+
356375
delete(r.state, id)
357376
}
358377

359378
// DeleteEndpoints deletes the endpoints for the service with the given id.
360379
func (r *templateRouter) DeleteEndpoints(id string) {
361-
service, ok := r.FindServiceUnit(id)
380+
r.lock.Lock()
381+
defer r.lock.Unlock()
382+
383+
service, ok := r.findMatchingServiceUnit(id)
362384
if !ok {
363385
return
364386
}
387+
365388
service.EndpointTable = []Endpoint{}
366389

367390
r.state[id] = service
@@ -391,8 +414,6 @@ func (r *templateRouter) routeKey(route *routeapi.Route) string {
391414

392415
// AddRoute adds a route for the given id
393416
func (r *templateRouter) AddRoute(id string, route *routeapi.Route, host string) bool {
394-
frontend, _ := r.FindServiceUnit(id)
395-
396417
backendKey := r.routeKey(route)
397418

398419
config := ServiceAliasConfig{
@@ -450,6 +471,10 @@ func (r *templateRouter) AddRoute(id string, route *routeapi.Route, host string)
450471
}
451472
}
452473

474+
r.lock.Lock()
475+
defer r.lock.Unlock()
476+
477+
frontend, _ := r.findMatchingServiceUnit(id)
453478
//create or replace
454479
frontend.ServiceAliasConfigs[backendKey] = config
455480
r.state[id] = frontend
@@ -478,6 +503,9 @@ func (r *templateRouter) cleanUpdates(frontendKey string, backendKey string) {
478503

479504
// RemoveRoute removes the given route for the given id.
480505
func (r *templateRouter) RemoveRoute(id string, route *routeapi.Route) {
506+
r.lock.Lock()
507+
defer r.lock.Unlock()
508+
481509
serviceUnit, ok := r.state[id]
482510
if !ok {
483511
return
@@ -494,7 +522,9 @@ func (r *templateRouter) RemoveRoute(id string, route *routeapi.Route) {
494522

495523
// AddEndpoints adds new Endpoints for the given id.
496524
func (r *templateRouter) AddEndpoints(id string, endpoints []Endpoint) bool {
497-
frontend, _ := r.FindServiceUnit(id)
525+
r.lock.Lock()
526+
defer r.lock.Unlock()
527+
frontend, _ := r.findMatchingServiceUnit(id)
498528

499529
//only make the change if there is a difference
500530
if reflect.DeepEqual(frontend.EndpointTable, endpoints) {

0 commit comments

Comments
 (0)