diff options
author | Bhasker Hariharan <bhaskerh@google.com> | 2020-03-17 19:09:24 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-03-17 19:10:53 -0700 |
commit | eddd6ce514e3bbcc08ce9d8435c7dac12715989c (patch) | |
tree | bdb678f41a203afc9db4783164c10fb64908b4ce | |
parent | 1cc5a71a0e2100c89c97ffd12a38143907b33630 (diff) |
Wrap rand.Reader in a bufio.Reader.
rand.Read() results in a syscall to the host on every call instead
we can wrap it with a bufio.Reader to buffer and reduce number of syscalls.
This is especially important for TCP where every newly created endpoint
reads random data to initialize the timestamp offsets for the endpoint.
Updates #231
PiperOrigin-RevId: 301501607
-rw-r--r-- | pkg/rand/rand_linux.go | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/pkg/rand/rand_linux.go b/pkg/rand/rand_linux.go index 0bdad5fad..1aec96e2b 100644 --- a/pkg/rand/rand_linux.go +++ b/pkg/rand/rand_linux.go @@ -45,12 +45,18 @@ func (r *reader) Read(p []byte) (int, error) { return rand.Read(p) } +// mu protects the global Reader below. +var mu sync.Mutex + // Reader is the default reader. var Reader io.Reader = &reader{} // Read reads from the default reader. func Read(b []byte) (int, error) { - return io.ReadFull(Reader, b) + mu.Lock() + n, err := io.ReadFull(Reader, b) + mu.Unlock() + return n, err } // Init can be called to make sure /dev/urandom is pre-opened on kernels that |