| 5537 | } |
| 5538 | |
| 5539 | func (m *Manager) refreshAuth(ctx context.Context, id string) { |
| 5540 | if ctx == nil { |
| 5541 | ctx = context.Background() |
| 5542 | } |
| 5543 | m.mu.RLock() |
| 5544 | auth := m.auths[id] |
| 5545 | var exec ProviderExecutor |
| 5546 | var cloned *Auth |
| 5547 | if auth != nil { |
| 5548 | exec = m.executors[auth.Provider] |
| 5549 | cloned = auth.Clone() |
| 5550 | } |
| 5551 | m.mu.RUnlock() |
| 5552 | if auth == nil || exec == nil { |
| 5553 | return |
| 5554 | } |
| 5555 | updated, err := exec.Refresh(ctx, cloned) |
| 5556 | if err != nil && errors.Is(err, context.Canceled) { |
| 5557 | log.Debugf("refresh canceled for %s, %s", auth.Provider, auth.ID) |
| 5558 | return |
| 5559 | } |
| 5560 | log.Debugf("refreshed %s, %s, %v", auth.Provider, auth.ID, err) |
| 5561 | now := time.Now() |
| 5562 | if err != nil { |
| 5563 | unauthorized := isUnauthorizedError(err) |
| 5564 | shouldReschedule := false |
| 5565 | m.mu.Lock() |
| 5566 | if current := m.auths[id]; current != nil { |
| 5567 | current.LastError = refreshErrorFromError(err) |
| 5568 | if unauthorized { |
| 5569 | current.NextRefreshAfter = time.Time{} |
| 5570 | current.Unavailable = true |
| 5571 | current.Status = StatusError |
| 5572 | current.StatusMessage = "unauthorized" |
| 5573 | } else { |
| 5574 | current.NextRefreshAfter = now.Add(refreshFailureBackoff) |
| 5575 | } |
| 5576 | m.auths[id] = current |
| 5577 | shouldReschedule = true |
| 5578 | if m.scheduler != nil { |
| 5579 | m.scheduler.upsertAuth(current.Clone()) |
| 5580 | } |
| 5581 | } |
| 5582 | m.mu.Unlock() |
| 5583 | if shouldReschedule { |
| 5584 | m.queueRefreshReschedule(id) |
| 5585 | } |
| 5586 | return |
| 5587 | } |
| 5588 | if updated == nil { |
| 5589 | updated = cloned |
| 5590 | } |
| 5591 | // Preserve runtime created by the executor during Refresh. |
| 5592 | // If executor didn't set one, fall back to the previous runtime. |
| 5593 | if updated.Runtime == nil { |
| 5594 | updated.Runtime = auth.Runtime |
| 5595 | } |
| 5596 | updated.LastRefreshedAt = now |