mirror of
https://github.com/hashicorp/vault.git
synced 2025-12-01 15:41:22 +01:00
Remove data races around error/latency injector rand objects
This commit is contained in:
parent
d1be7165b0
commit
ab520a9119
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/hashicorp/go-hclog"
|
log "github.com/hashicorp/go-hclog"
|
||||||
@ -18,6 +19,7 @@ const (
|
|||||||
type ErrorInjector struct {
|
type ErrorInjector struct {
|
||||||
backend Backend
|
backend Backend
|
||||||
errorPercent int
|
errorPercent int
|
||||||
|
randomLock *sync.Mutex
|
||||||
random *rand.Rand
|
random *rand.Rand
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,6 +44,7 @@ func NewErrorInjector(b Backend, errorPercent int, logger log.Logger) *ErrorInje
|
|||||||
return &ErrorInjector{
|
return &ErrorInjector{
|
||||||
backend: b,
|
backend: b,
|
||||||
errorPercent: errorPercent,
|
errorPercent: errorPercent,
|
||||||
|
randomLock: new(sync.Mutex),
|
||||||
random: rand.New(rand.NewSource(int64(time.Now().Nanosecond()))),
|
random: rand.New(rand.NewSource(int64(time.Now().Nanosecond()))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,7 +62,9 @@ func (e *ErrorInjector) SetErrorPercentage(p int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *ErrorInjector) addError() error {
|
func (e *ErrorInjector) addError() error {
|
||||||
|
e.randomLock.Lock()
|
||||||
roll := e.random.Intn(100)
|
roll := e.random.Intn(100)
|
||||||
|
e.randomLock.Unlock()
|
||||||
if roll < e.errorPercent {
|
if roll < e.errorPercent {
|
||||||
return errors.New("random error")
|
return errors.New("random error")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package physical
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/hashicorp/go-hclog"
|
log "github.com/hashicorp/go-hclog"
|
||||||
@ -19,6 +20,7 @@ type LatencyInjector struct {
|
|||||||
backend Backend
|
backend Backend
|
||||||
latency time.Duration
|
latency time.Duration
|
||||||
jitterPercent int
|
jitterPercent int
|
||||||
|
randomLock *sync.Mutex
|
||||||
random *rand.Rand
|
random *rand.Rand
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,6 +47,7 @@ func NewLatencyInjector(b Backend, latency time.Duration, jitter int, logger log
|
|||||||
backend: b,
|
backend: b,
|
||||||
latency: latency,
|
latency: latency,
|
||||||
jitterPercent: jitter,
|
jitterPercent: jitter,
|
||||||
|
randomLock: new(sync.Mutex),
|
||||||
random: rand.New(rand.NewSource(int64(time.Now().Nanosecond()))),
|
random: rand.New(rand.NewSource(int64(time.Now().Nanosecond()))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,7 +71,9 @@ func (l *LatencyInjector) addLatency() {
|
|||||||
if l.jitterPercent > 0 {
|
if l.jitterPercent > 0 {
|
||||||
min := 100 - l.jitterPercent
|
min := 100 - l.jitterPercent
|
||||||
max := 100 + l.jitterPercent
|
max := 100 + l.jitterPercent
|
||||||
|
l.randomLock.Lock()
|
||||||
percent = l.random.Intn(max-min) + min
|
percent = l.random.Intn(max-min) + min
|
||||||
|
l.randomLock.Unlock()
|
||||||
}
|
}
|
||||||
latencyDuration := time.Duration(int(l.latency) * percent / 100)
|
latencyDuration := time.Duration(int(l.latency) * percent / 100)
|
||||||
time.Sleep(latencyDuration)
|
time.Sleep(latencyDuration)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user