delay.go 1.66 KB
Newer Older
1 2 3
package delay

import (
4
	"math/rand"
5 6 7 8
	"sync"
	"time"
)

9
// D (Delay) makes it easy to add (threadsafe) configurable delays to other
10 11 12 13
// objects.
type D interface {
	Set(time.Duration) time.Duration
	Wait()
14
	NextWaitTime() time.Duration
15 16 17 18
	Get() time.Duration
}

type delay struct {
19 20 21
	l         sync.RWMutex
	t         time.Duration
	generator Generator
22 23 24 25 26 27 28 29 30 31 32 33 34
}

func (d *delay) Set(t time.Duration) time.Duration {
	d.l.Lock()
	defer d.l.Unlock()
	prev := d.t
	d.t = t
	return prev
}

func (d *delay) Wait() {
	d.l.RLock()
	defer d.l.RUnlock()
35
	time.Sleep(d.generator.NextWaitTime(d.t))
36 37 38 39 40
}

func (d *delay) NextWaitTime() time.Duration {
	d.l.Lock()
	defer d.l.Unlock()
41
	return d.generator.NextWaitTime(d.t)
42 43 44 45 46 47 48
}

func (d *delay) Get() time.Duration {
	d.l.Lock()
	defer d.l.Unlock()
	return d.t
}
49

50
// Delay generates a generic delay form a t, a sleeper, and a generator
51
func Delay(t time.Duration, generator Generator) D {
52 53 54
	return &delay{
		t:         t,
		generator: generator,
55 56 57
	}
}

58 59
// Fixed returns a delay with fixed latency
func Fixed(t time.Duration) D {
60
	return Delay(t, FixedGenerator())
61 62 63 64 65 66
}

// 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 {
67
	return Delay(t, VariableUniformGenerator(d, rng))
68 69
}

70 71 72 73
// 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 {
74
	return Delay(t, VariableNormalGenerator(std, rng))
75
}