mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-02 09:52:20 +00:00
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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user