Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish the routing source code #53

Open
mathetake opened this issue Dec 27, 2024 · 2 comments
Open

Polish the routing source code #53

mathetake opened this issue Dec 27, 2024 · 2 comments
Labels
help wanted Extra attention is needed
Milestone

Comments

@mathetake
Copy link
Member

I added the very naive implementation in #50

// Calculate implements [Router.Calculate].
func (r *router) Calculate(headers map[string]string) (backendName string, outputSchema extprocconfig.VersionedAPISchema, err error) {
var rule *extprocconfig.RouteRule
for i := range r.rules {
_rule := &r.rules[i]
for _, hdr := range _rule.Headers {
v, ok := headers[string(hdr.Name)]
// Currently, we only do the exact matching.
if ok && v == hdr.Value {
rule = _rule
break
}
}
}
if rule == nil {
return "", extprocconfig.VersionedAPISchema{}, errors.New("no matching rule found")
}
backendName, outputSchema = r.selectBackendFromRule(rule)
return
}
func (r *router) selectBackendFromRule(rule *extprocconfig.RouteRule) (backendName string, outputSchema extprocconfig.VersionedAPISchema) {
// Each backend has a weight, so we randomly select depending on the weight.
// This is a pretty naive implementation and can be buggy, so fix it later.
totalWeight := 0
for _, b := range rule.Backends {
totalWeight += b.Weight
}
if totalWeight == 0 {
return rule.Backends[0].Name, rule.Backends[0].OutputSchema
}
selected := r.rng.Intn(totalWeight)
for _, b := range rule.Backends {
if selected < b.Weight {
return b.Name, b.OutputSchema
}
selected -= b.Weight
}
return rule.Backends[0].Name, rule.Backends[0].OutputSchema
}

which I think we should refactor before v0.1.0

@mathetake mathetake added this to the v.0.1.0 milestone Dec 27, 2024
@mathetake mathetake added the help wanted Extra attention is needed label Dec 27, 2024
@theBeginner86
Copy link

Hi @mathetake,
May I help in improving this?

@mathetake
Copy link
Member Author

nah i am sorry but i would rather one of maintainers to take care of this, but thanks for the offer anyways

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants