Merge pull request #67 from router-for-me/rr
fix(auth): Make round-robin auth selection deterministic
This commit is contained in:
@@ -2,6 +2,7 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -36,6 +37,10 @@ func (s *RoundRobinSelector) Pick(ctx context.Context, provider, model string, o
|
|||||||
if len(available) == 0 {
|
if len(available) == 0 {
|
||||||
return nil, &Error{Code: "auth_unavailable", Message: "no auth available"}
|
return nil, &Error{Code: "auth_unavailable", Message: "no auth available"}
|
||||||
}
|
}
|
||||||
|
// Make round-robin deterministic even if caller's candidate order is unstable.
|
||||||
|
if len(available) > 1 {
|
||||||
|
sort.Slice(available, func(i, j int) bool { return available[i].ID < available[j].ID })
|
||||||
|
}
|
||||||
key := provider + ":" + model
|
key := provider + ":" + model
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
index := s.cursors[key]
|
index := s.cursors[key]
|
||||||
|
|||||||
Reference in New Issue
Block a user