Refactor router to fix api data race - #6678
Open
Fangliding wants to merge 1 commit into
Open
Conversation
MakostaDev
reviewed
Aug 24, 2026
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") | ||
| } |
Contributor
There was a problem hiding this comment.
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
MakostaDev
reviewed
Aug 24, 2026
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] | |||
Contributor
There was a problem hiding this comment.
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()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复API调整路由时的竞争问题 顺便简化逻辑