kubo/util/delay/delay.go
Brian Tiger Chow ae79ac04d3 feat(util/delay) add Delay
License: MIT
Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
2014-12-13 05:17:16 -08:00

40 lines
630 B
Go

package delay
import (
"sync"
"time"
)
// Delay makes it easy to add (threadsafe) configurable delays to other
// objects.
type D interface {
Set(time.Duration) time.Duration
Wait()
}
// Fixed returns a delay with fixed latency
func Fixed(t time.Duration) D {
return &delay{t: t}
}
type delay struct {
l sync.RWMutex
t time.Duration
}
// TODO func Variable(time.Duration) D returns a delay with probablistic latency
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()
time.Sleep(d.t)
}