SnowFlake.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Reflection.PortableExecutable;
  2. namespace ApiDemo
  3. {
  4. /// <summary>
  5. /// 雪花算法生成id
  6. /// </summary>
  7. public class SnowFlake
  8. {
  9. /// <summary>
  10. /// 机器id所占的位数
  11. /// </summary>
  12. private const int workerIdBits = 5;
  13. /// <summary>
  14. /// 数据表述id所占位数
  15. /// </summary>
  16. private const int datacenterIdBits = 5;
  17. /// <summary>
  18. /// 支持的最大机器id,结果是31
  19. /// </summary>
  20. private const long maxWorkerId = -1L ^ (-1L << workerIdBits);
  21. /// <summary>
  22. /// 支持的最大数据表示id,结果是31
  23. /// </summary>
  24. private const long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
  25. /// <summary>
  26. /// 序列在id中所占的位数
  27. /// </summary>
  28. private const int sequenceBits = 12;
  29. /// <summary>
  30. /// 数据标识id向左移17位
  31. /// </summary>
  32. private const int datacenterIdShift = sequenceBits + workerIdBits;
  33. /// <summary>
  34. /// 机器ID向左移12位
  35. /// </summary>
  36. private const int workerIdShift = sequenceBits;
  37. private const int timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
  38. /// <summary>
  39. /// 生成序列的掩码
  40. /// </summary>
  41. private const long sequenceMask = -1L ^ (-1L << sequenceBits);
  42. /// <summary>
  43. /// 数据中心ID(0-31)
  44. /// </summary>
  45. public long DatacenterId { get; private set; }
  46. /// <summary>
  47. /// 工作机器ID(0-31)
  48. /// </summary>
  49. public long WorkerId { get; private set; }
  50. /// <summary>
  51. /// 毫秒内序列(0-4095)
  52. /// </summary>
  53. public long Sequence { get; private set; }
  54. /// <summary>
  55. /// 上次生成ID的时间戳
  56. /// </summary>
  57. public long LastTimestamp { get; private set; }
  58. /// <summary>
  59. /// 开始时间戳。首次使用前设置,否则无效,默认2010-1-1
  60. /// </summary>
  61. public DateTime StartTimestamp { get; set; } = new DateTime(2010,1,1);
  62. static object syncRoot = new object();
  63. static readonly Lazy<SnowFlake> snowflake = new(() => new SnowFlake(0L,0L));
  64. /// <summary>
  65. /// 默认金泰实例,WorkerId = 0,DatacenterId = 0
  66. /// </summary>
  67. public static SnowFlake Instance { get; } = snowflake.Value;
  68. /// <summary>
  69. /// 构造函数
  70. /// </summary>
  71. /// <param name="workerId">工作机器ID</param>
  72. /// <param name="datacenterId">数据中心ID</param>
  73. /// <exception cref="ArgumentOutOfRangeException"></exception>
  74. public SnowFlake(long workerId,long datacenterId)
  75. {
  76. if (workerId > maxWorkerId || workerId < 0)
  77. {
  78. throw new ArgumentOutOfRangeException(nameof(workerId),$"不能大于{maxWorkerId}或小于0");
  79. }
  80. if (datacenterId > maxDatacenterId || datacenterId < 0)
  81. {
  82. throw new ArgumentOutOfRangeException(nameof(datacenterId), $"不能大于{maxDatacenterId}或小于0");
  83. }
  84. WorkerId = workerId;
  85. DatacenterId = datacenterId;
  86. Sequence = 0L;
  87. LastTimestamp = -1L;
  88. }
  89. /// <summary>
  90. /// 获取下一个ID
  91. /// </summary>
  92. /// <returns></returns>
  93. public long NextId()
  94. {
  95. lock (syncRoot)
  96. {
  97. //获取当前时间戳
  98. long timestamp = GetCurrentTimestamp();
  99. if (timestamp > LastTimestamp)//时间戳改变,毫秒内序列重置
  100. {
  101. Sequence = 0L;
  102. }
  103. else if (timestamp == LastTimestamp)//如果是同一时间生成的,则进行毫秒内序列
  104. {
  105. Sequence = (Sequence + 1) & sequenceMask;
  106. if (Sequence == 0)//毫秒内序列溢出
  107. {
  108. timestamp = GetNextTimestamp(LastTimestamp);//阻塞到下一个毫秒,获取新的时间戳
  109. }
  110. }
  111. else//当前时间小于上一次ID生成的时间戳,证明系统时钟被回拨,此时需要做回拨处理
  112. {
  113. Sequence = (Sequence + 1) & sequenceMask;
  114. if (Sequence > 0)
  115. {
  116. timestamp = LastTimestamp;//停留再最后一次时间戳上,等待系统时间追上后即完全度过了时钟回拨问题
  117. }
  118. else {
  119. timestamp = LastTimestamp + 1;//直接进位到下一个毫秒
  120. }
  121. }
  122. LastTimestamp = timestamp;//上次生成ID的时间戳
  123. //移动并通过或运算拼到一起组成64位的ID
  124. var id = (timestamp << timestampLeftShift)
  125. | (DatacenterId << datacenterIdShift)
  126. | (WorkerId << workerIdShift)
  127. | Sequence;
  128. return id;
  129. }
  130. }
  131. /// <summary>
  132. /// 阻塞到下一个毫秒,直到获得新的时间戳
  133. /// </summary>
  134. /// <param name="lastTimestamp"></param>
  135. /// <returns></returns>
  136. private long GetNextTimestamp(long lastTimestamp)
  137. {
  138. //获取当前时间戳
  139. long timestamp = GetCurrentTimestamp();
  140. while (timestamp <= lastTimestamp)
  141. {
  142. timestamp = GetCurrentTimestamp();
  143. }
  144. return timestamp;
  145. }
  146. /// <summary>
  147. /// 获取当前时间戳
  148. /// </summary>
  149. /// <returns></returns>
  150. private long GetCurrentTimestamp()
  151. {
  152. return (long)(DateTime.Now - StartTimestamp).TotalMilliseconds;
  153. }
  154. }
  155. }