From 39bfc51ce4592322d28f79ccf5c3515a2331a45f Mon Sep 17 00:00:00 2001 From: TwiN Date: Thu, 11 Sep 2025 14:13:31 -0400 Subject: [PATCH] fix(storage): race issue with memory store (#1256) --- storage/store/memory/memory.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/storage/store/memory/memory.go b/storage/store/memory/memory.go index e88c8c77..79f4e505 100644 --- a/storage/store/memory/memory.go +++ b/storage/store/memory/memory.go @@ -43,6 +43,8 @@ func NewStore(maximumNumberOfResults, maximumNumberOfEvents int) (*Store, error) // GetAllEndpointStatuses returns all monitored endpoint.Status // with a subset of endpoint.Result defined by the page and pageSize parameters func (s *Store) GetAllEndpointStatuses(params *paging.EndpointStatusParams) ([]*endpoint.Status, error) { + s.RLock() + defer s.RUnlock() allStatuses := s.endpointCache.GetAll() pagedEndpointStatuses := make([]*endpoint.Status, 0, len(allStatuses)) for _, v := range allStatuses { @@ -79,6 +81,8 @@ func (s *Store) GetEndpointStatus(groupName, endpointName string, params *paging // GetEndpointStatusByKey returns the endpoint status for a given key func (s *Store) GetEndpointStatusByKey(key string, params *paging.EndpointStatusParams) (*endpoint.Status, error) { + s.RLock() + defer s.RUnlock() endpointStatus := s.endpointCache.GetValue(key) if endpointStatus == nil { return nil, common.ErrEndpointNotFound @@ -102,6 +106,8 @@ func (s *Store) GetUptimeByKey(key string, from, to time.Time) (float64, error) if from.After(to) { return 0, common.ErrInvalidTimeRange } + s.RLock() + defer s.RUnlock() endpointStatus := s.endpointCache.GetValue(key) if endpointStatus == nil || endpointStatus.(*endpoint.Status).Uptime == nil { return 0, common.ErrEndpointNotFound @@ -131,6 +137,8 @@ func (s *Store) GetAverageResponseTimeByKey(key string, from, to time.Time) (int if from.After(to) { return 0, common.ErrInvalidTimeRange } + s.RLock() + defer s.RUnlock() endpointStatus := s.endpointCache.GetValue(key) if endpointStatus == nil || endpointStatus.(*endpoint.Status).Uptime == nil { return 0, common.ErrEndpointNotFound @@ -159,6 +167,8 @@ func (s *Store) GetHourlyAverageResponseTimeByKey(key string, from, to time.Time if from.After(to) { return nil, common.ErrInvalidTimeRange } + s.RLock() + defer s.RUnlock() endpointStatus := s.endpointCache.GetValue(key) if endpointStatus == nil || endpointStatus.(*endpoint.Status).Uptime == nil { return nil, common.ErrEndpointNotFound