...

Source file src/github.com/docker/distribution/uuid/uuid.go

Documentation: github.com/docker/distribution/uuid

     1  // Package uuid provides simple UUID generation. Only version 4 style UUIDs
     2  // can be generated.
     3  //
     4  // Please see http://tools.ietf.org/html/rfc4122 for details on UUIDs.
     5  package uuid
     6  
     7  import (
     8  	"crypto/rand"
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  	"syscall"
    13  	"time"
    14  )
    15  
    16  const (
    17  	// Bits is the number of bits in a UUID
    18  	Bits = 128
    19  
    20  	// Size is the number of bytes in a UUID
    21  	Size = Bits / 8
    22  
    23  	format = "%08x-%04x-%04x-%04x-%012x"
    24  )
    25  
    26  var (
    27  	// ErrUUIDInvalid indicates a parsed string is not a valid uuid.
    28  	ErrUUIDInvalid = fmt.Errorf("invalid uuid")
    29  
    30  	// Loggerf can be used to override the default logging destination. Such
    31  	// log messages in this library should be logged at warning or higher.
    32  	Loggerf = func(format string, args ...interface{}) {}
    33  )
    34  
    35  // UUID represents a UUID value. UUIDs can be compared and set to other values
    36  // and accessed by byte.
    37  type UUID [Size]byte
    38  
    39  // Generate creates a new, version 4 uuid.
    40  func Generate() (u UUID) {
    41  	const (
    42  		// ensures we backoff for less than 450ms total. Use the following to
    43  		// select new value, in units of 10ms:
    44  		// 	n*(n+1)/2 = d -> n^2 + n - 2d -> n = (sqrt(8d + 1) - 1)/2
    45  		maxretries = 9
    46  		backoff    = time.Millisecond * 10
    47  	)
    48  
    49  	var (
    50  		totalBackoff time.Duration
    51  		count        int
    52  		retries      int
    53  	)
    54  
    55  	for {
    56  		// This should never block but the read may fail. Because of this,
    57  		// we just try to read the random number generator until we get
    58  		// something. This is a very rare condition but may happen.
    59  		b := time.Duration(retries) * backoff
    60  		time.Sleep(b)
    61  		totalBackoff += b
    62  
    63  		n, err := io.ReadFull(rand.Reader, u[count:])
    64  		if err != nil {
    65  			if retryOnError(err) && retries < maxretries {
    66  				count += n
    67  				retries++
    68  				Loggerf("error generating version 4 uuid, retrying: %v", err)
    69  				continue
    70  			}
    71  
    72  			// Any other errors represent a system problem. What did someone
    73  			// do to /dev/urandom?
    74  			panic(fmt.Errorf("error reading random number generator, retried for %v: %v", totalBackoff.String(), err))
    75  		}
    76  
    77  		break
    78  	}
    79  
    80  	u[6] = (u[6] & 0x0f) | 0x40 // set version byte
    81  	u[8] = (u[8] & 0x3f) | 0x80 // set high order byte 0b10{8,9,a,b}
    82  
    83  	return u
    84  }
    85  
    86  // Parse attempts to extract a uuid from the string or returns an error.
    87  func Parse(s string) (u UUID, err error) {
    88  	if len(s) != 36 {
    89  		return UUID{}, ErrUUIDInvalid
    90  	}
    91  
    92  	// create stack addresses for each section of the uuid.
    93  	p := make([][]byte, 5)
    94  
    95  	if _, err := fmt.Sscanf(s, format, &p[0], &p[1], &p[2], &p[3], &p[4]); err != nil {
    96  		return u, err
    97  	}
    98  
    99  	copy(u[0:4], p[0])
   100  	copy(u[4:6], p[1])
   101  	copy(u[6:8], p[2])
   102  	copy(u[8:10], p[3])
   103  	copy(u[10:16], p[4])
   104  
   105  	return
   106  }
   107  
   108  func (u UUID) String() string {
   109  	return fmt.Sprintf(format, u[:4], u[4:6], u[6:8], u[8:10], u[10:])
   110  }
   111  
   112  // retryOnError tries to detect whether or not retrying would be fruitful.
   113  func retryOnError(err error) bool {
   114  	switch err := err.(type) {
   115  	case *os.PathError:
   116  		return retryOnError(err.Err) // unpack the target error
   117  	case syscall.Errno:
   118  		if err == syscall.EPERM {
   119  			// EPERM represents an entropy pool exhaustion, a condition under
   120  			// which we backoff and retry.
   121  			return true
   122  		}
   123  	}
   124  
   125  	return false
   126  }
   127  

View as plain text