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.3 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 代码编辑:guoyahao
  4. * 代码修订:yitter
  5. * 开源地址:https://gitee.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. var snowWorker ISnowWorker
  58. switch options.Method {
  59. case 1:
  60. snowWorker = NewSnowWorkerM1(options)
  61. case 2:
  62. snowWorker = NewSnowWorkerM2(options)
  63. default:
  64. snowWorker = NewSnowWorkerM1(options)
  65. }
  66. if options.Method == 1 {
  67. time.Sleep(time.Duration(500) * time.Microsecond)
  68. }
  69. return &DefaultIdGenerator{
  70. Options: options,
  71. SnowWorker: snowWorker,
  72. }
  73. }
  74. func (dig DefaultIdGenerator) NewLong() int64 {
  75. return dig.SnowWorker.NextId()
  76. }

雪花算法中非常好用的数字ID生成器