refactor: Fix race condition in chaos service and update Redis rate limiter test mocks

- Add copy method to Experiment struct to create deep copy with proper Finished pointer handling
- Return experiment copy from RunExperiment to prevent race condition with JSON marshaling
- Update mock Eval calls to use float64 for window parameter instead of int64 in redis_ratelimit_test.go
- Update comments to reflect correct parameter types: now (int64), window (float64), limit (int64)
This commit is contained in:
nutcas3
2026-04-25 06:54:29 +03:00
parent 0220dc5b52
commit f6ec659fe9
2 changed files with 27 additions and 6 deletions
+22 -1
View File
@@ -30,6 +30,26 @@ type Experiment struct {
Error string
}
// copy creates a deep copy of the experiment to avoid race conditions
func (e *Experiment) copy() *Experiment {
var finished *time.Time
if e.Finished != nil {
fin := *e.Finished
finished = &fin
}
return &Experiment{
ID: e.ID,
Name: e.Name,
Target: e.Target,
Type: e.Type,
Config: e.Config,
Status: e.Status,
Started: e.Started,
Finished: finished,
Error: e.Error,
}
}
type ExperimentType string
const (
@@ -90,7 +110,8 @@ func (cs *ChaosService) RunExperiment(ctx context.Context, expType ExperimentTyp
// Run experiment in background
go cs.executeExperiment(ctx, exp)
return exp, nil
// Return a copy to avoid race condition with JSON marshaling
return exp.copy(), nil
}
func (cs *ChaosService) executeExperiment(ctx context.Context, exp *Experiment) {