Files
SafelyYouCodingChallenge/go/internal/store/store.go
T

39 lines
1.1 KiB
Go
Raw Normal View History

package store
import "sync"
// DeviceID is a typed device identifier.
// Using a named type (instead of plain string) makes call sites clearer
// and prevents accidentally mixing unrelated string values.
type DeviceID string
// Store holds in-memory fleet metrics for known devices.
type Store struct {
known map[DeviceID]struct{}
// mu protects the heartbeats/uploads maps (insert/lookup of entries only).
// Each per-device history has its own mutex for mutations.
mu sync.Mutex
heartbeats map[DeviceID]*heartbeatHistory
uploads map[DeviceID]*uploadHistory
}
// New creates a Store seeded with the given known device IDs.
func New(known []DeviceID) *Store {
m := make(map[DeviceID]struct{}, len(known))
for _, id := range known {
m[id] = struct{}{}
}
return &Store{
known: m,
heartbeats: make(map[DeviceID]*heartbeatHistory),
uploads: make(map[DeviceID]*uploadHistory),
}
}
// Known reports whether deviceID was present in the startup device list.
func (s *Store) Known(deviceID DeviceID) bool {
_, ok := s.known[deviceID]
return ok
}