StartAutoRefresh launches a background loop that evaluates auth freshness every few seconds and triggers refresh operations when required. Only one loop is kept alive; starting a new one cancels the previous run.
(parent context.Context, interval time.Duration)
| 5237 | // every few seconds and triggers refresh operations when required. |
| 5238 | // Only one loop is kept alive; starting a new one cancels the previous run. |
| 5239 | func (m *Manager) StartAutoRefresh(parent context.Context, interval time.Duration) { |
| 5240 | if interval <= 0 { |
| 5241 | interval = refreshCheckInterval |
| 5242 | } |
| 5243 | |
| 5244 | m.mu.Lock() |
| 5245 | cancelPrev := m.refreshCancel |
| 5246 | m.refreshCancel = nil |
| 5247 | m.refreshLoop = nil |
| 5248 | m.mu.Unlock() |
| 5249 | if cancelPrev != nil { |
| 5250 | cancelPrev() |
| 5251 | } |
| 5252 | |
| 5253 | ctx, cancelCtx := context.WithCancel(parent) |
| 5254 | workers := refreshMaxConcurrency |
| 5255 | if cfg, ok := m.runtimeConfig.Load().(*internalconfig.Config); ok && cfg != nil && cfg.AuthAutoRefreshWorkers > 0 { |
| 5256 | workers = cfg.AuthAutoRefreshWorkers |
| 5257 | } |
| 5258 | loop := newAuthAutoRefreshLoop(m, interval, workers) |
| 5259 | |
| 5260 | m.mu.Lock() |
| 5261 | m.refreshCancel = cancelCtx |
| 5262 | m.refreshLoop = loop |
| 5263 | m.mu.Unlock() |
| 5264 | |
| 5265 | loop.rebuild(time.Now()) |
| 5266 | go loop.run(ctx) |
| 5267 | } |
| 5268 | |
| 5269 | // StopAutoRefresh cancels the background refresh loop, if running. |
| 5270 | // It also stops the selector if it implements StoppableSelector. |
no test coverage detected