Skip to content

Refactor router to fix api data race - #6678

Open
Fangliding wants to merge 1 commit into
mainfrom
route-api-data-race
Open

Refactor router to fix api data race#6678
Fangliding wants to merge 1 commit into
mainfrom
route-api-data-race

Conversation

@Fangliding

Copy link
Copy Markdown
Member

修复API调整路由时的竞争问题 顺便简化逻辑

Comment on lines 10 to 19
var b *Balancer
for tag, bl := range r.balancers {
for tag, bl := range *r.balancers.Load() {
if tag == balancer {
b = bl
break
}
}
if b == nil {
return errors.New("balancer '", balancer, "' not found")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do this instead:

b, ok := (*r.balancers.Load())[balancer]
if !ok {
    return errors.New("balancer '", balancer, "' not found")
}

Also, maybe worth adding a getBalancer(tag) function for more readable code, which would replace this and the three (*r.balancers.Load())[tag] in app/router/balancing.go

Comment thread app/router/router.go
Comment on lines 46 to -78
@@ -68,26 +72,23 @@ func (r *Router) Init(ctx context.Context, config *Config, d dns.Client, ohm out
if wh := rule.GetWebhook(); wh != nil {
notifier, err := NewWebhookNotifier(wh)
if err != nil {
r.closeWebhooks()
return err
}
rr.Webhook = notifier
}
btag := rule.GetBalancingTag()
if len(btag) > 0 {
brule, found := r.balancers[btag]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Init() and ReloadRules() have almost identical structure when iterating over balancers and rules. I think it's worth extracting this into a separate function to keep them in sync. Also, unlike ReloadRules(), Init() doesn't check for duplicates!
So it's worth doing at least for the future, in case something changes in Init() but gets forgotten in ReloadRules()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants