1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/random/random.go
Alexander Morozov d8661250e7 Use goroutine-safe version of rand.Source
Signed-off-by: Alexander Morozov <lk4d4@docker.com>
2015-05-19 12:32:40 -07:00

34 lines
568 B
Go

package random
import (
"math/rand"
"sync"
"time"
)
// copypaste from standard math/rand
type lockedSource struct {
lk sync.Mutex
src rand.Source
}
func (r *lockedSource) Int63() (n int64) {
r.lk.Lock()
n = r.src.Int63()
r.lk.Unlock()
return
}
func (r *lockedSource) Seed(seed int64) {
r.lk.Lock()
r.src.Seed(seed)
r.lk.Unlock()
}
// NewSource returns math/rand.Source safe for concurrent use and initialized
// with current unix-nano timestamp
func NewSource() rand.Source {
return &lockedSource{
src: rand.NewSource(time.Now().UnixNano()),
}
}