CountActiveReplicas returns the count of active replicas. A replica is considered active if it has sent a heartbeat within the last 30 seconds. Returns at least 1 (the current replica is always counted).
(ctx context.Context)
| 483 | // A replica is considered active if it has sent a heartbeat within the last 30 seconds. |
| 484 | // Returns at least 1 (the current replica is always counted). |
| 485 | func (s *LicenseService) CountActiveReplicas(ctx context.Context) int { |
| 486 | if state := s.replicaCache.Load(); state != nil && time.Since(state.loadedAt) < replicaActiveWindow { |
| 487 | return state.replicaCount |
| 488 | } |
| 489 | |
| 490 | count, err := s.store.CountActiveReplicas(ctx, replicaActiveWindow) |
| 491 | if err != nil { |
| 492 | slog.Warn("failed to count active replicas", log.BBError(err)) |
| 493 | return 1 |
| 494 | } |
| 495 | |
| 496 | if count < 1 { |
| 497 | count = 1 |
| 498 | } |
| 499 | s.replicaCache.Store(&replicaCacheState{ |
| 500 | replicaCount: count, |
| 501 | loadedAt: time.Now(), |
| 502 | }) |
| 503 | |
| 504 | return count |
| 505 | } |
| 506 | |
| 507 | // CheckReplicaLimit checks if the current replica count exceeds the allowed limit. |
| 508 | // Returns error if HA is not allowed and there are multiple active replicas. |
no test coverage detected