mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 02:30:39 +08:00
Merge pull request #2871 from rikonor/master
Added variable latency delay, normal and uniform based
This commit is contained in:
63
thirdparty/delay/delay.go
vendored
63
thirdparty/delay/delay.go
vendored
@ -1,10 +1,13 @@
|
|||||||
package delay
|
package delay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var sharedRNG = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
|
||||||
// Delay makes it easy to add (threadsafe) configurable delays to other
|
// Delay makes it easy to add (threadsafe) configurable delays to other
|
||||||
// objects.
|
// objects.
|
||||||
type D interface {
|
type D interface {
|
||||||
@ -23,8 +26,6 @@ type delay struct {
|
|||||||
t time.Duration
|
t time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO func Variable(time.Duration) D returns a delay with probablistic latency
|
|
||||||
|
|
||||||
func (d *delay) Set(t time.Duration) time.Duration {
|
func (d *delay) Set(t time.Duration) time.Duration {
|
||||||
d.l.Lock()
|
d.l.Lock()
|
||||||
defer d.l.Unlock()
|
defer d.l.Unlock()
|
||||||
@ -44,3 +45,61 @@ func (d *delay) Get() time.Duration {
|
|||||||
defer d.l.Unlock()
|
defer d.l.Unlock()
|
||||||
return d.t
|
return d.t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VariableNormal is a delay following a normal distribution
|
||||||
|
// Notice that to implement the D interface Set can only change the mean delay
|
||||||
|
// the standard deviation is set only at initialization
|
||||||
|
func VariableNormal(t, std time.Duration, rng *rand.Rand) D {
|
||||||
|
if rng == nil {
|
||||||
|
rng = sharedRNG
|
||||||
|
}
|
||||||
|
|
||||||
|
v := &variableNormal{
|
||||||
|
std: std,
|
||||||
|
rng: rng,
|
||||||
|
}
|
||||||
|
v.t = t
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
type variableNormal struct {
|
||||||
|
delay
|
||||||
|
std time.Duration
|
||||||
|
rng *rand.Rand
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *variableNormal) Wait() {
|
||||||
|
d.l.RLock()
|
||||||
|
defer d.l.RUnlock()
|
||||||
|
randomDelay := time.Duration(d.rng.NormFloat64() * float64(d.std))
|
||||||
|
time.Sleep(randomDelay + d.t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// VariableUniform is a delay following a uniform distribution
|
||||||
|
// Notice that to implement the D interface Set can only change the minimum delay
|
||||||
|
// the delta is set only at initialization
|
||||||
|
func VariableUniform(t, d time.Duration, rng *rand.Rand) D {
|
||||||
|
if rng == nil {
|
||||||
|
rng = sharedRNG
|
||||||
|
}
|
||||||
|
|
||||||
|
v := &variableUniform{
|
||||||
|
d: d,
|
||||||
|
rng: rng,
|
||||||
|
}
|
||||||
|
v.t = t
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
type variableUniform struct {
|
||||||
|
delay
|
||||||
|
d time.Duration // max delta
|
||||||
|
rng *rand.Rand
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *variableUniform) Wait() {
|
||||||
|
d.l.RLock()
|
||||||
|
defer d.l.RUnlock()
|
||||||
|
randomDelay := time.Duration(d.rng.Float64() * float64(d.d))
|
||||||
|
time.Sleep(randomDelay + d.t)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user