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

DefaultIdGenerator.go 2.6 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 代码编辑:guoyahao
  4. * 代码修订:yitter
  5. * 开源地址:https://github.com/yitter/idgenerator
  6. */
  7. package idgen
  8. import (
  9. "strconv"
  10. "time"
  11. )
  12. type DefaultIdGenerator struct {
  13. Options *IdGeneratorOptions
  14. SnowWorker ISnowWorker
  15. IdGeneratorException IdGeneratorException
  16. }
  17. func NewDefaultIdGenerator(options *IdGeneratorOptions) *DefaultIdGenerator {
  18. if options == nil {
  19. panic("dig.Options error.")
  20. }
  21. // 1.BaseTime
  22. minTime := int64(631123200000) // time.Now().AddDate(-30, 0, 0).UnixNano() / 1e6
  23. if options.BaseTime < minTime || options.BaseTime > time.Now().UnixNano()/1e6 {
  24. panic("BaseTime error.")
  25. }
  26. // 2.WorkerIdBitLength
  27. if options.WorkerIdBitLength <= 0 {
  28. panic("WorkerIdBitLength error.(range:[1, 21])")
  29. }
  30. if options.WorkerIdBitLength+options.SeqBitLength > 22 {
  31. panic("error:WorkerIdBitLength + SeqBitLength <= 22")
  32. }
  33. // 3.WorkerId
  34. maxWorkerIdNumber := uint16(1<<options.WorkerIdBitLength) - 1
  35. if maxWorkerIdNumber == 0 {
  36. maxWorkerIdNumber = 63
  37. }
  38. if options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber {
  39. panic("WorkerId error. (range:[0, " + strconv.FormatUint(uint64(maxWorkerIdNumber), 10) + "]")
  40. }
  41. // 4.SeqBitLength
  42. if options.SeqBitLength < 2 || options.SeqBitLength > 21 {
  43. panic("SeqBitLength error. (range:[2, 21])")
  44. }
  45. // 5.MaxSeqNumber
  46. maxSeqNumber := uint32(1<<options.SeqBitLength) - 1
  47. if maxSeqNumber == 0 {
  48. maxSeqNumber = 63
  49. }
  50. if options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber {
  51. panic("MaxSeqNumber error. (range:[1, " + strconv.FormatUint(uint64(maxSeqNumber), 10) + "]")
  52. }
  53. // 6.MinSeqNumber
  54. if options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber {
  55. panic("MinSeqNumber error. (range:[5, " + strconv.FormatUint(uint64(maxSeqNumber), 10) + "]")
  56. }
  57. // 7.TopOverCostCount
  58. if options.TopOverCostCount < 0 || options.TopOverCostCount > 10000 {
  59. panic("TopOverCostCount error. (range:[0, 10000]")
  60. }
  61. var snowWorker ISnowWorker
  62. switch options.Method {
  63. case 1:
  64. snowWorker = NewSnowWorkerM1(options)
  65. case 2:
  66. snowWorker = NewSnowWorkerM2(options)
  67. default:
  68. snowWorker = NewSnowWorkerM1(options)
  69. }
  70. if options.Method == 1 {
  71. time.Sleep(time.Duration(500) * time.Microsecond)
  72. }
  73. return &DefaultIdGenerator{
  74. Options: options,
  75. SnowWorker: snowWorker,
  76. }
  77. }
  78. func (dig DefaultIdGenerator) NewLong() int64 {
  79. return dig.SnowWorker.NextId()
  80. }
  81. func (dig DefaultIdGenerator) ExtractTime(id int64) time.Time {
  82. return time.UnixMilli(id>>(dig.Options.WorkerIdBitLength+dig.Options.SeqBitLength) + dig.Options.BaseTime)
  83. }