You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

111 line
3.4 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package uid supports generating unique IDs. Its chief purpose is to prevent
  15. // multiple test executions from interfering with each other, and to facilitate
  16. // cleanup of old entities that may remain if tests exit early.
  17. package uid
  18. import (
  19. "fmt"
  20. "regexp"
  21. "strconv"
  22. "sync"
  23. "time"
  24. )
  25. // A Space manages a set of unique IDs distinguished by a prefix.
  26. type Space struct {
  27. Prefix string // Prefix of UIDs. Read-only.
  28. Sep rune // Separates UID parts. Read-only.
  29. Time time.Time // Timestamp for UIDs. Read-only.
  30. re *regexp.Regexp
  31. mu sync.Mutex
  32. count int
  33. }
  34. // Options are optional values for a Space.
  35. type Options struct {
  36. Sep rune // Separates parts of the UID. Defaults to '-'.
  37. Time time.Time // Timestamp for all UIDs made with this space. Defaults to current time.
  38. }
  39. func NewSpace(prefix string, opts *Options) *Space {
  40. sep := '-'
  41. tm := time.Now().UTC()
  42. if opts != nil {
  43. if opts.Sep != 0 {
  44. sep = opts.Sep
  45. }
  46. if !opts.Time.IsZero() {
  47. tm = opts.Time
  48. }
  49. }
  50. re := fmt.Sprintf(`^%s%[2]c(\d{4})(\d{2})(\d{2})%[2]c(\d+)%[2]c\d+$`,
  51. regexp.QuoteMeta(prefix), sep)
  52. return &Space{
  53. Prefix: prefix,
  54. Sep: sep,
  55. Time: tm,
  56. re: regexp.MustCompile(re),
  57. }
  58. }
  59. // New generates a new unique ID. The ID consists of the Space's prefix, a
  60. // timestamp, and a counter value. All unique IDs generated in the same test
  61. // execution will have the same timestamp.
  62. //
  63. // Aside from the characters in the prefix, IDs contain only letters, numbers
  64. // and sep.
  65. func (s *Space) New() string {
  66. s.mu.Lock()
  67. c := s.count
  68. s.count++
  69. s.mu.Unlock()
  70. // Write the time as a date followed by nanoseconds from midnight of that date.
  71. // That makes it easier to see the approximate time of the ID when it is displayed.
  72. y, m, d := s.Time.Date()
  73. ns := s.Time.Sub(time.Date(y, m, d, 0, 0, 0, 0, time.UTC))
  74. // Zero-pad the counter for lexical sort order for IDs with the same timestamp.
  75. return fmt.Sprintf("%s%c%04d%02d%02d%c%d%c%04d",
  76. s.Prefix, s.Sep, y, m, d, s.Sep, ns, s.Sep, c)
  77. }
  78. // Timestamp extracts the timestamp of uid, which must have been generated by
  79. // s. The second return value is true on success, false if there was a problem.
  80. func (s *Space) Timestamp(uid string) (time.Time, bool) {
  81. subs := s.re.FindStringSubmatch(uid)
  82. if subs == nil {
  83. return time.Time{}, false
  84. }
  85. y, err1 := strconv.Atoi(subs[1])
  86. m, err2 := strconv.Atoi(subs[2])
  87. d, err3 := strconv.Atoi(subs[3])
  88. ns, err4 := strconv.Atoi(subs[4])
  89. if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
  90. return time.Time{}, false
  91. }
  92. return time.Date(y, time.Month(m), d, 0, 0, 0, ns, time.UTC), true
  93. }
  94. // Older reports whether uid was created by m and has a timestamp older than
  95. // the current time by at least d.
  96. func (s *Space) Older(uid string, d time.Duration) bool {
  97. ts, ok := s.Timestamp(uid)
  98. if !ok {
  99. return false
  100. }
  101. return time.Since(ts) > d
  102. }