Reduce consumption of entropy source

The UUID generation retries multiple times to read a full UUID, but
preserves any bytes of entropy he successfully managed to read between
retries.

Signed-off-by: Arnaud Porterie <arnaud.porterie@docker.com>
This commit is contained in:
Arnaud Porterie 2015-07-28 10:01:54 -07:00
parent 0355c3026c
commit c4d69174ee

View file

@ -49,6 +49,7 @@ func Generate() (u UUID) {
var ( var (
totalBackoff time.Duration totalBackoff time.Duration
count int
retries int retries int
) )
@ -60,9 +61,10 @@ func Generate() (u UUID) {
time.Sleep(b) time.Sleep(b)
totalBackoff += b totalBackoff += b
_, err := io.ReadFull(rand.Reader, u[:]) n, err := io.ReadFull(rand.Reader, u[count:])
if err != nil { if err != nil {
if retryOnError(err) && retries < maxretries { if retryOnError(err) && retries < maxretries {
count += n
retries++ retries++
Loggerf("error generating version 4 uuid, retrying: %v", err) Loggerf("error generating version 4 uuid, retrying: %v", err)
continue continue