| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package main
- import (
- "sort"
- "sync"
- )
- // statusStore provides synchronized access to the current set of service statuses.
- type statusStore struct {
- mu sync.RWMutex
- records []serviceStatus
- }
- // set replaces the store contents with a copy of the provided status records.
- func (s *statusStore) set(records []serviceStatus) {
- s.mu.Lock()
- defer s.mu.Unlock()
- s.records = make([]serviceStatus, len(records))
- copy(s.records, records)
- }
- // upsert inserts a new status record or replaces the existing one with the same name.
- func (s *statusStore) upsert(record serviceStatus) {
- s.mu.Lock()
- defer s.mu.Unlock()
- for i := range s.records {
- if s.records[i].Name == record.Name {
- s.records[i] = record
- return
- }
- }
- s.records = append(s.records, record)
- }
- // list returns a copy of the current status records.
- func (s *statusStore) list() []serviceStatus {
- s.mu.RLock()
- defer s.mu.RUnlock()
- records := make([]serviceStatus, len(s.records))
- copy(records, s.records)
- return records
- }
- // sort orders the stored status records by health and name.
- func (s *statusStore) sort() {
- s.mu.Lock()
- defer s.mu.Unlock()
- sortStatuses(s.records)
- }
- // sortStatuses orders healthy services first and uses the service name as a tiebreaker.
- func sortStatuses(records []serviceStatus) {
- sort.Slice(records, func(i, j int) bool {
- if records[i].Healthy == records[j].Healthy {
- return records[i].Name < records[j].Name
- }
- return records[i].Healthy && !records[j].Healthy
- })
- }
|