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.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
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
2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 代码编辑:guoyahao
  4. * 代码修订:yitter
  5. * 开源地址:https://github.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.TopOverCostCount
  67. var topOverCostCount = options.TopOverCostCount
  68. // if topOverCostCount == 0 {
  69. // topOverCostCount = 2000
  70. // }
  71. // 8.Others
  72. timestampShift := (byte)(workerIdBitLength + seqBitLength)
  73. currentSeqNumber := minSeqNumber
  74. return &SnowWorkerM1{
  75. BaseTime: baseTime,
  76. WorkerIdBitLength: workerIdBitLength,
  77. WorkerId: workerId,
  78. SeqBitLength: seqBitLength,
  79. MaxSeqNumber: maxSeqNumber,
  80. MinSeqNumber: minSeqNumber,
  81. TopOverCostCount: topOverCostCount,
  82. _TimestampShift: timestampShift,
  83. _CurrentSeqNumber: currentSeqNumber,
  84. _LastTimeTick: 0,
  85. _TurnBackTimeTick: 0,
  86. _TurnBackIndex: 0,
  87. _IsOverCost: false,
  88. _OverCostCountInOneTerm: 0,
  89. // _GenCountInOneTerm: 0,
  90. // _TermIndex: 0,
  91. }
  92. }
  93. // DoGenIDAction .
  94. func (m1 *SnowWorkerM1) DoGenIdAction(arg *OverCostActionArg) {
  95. }
  96. func (m1 *SnowWorkerM1) BeginOverCostAction(useTimeTick int64) {
  97. }
  98. func (m1 *SnowWorkerM1) EndOverCostAction(useTimeTick int64) {
  99. // if m1._TermIndex > 10000 {
  100. // m1._TermIndex = 0
  101. // }
  102. }
  103. func (m1 *SnowWorkerM1) BeginTurnBackAction(useTimeTick int64) {
  104. }
  105. func (m1 *SnowWorkerM1) EndTurnBackAction(useTimeTick int64) {
  106. }
  107. func (m1 *SnowWorkerM1) NextOverCostId() int64 {
  108. currentTimeTick := m1.GetCurrentTimeTick()
  109. if currentTimeTick > m1._LastTimeTick {
  110. // m1.EndOverCostAction(currentTimeTick)
  111. m1._LastTimeTick = currentTimeTick
  112. m1._CurrentSeqNumber = m1.MinSeqNumber
  113. m1._IsOverCost = false
  114. m1._OverCostCountInOneTerm = 0
  115. // m1._GenCountInOneTerm = 0
  116. return m1.CalcId(m1._LastTimeTick)
  117. }
  118. if m1._OverCostCountInOneTerm >= m1.TopOverCostCount {
  119. // m1.EndOverCostAction(currentTimeTick)
  120. m1._LastTimeTick = m1.GetNextTimeTick()
  121. m1._CurrentSeqNumber = m1.MinSeqNumber
  122. m1._IsOverCost = false
  123. m1._OverCostCountInOneTerm = 0
  124. // m1._GenCountInOneTerm = 0
  125. return m1.CalcId(m1._LastTimeTick)
  126. }
  127. if m1._CurrentSeqNumber > m1.MaxSeqNumber {
  128. m1._LastTimeTick++
  129. m1._CurrentSeqNumber = m1.MinSeqNumber
  130. m1._IsOverCost = true
  131. m1._OverCostCountInOneTerm++
  132. // m1._GenCountInOneTerm++
  133. return m1.CalcId(m1._LastTimeTick)
  134. }
  135. // m1._GenCountInOneTerm++
  136. return m1.CalcId(m1._LastTimeTick)
  137. }
  138. // NextNormalID .
  139. func (m1 *SnowWorkerM1) NextNormalId() int64 {
  140. currentTimeTick := m1.GetCurrentTimeTick()
  141. if currentTimeTick < m1._LastTimeTick {
  142. if m1._TurnBackTimeTick < 1 {
  143. m1._TurnBackTimeTick = m1._LastTimeTick - 1
  144. m1._TurnBackIndex++
  145. // 每毫秒序列数的前5位是预留位,0用于手工新值,1-4是时间回拨次序
  146. // 支持4次回拨次序(避免回拨重叠导致ID重复),可无限次回拨(次序循环使用)。
  147. if m1._TurnBackIndex > 4 {
  148. m1._TurnBackIndex = 1
  149. }
  150. m1.BeginTurnBackAction(m1._TurnBackTimeTick)
  151. }
  152. // time.Sleep(time.Duration(1) * time.Millisecond)
  153. return m1.CalcTurnBackId(m1._TurnBackTimeTick)
  154. }
  155. // 时间追平时,_TurnBackTimeTick清零
  156. if m1._TurnBackTimeTick > 0 {
  157. m1.EndTurnBackAction(m1._TurnBackTimeTick)
  158. m1._TurnBackTimeTick = 0
  159. }
  160. if currentTimeTick > m1._LastTimeTick {
  161. m1._LastTimeTick = currentTimeTick
  162. m1._CurrentSeqNumber = m1.MinSeqNumber
  163. return m1.CalcId(m1._LastTimeTick)
  164. }
  165. if m1._CurrentSeqNumber > m1.MaxSeqNumber {
  166. m1.BeginOverCostAction(currentTimeTick)
  167. // m1._TermIndex++
  168. m1._LastTimeTick++
  169. m1._CurrentSeqNumber = m1.MinSeqNumber
  170. m1._IsOverCost = true
  171. m1._OverCostCountInOneTerm = 1
  172. // m1._GenCountInOneTerm = 1
  173. return m1.CalcId(m1._LastTimeTick)
  174. }
  175. return m1.CalcId(m1._LastTimeTick)
  176. }
  177. // CalcID .
  178. func (m1 *SnowWorkerM1) CalcId(useTimeTick int64) int64 {
  179. result := int64(useTimeTick<<m1._TimestampShift) + int64(m1.WorkerId<<m1.SeqBitLength) + int64(m1._CurrentSeqNumber)
  180. m1._CurrentSeqNumber++
  181. return result
  182. }
  183. // CalcTurnBackID .
  184. func (m1 *SnowWorkerM1) CalcTurnBackId(useTimeTick int64) int64 {
  185. result := int64(useTimeTick<<m1._TimestampShift) + int64(m1.WorkerId<<m1.SeqBitLength) + int64(m1._TurnBackIndex)
  186. m1._TurnBackTimeTick--
  187. return result
  188. }
  189. // GetCurrentTimeTick .
  190. func (m1 *SnowWorkerM1) GetCurrentTimeTick() int64 {
  191. var millis = time.Now().UnixNano() / 1e6
  192. return millis - m1.BaseTime
  193. }
  194. // GetNextTimeTick .
  195. func (m1 *SnowWorkerM1) GetNextTimeTick() int64 {
  196. tempTimeTicker := m1.GetCurrentTimeTick()
  197. for tempTimeTicker <= m1._LastTimeTick {
  198. time.Sleep(time.Duration(1) * time.Millisecond)
  199. tempTimeTicker = m1.GetCurrentTimeTick()
  200. }
  201. return tempTimeTicker
  202. }
  203. // NextId .
  204. func (m1 *SnowWorkerM1) NextId() int64 {
  205. m1.Lock()
  206. defer m1.Unlock()
  207. if m1._IsOverCost {
  208. return m1.NextOverCostId()
  209. } else {
  210. return m1.NextNormalId()
  211. }
  212. }