summaryrefslogtreecommitdiffhomepage
path: root/pkg/rand
diff options
context:
space:
mode:
authorBhasker Hariharan <bhaskerh@google.com>2020-03-20 17:00:55 -0700
committergVisor bot <gvisor-bot@google.com>2020-03-20 17:02:13 -0700
commit1bf2e52bdb5f366b397cb887d4cbdb91dd5e3213 (patch)
tree95acd386e7147b1425fed4ef1d4638b1c0cf2ecc /pkg/rand
parent49aef9cee70d111f6c3e1a6b04430bbe414a6c1e (diff)
Actually wrap rand.Reader in bufio.Reader.
Updates #231 PiperOrigin-RevId: 302127697
Diffstat (limited to 'pkg/rand')
-rw-r--r--pkg/rand/rand_linux.go17
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) {