From c5c10e04ffe507b48dbd61ee5dbee3276543ebb5 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Fri, 23 Aug 2019 15:13:12 +0300 Subject: [PATCH] Fix lock in the managed routing table --- pkg/router/managed_routing_table.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/router/managed_routing_table.go b/pkg/router/managed_routing_table.go index 6835b56371..0b07363af4 100644 --- a/pkg/router/managed_routing_table.go +++ b/pkg/router/managed_routing_table.go @@ -42,13 +42,14 @@ func (rt *managedRoutingTable) AddRule(rule routing.Rule) (routing.RouteID, erro } func (rt *managedRoutingTable) Rule(routeID routing.RouteID) (routing.Rule, error) { + rt.mu.Lock() + defer rt.mu.Unlock() + rule, err := rt.Table.Rule(routeID) if err != nil { return nil, err } - rt.mu.Lock() - defer rt.mu.Unlock() if rt.ruleIsTimedOut(routeID, rule) { return nil, ErrRuleTimedOut } @@ -59,14 +60,14 @@ func (rt *managedRoutingTable) Rule(routeID routing.RouteID) (routing.Rule, erro func (rt *managedRoutingTable) Cleanup() error { expiredIDs := make([]routing.RouteID, 0) rt.mu.Lock() + defer rt.mu.Unlock() + err := rt.RangeRules(func(routeID routing.RouteID, rule routing.Rule) bool { if rt.ruleIsTimedOut(routeID, rule) { expiredIDs = append(expiredIDs, routeID) } return true }) - rt.mu.Unlock() - if err != nil { return err }