(auths []*Auth, provider, routeModel string, now time.Time)
| 1355 | } |
| 1356 | |
| 1357 | func (m *Manager) availableAuthsForRouteModel(auths []*Auth, provider, routeModel string, now time.Time) ([]*Auth, error) { |
| 1358 | if len(auths) == 0 { |
| 1359 | return nil, &Error{Code: "auth_not_found", Message: "no auth candidates"} |
| 1360 | } |
| 1361 | |
| 1362 | availableByPriority := make(map[int][]*Auth) |
| 1363 | cooldownCount := 0 |
| 1364 | var earliest time.Time |
| 1365 | for _, candidate := range auths { |
| 1366 | checkModel := m.selectionModelForAuth(candidate, routeModel) |
| 1367 | blocked, reason, next := isAuthBlockedForModel(candidate, checkModel, now) |
| 1368 | if !blocked { |
| 1369 | priority := authPriority(candidate) |
| 1370 | availableByPriority[priority] = append(availableByPriority[priority], candidate) |
| 1371 | continue |
| 1372 | } |
| 1373 | if reason == blockReasonCooldown { |
| 1374 | cooldownCount++ |
| 1375 | if !next.IsZero() && (earliest.IsZero() || next.Before(earliest)) { |
| 1376 | earliest = next |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | if len(availableByPriority) == 0 { |
| 1382 | if cooldownCount == len(auths) && !earliest.IsZero() { |
| 1383 | providerForError := provider |
| 1384 | if providerForError == "mixed" { |
| 1385 | providerForError = "" |
| 1386 | } |
| 1387 | resetIn := earliest.Sub(now) |
| 1388 | if resetIn < 0 { |
| 1389 | resetIn = 0 |
| 1390 | } |
| 1391 | return nil, newModelCooldownError(routeModel, providerForError, resetIn) |
| 1392 | } |
| 1393 | return nil, &Error{Code: "auth_unavailable", Message: "no auth available"} |
| 1394 | } |
| 1395 | |
| 1396 | bestPriority := 0 |
| 1397 | found := false |
| 1398 | for priority := range availableByPriority { |
| 1399 | if !found || priority > bestPriority { |
| 1400 | bestPriority = priority |
| 1401 | found = true |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | available := availableByPriority[bestPriority] |
| 1406 | if len(available) > 1 { |
| 1407 | sort.Slice(available, func(i, j int) bool { return available[i].ID < available[j].ID }) |
| 1408 | } |
| 1409 | return available, nil |
| 1410 | } |
| 1411 | |
| 1412 | func selectionArgForSelector(selector Selector, routeModel string) string { |
| 1413 | if isBuiltInSelector(selector) { |
no test coverage detected