-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelay.go
49 lines (39 loc) · 970 Bytes
/
delay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package chaos
import (
"encoding/json"
"fmt"
"math/rand"
"time"
)
type delaySpec struct {
duration time.Duration
probability float64
}
func (s *delaySpec) UnmarshalJSON(data []byte) error {
delaySpec := struct {
Duration int `json:"duration"`
Probability float64 `json:"p"`
}{}
if err := json.Unmarshal(data, &delaySpec); err != nil {
return err
}
s.duration = time.Duration(delaySpec.Duration) * time.Millisecond
s.probability = delaySpec.Probability
if delaySpec.Duration <= 0 {
return fmt.Errorf("delay duration parameter value must be greater than 0 ")
}
if s.probability < 0 || s.probability > 1 {
return fmt.Errorf("probability parameter value must be 0 < p < 1 ")
}
return nil
}
func (s *spec) injectDelay() bool {
if s.delay != nil {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
if p := rnd.Float64(); p > 1-s.delay.probability {
time.Sleep(s.delay.duration)
return true
}
}
return false
}