mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
feat(cliproxy/auth): restructure auth candidate selection and ensure synchronization
- Refactored candidate selection logic in `auth/manager.go`. - Ensured proper synchronization around `mu.RUnlock` to prevent racing conditions.
This commit is contained in:
@@ -787,27 +787,31 @@ func (m *Manager) pickNext(ctx context.Context, provider, model string, opts cli
|
|||||||
return nil, nil, &Error{Code: "executor_not_found", Message: "executor not registered"}
|
return nil, nil, &Error{Code: "executor_not_found", Message: "executor not registered"}
|
||||||
}
|
}
|
||||||
candidates := make([]*Auth, 0, len(m.auths))
|
candidates := make([]*Auth, 0, len(m.auths))
|
||||||
for _, auth := range m.auths {
|
for _, candidate := range m.auths {
|
||||||
if auth.Provider != provider || auth.Disabled {
|
if candidate.Provider != provider || candidate.Disabled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, used := tried[auth.ID]; used {
|
if _, used := tried[candidate.ID]; used {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
candidates = append(candidates, auth.Clone())
|
candidates = append(candidates, candidate)
|
||||||
}
|
}
|
||||||
m.mu.RUnlock()
|
|
||||||
if len(candidates) == 0 {
|
if len(candidates) == 0 {
|
||||||
|
m.mu.RUnlock()
|
||||||
return nil, nil, &Error{Code: "auth_not_found", Message: "no auth available"}
|
return nil, nil, &Error{Code: "auth_not_found", Message: "no auth available"}
|
||||||
}
|
}
|
||||||
auth, errPick := m.selector.Pick(ctx, provider, model, opts, candidates)
|
selected, errPick := m.selector.Pick(ctx, provider, model, opts, candidates)
|
||||||
if errPick != nil {
|
if errPick != nil {
|
||||||
|
m.mu.RUnlock()
|
||||||
return nil, nil, errPick
|
return nil, nil, errPick
|
||||||
}
|
}
|
||||||
if auth == nil {
|
if selected == nil {
|
||||||
|
m.mu.RUnlock()
|
||||||
return nil, nil, &Error{Code: "auth_not_found", Message: "selector returned no auth"}
|
return nil, nil, &Error{Code: "auth_not_found", Message: "selector returned no auth"}
|
||||||
}
|
}
|
||||||
return auth, executor, nil
|
authCopy := selected.Clone()
|
||||||
|
m.mu.RUnlock()
|
||||||
|
return authCopy, executor, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) persist(ctx context.Context, auth *Auth) error {
|
func (m *Manager) persist(ctx context.Context, auth *Auth) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user