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.

SnowWorkerM1.go 6.1 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 代码编辑:guoyahao
  4. * 代码修订:yitter
  5. * 开源地址:https://gitee.com/yitter/idgenerator
  6. */
  7. package idgen
  8. import (
  9. "sync"
  10. "time"
  11. )
  12. // SnowWorkerM1 .
  13. type SnowWorkerM1 struct {
  14. BaseTime int64 //基础时间
  15. WorkerId uint16 //机器码
  16. WorkerIdBitLength byte //机器码位长
  17. SeqBitLength byte //自增序列数位长
  18. MaxSeqNumber uint32 //最大序列数(含)
  19. MinSeqNumber uint32 //最小序列数(含)
  20. TopOverCostCount uint32 //最大漂移次数
  21. _TimestampShift byte
  22. _CurrentSeqNumber uint32
  23. _LastTimeTick int64
  24. _TurnBackTimeTick int64
  25. _TurnBackIndex byte
  26. _IsOverCost bool
  27. _OverCostCountInOneTerm uint32
  28. _GenCountInOneTerm uint32
  29. _TermIndex uint32
  30. sync.Mutex
  31. }
  32. // NewSnowWorkerM1 .
  33. func NewSnowWorkerM1(options *IdGeneratorOptions) ISnowWorker {
  34. var workerIdBitLength byte
  35. var seqBitLength byte
  36. var maxSeqNumber uint32
  37. // 1.BaseTime
  38. var baseTime int64
  39. if options.BaseTime != 0 {
  40. baseTime = options.BaseTime
  41. } else {
  42. baseTime = 1582136402000
  43. }
  44. // 2.WorkerIdBitLength
  45. if options.WorkerIdBitLength == 0 {
  46. workerIdBitLength = 6
  47. } else {
  48. workerIdBitLength = options.WorkerIdBitLength
  49. }
  50. // 3.WorkerId
  51. var workerId = options.WorkerId
  52. // 4.SeqBitLength
  53. if options.SeqBitLength == 0 {
  54. seqBitLength = 6
  55. } else {
  56. seqBitLength = options.SeqBitLength
  57. }
  58. // 5.MaxSeqNumber
  59. if options.MaxSeqNumber <= 0 {
  60. maxSeqNumber = (1 << seqBitLength) - 1
  61. } else {
  62. maxSeqNumber = options.MaxSeqNumber
  63. }
  64. // 6.MinSeqNumber
  65. var minSeqNumber = options.MinSeqNumber
  66. // 7.Others
  67. var topOverCostCount = options.TopOverCostCount
  68. if topOverCostCount == 0 {
  69. topOverCostCount = 2000
  70. }
  71. timestampShift := (byte)(workerIdBitLength + seqBitLength)
  72. currentSeqNumber := minSeqNumber
  73. return &SnowWorkerM1{
  74. BaseTime: baseTime,
  75. WorkerIdBitLength: workerIdBitLength,
  76. WorkerId: workerId,
  77. SeqBitLength: seqBitLength,
  78. MaxSeqNumber: maxSeqNumber,
  79. MinSeqNumber: minSeqNumber,
  80. TopOverCostCount: topOverCostCount,
  81. _TimestampShift: timestampShift,
  82. _CurrentSeqNumber: currentSeqNumber,
  83. _LastTimeTick: 0,
  84. _TurnBackTimeTick: 0,
  85. _TurnBackIndex: 0,
  86. _IsOverCost: false,
  87. _OverCostCountInOneTerm: 0,
  88. _GenCountInOneTerm: 0,
  89. _TermIndex: 0,
  90. }
  91. }
  92. // DoGenIDAction .
  93. func (m1 *SnowWorkerM1) DoGenIdAction(arg *OverCostActionArg) {
  94. }
  95. func (m1 *SnowWorkerM1) BeginOverCostAction(useTimeTick int64) {
  96. }
  97. func (m1 *SnowWorkerM1) EndOverCostAction(useTimeTick int64) {
  98. if m1._TermIndex > 10000 {
  99. m1._TermIndex = 0
  100. }
  101. }
  102. func (m1 *SnowWorkerM1) BeginTurnBackAction(useTimeTick int64) {
  103. }
  104. func (m1 *SnowWorkerM1) EndTurnBackAction(useTimeTick int64) {
  105. }
  106. func (m1 *SnowWorkerM1) NextOverCostId() int64 {
  107. currentTimeTick := m1.GetCurrentTimeTick()
  108. if currentTimeTick > m1._LastTimeTick {
  109. m1.EndOverCostAction(currentTimeTick)
  110. m1._LastTimeTick = currentTimeTick
  111. m1._CurrentSeqNumber = m1.MinSeqNumber
  112. m1._IsOverCost = false
  113. m1._OverCostCountInOneTerm = 0
  114. m1._GenCountInOneTerm = 0
  115. return m1.CalcId(m1._LastTimeTick)
  116. }
  117. if m1._OverCostCountInOneTerm >= m1.TopOverCostCount {
  118. m1.EndOverCostAction(currentTimeTick)
  119. m1._LastTimeTick = m1.GetNextTimeTick()
  120. m1._CurrentSeqNumber = m1.MinSeqNumber
  121. m1._IsOverCost = false
  122. m1._OverCostCountInOneTerm = 0
  123. m1._GenCountInOneTerm = 0
  124. return m1.CalcId(m1._LastTimeTick)
  125. }
  126. if m1._CurrentSeqNumber > m1.MaxSeqNumber {
  127. m1._LastTimeTick++
  128. m1._CurrentSeqNumber = m1.MinSeqNumber
  129. m1._IsOverCost = true
  130. m1._OverCostCountInOneTerm++
  131. m1._GenCountInOneTerm++
  132. return m1.CalcId(m1._LastTimeTick)
  133. }
  134. m1._GenCountInOneTerm++
  135. return m1.CalcId(m1._LastTimeTick)
  136. }
  137. // NextNormalID .
  138. func (m1 *SnowWorkerM1) NextNormalId() int64 {
  139. currentTimeTick := m1.GetCurrentTimeTick()
  140. if currentTimeTick < m1._LastTimeTick {
  141. if m1._TurnBackTimeTick < 1 {
  142. m1._TurnBackTimeTick = m1._LastTimeTick - 1
  143. m1._TurnBackIndex++
  144. // 每毫秒序列数的前5位是预留位,0用于手工新值,1-4是时间回拨次序
  145. // 最多4次回拨(防止回拨重叠)
  146. if m1._TurnBackIndex > 4 {
  147. m1._TurnBackIndex = 1
  148. }
  149. m1.BeginTurnBackAction(m1._TurnBackTimeTick)
  150. }
  151. // time.Sleep(time.Duration(1) * time.Millisecond)
  152. return m1.CalcTurnBackId(m1._TurnBackTimeTick)
  153. }
  154. // 时间追平时,_TurnBackTimeTick清零
  155. if m1._TurnBackTimeTick > 0 {
  156. m1.EndTurnBackAction(m1._TurnBackTimeTick)
  157. m1._TurnBackTimeTick = 0
  158. }
  159. if currentTimeTick > m1._LastTimeTick {
  160. m1._LastTimeTick = currentTimeTick
  161. m1._CurrentSeqNumber = m1.MinSeqNumber
  162. return m1.CalcId(m1._LastTimeTick)
  163. }
  164. if m1._CurrentSeqNumber > m1.MaxSeqNumber {
  165. m1.BeginOverCostAction(currentTimeTick)
  166. m1._TermIndex++
  167. m1._LastTimeTick++
  168. m1._CurrentSeqNumber = m1.MinSeqNumber
  169. m1._IsOverCost = true
  170. m1._OverCostCountInOneTerm = 1
  171. m1._GenCountInOneTerm = 1
  172. return m1.CalcId(m1._LastTimeTick)
  173. }
  174. return m1.CalcId(m1._LastTimeTick)
  175. }
  176. // CalcID .
  177. func (m1 *SnowWorkerM1) CalcId(useTimeTick int64) int64 {
  178. result := int64(useTimeTick<<m1._TimestampShift) + int64(m1.WorkerId<<m1.SeqBitLength) + int64(m1._CurrentSeqNumber)
  179. m1._CurrentSeqNumber++
  180. return result
  181. }
  182. // CalcTurnBackID .
  183. func (m1 *SnowWorkerM1) CalcTurnBackId(useTimeTick int64) int64 {
  184. result := int64(useTimeTick<<m1._TimestampShift) + int64(m1.WorkerId<<m1.SeqBitLength) + int64(m1._TurnBackIndex)
  185. m1._TurnBackTimeTick--
  186. return result
  187. }
  188. // GetCurrentTimeTick .
  189. func (m1 *SnowWorkerM1) GetCurrentTimeTick() int64 {
  190. var millis = time.Now().UnixNano() / 1e6
  191. return millis - m1.BaseTime
  192. }
  193. // GetNextTimeTick .
  194. func (m1 *SnowWorkerM1) GetNextTimeTick() int64 {
  195. tempTimeTicker := m1.GetCurrentTimeTick()
  196. for tempTimeTicker <= m1._LastTimeTick {
  197. tempTimeTicker = m1.GetCurrentTimeTick()
  198. }
  199. return tempTimeTicker
  200. }
  201. // NextId .
  202. func (m1 *SnowWorkerM1) NextId() int64 {
  203. m1.Lock()
  204. defer m1.Unlock()
  205. if m1._IsOverCost {
  206. return m1.NextOverCostId()
  207. } else {
  208. return m1.NextNormalId()
  209. }
  210. }

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