cachedBackendHealth probes adapter.Health per backend with TTL cache (shared by routing and /status).
(ctx context.Context)
| 274 | |
| 275 | // cachedBackendHealth probes adapter.Health per backend with TTL cache (shared by routing and /status). |
| 276 | func (s *Server) cachedBackendHealth(ctx context.Context) map[string]bool { |
| 277 | ttl := time.Duration(s.cfg.Server.HealthCacheTTLMS) * time.Millisecond |
| 278 | if ttl <= 0 { |
| 279 | ttl = 2 * time.Second |
| 280 | } |
| 281 | perCheck := time.Duration(s.cfg.Server.HealthCheckPerMS) * time.Millisecond |
| 282 | if perCheck <= 0 { |
| 283 | perCheck = 1500 * time.Millisecond |
| 284 | } |
| 285 | |
| 286 | if snap := s.peekFreshHealthSnapshot(ttl); snap != nil { |
| 287 | return snap |
| 288 | } |
| 289 | |
| 290 | v, _, _ := s.healthFlight.Do("backend_health", func() (interface{}, error) { |
| 291 | if snap := s.peekFreshHealthSnapshot(ttl); snap != nil { |
| 292 | return snap, nil |
| 293 | } |
| 294 | out := s.probeBackendHealth(perCheck) |
| 295 | s.healthMu.Lock() |
| 296 | s.healthSnapshot = out |
| 297 | s.healthAt = time.Now() |
| 298 | s.healthMu.Unlock() |
| 299 | return out, nil |
| 300 | }) |
| 301 | m, _ := v.(map[string]bool) |
| 302 | return cloneBoolMap(m) |
| 303 | } |
| 304 | |
| 305 | func (s *Server) peekFreshHealthSnapshot(ttl time.Duration) map[string]bool { |
| 306 | s.healthMu.Lock() |
no test coverage detected