mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
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
This commit is contained in:
committed by
gVisor bot
parent
1cc5a71a0e
commit
eddd6ce514
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user