fix(storage): resolve race condition in memory store (#1270)
* fix(storage): resolve race condition in memory store * fix: resolve variable shadowing in CopyEndpointStatus * fix: update test files to use CopyEndpointStatus function
This commit is contained in:
@@ -6,35 +6,47 @@ import (
|
||||
"github.com/TwiN/gatus/v5/storage/store/common/paging"
|
||||
)
|
||||
|
||||
// ShallowCopyEndpointStatus returns a shallow copy of a Status with only the results
|
||||
// within the range defined by the page and pageSize parameters
|
||||
func ShallowCopyEndpointStatus(ss *endpoint.Status, params *paging.EndpointStatusParams) *endpoint.Status {
|
||||
shallowCopy := &endpoint.Status{
|
||||
// CopyEndpointStatus returns a safe copy of a Status with only the results
|
||||
// within the range defined by the page and pageSize parameters.
|
||||
// This function performs deep copying of slices to prevent race conditions
|
||||
// when the original slice is modified concurrently.
|
||||
func CopyEndpointStatus(ss *endpoint.Status, params *paging.EndpointStatusParams) *endpoint.Status {
|
||||
statusCopy := &endpoint.Status{
|
||||
Name: ss.Name,
|
||||
Group: ss.Group,
|
||||
Key: ss.Key,
|
||||
Uptime: endpoint.NewUptime(),
|
||||
}
|
||||
if params == nil || (params.ResultsPage == 0 && params.ResultsPageSize == 0 && params.EventsPage == 0 && params.EventsPageSize == 0) {
|
||||
shallowCopy.Results = ss.Results
|
||||
shallowCopy.Events = ss.Events
|
||||
// Deep copy all results to prevent race conditions
|
||||
statusCopy.Results = make([]*endpoint.Result, len(ss.Results))
|
||||
copy(statusCopy.Results, ss.Results)
|
||||
// Deep copy all events to prevent race conditions
|
||||
statusCopy.Events = make([]*endpoint.Event, len(ss.Events))
|
||||
copy(statusCopy.Events, ss.Events)
|
||||
} else {
|
||||
numberOfResults := len(ss.Results)
|
||||
resultsStart, resultsEnd := getStartAndEndIndex(numberOfResults, params.ResultsPage, params.ResultsPageSize)
|
||||
if resultsStart < 0 || resultsEnd < 0 {
|
||||
shallowCopy.Results = []*endpoint.Result{}
|
||||
statusCopy.Results = []*endpoint.Result{}
|
||||
} else {
|
||||
shallowCopy.Results = ss.Results[resultsStart:resultsEnd]
|
||||
// Deep copy the slice range to prevent race conditions
|
||||
resultRange := ss.Results[resultsStart:resultsEnd]
|
||||
statusCopy.Results = make([]*endpoint.Result, len(resultRange))
|
||||
copy(statusCopy.Results, resultRange)
|
||||
}
|
||||
numberOfEvents := len(ss.Events)
|
||||
eventsStart, eventsEnd := getStartAndEndIndex(numberOfEvents, params.EventsPage, params.EventsPageSize)
|
||||
if eventsStart < 0 || eventsEnd < 0 {
|
||||
shallowCopy.Events = []*endpoint.Event{}
|
||||
statusCopy.Events = []*endpoint.Event{}
|
||||
} else {
|
||||
shallowCopy.Events = ss.Events[eventsStart:eventsEnd]
|
||||
// Deep copy the slice range to prevent race conditions
|
||||
eventRange := ss.Events[eventsStart:eventsEnd]
|
||||
statusCopy.Events = make([]*endpoint.Event, len(eventRange))
|
||||
copy(statusCopy.Events, eventRange)
|
||||
}
|
||||
}
|
||||
return shallowCopy
|
||||
return statusCopy
|
||||
}
|
||||
|
||||
// ShallowCopySuiteStatus returns a shallow copy of a suite Status with only the results
|
||||
|
||||
Reference in New Issue
Block a user