diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/rand/rand_linux.go | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/pkg/rand/rand_linux.go b/pkg/rand/rand_linux.go index 0bdad5fad..fa6a21026 100644 --- a/pkg/rand/rand_linux.go +++ b/pkg/rand/rand_linux.go @@ -17,6 +17,7 @@ package rand import ( + "bufio" "crypto/rand" "io" @@ -45,8 +46,22 @@ func (r *reader) Read(p []byte) (int, error) { return rand.Read(p) } +// bufferedReader implements a threadsafe buffered io.Reader. +type bufferedReader struct { + mu sync.Mutex + r *bufio.Reader +} + +// Read implements io.Reader.Read. +func (b *bufferedReader) Read(p []byte) (int, error) { + b.mu.Lock() + n, err := b.r.Read(p) + b.mu.Unlock() + return n, err +} + // Reader is the default reader. -var Reader io.Reader = &reader{} +var Reader io.Reader = &bufferedReader{r: bufio.NewReader(&reader{})} // Read reads from the default reader. func Read(b []byte) (int, error) { |