MCPcopy Create free account
hub / github.com/bytebase/bytebase / CountActiveReplicas

Method CountActiveReplicas

backend/enterprise/license.go:485–505  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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).
485func (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.

Callers 1

CheckReplicaLimitMethod · 0.95

Calls 2

BBErrorFunction · 0.92
LoadMethod · 0.80

Tested by

no test coverage detected