uuid.go 3.0 KB

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