Snowflake.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using YiSha.Util;
  7. namespace YiSha.IdGenerator
  8. {
  9. public class Snowflake
  10. {
  11. private const long TwEpoch = 1546272000000L;//2019-01-01 00:00:00
  12. private const int WorkerIdBits = 5;
  13. private const int DatacenterIdBits = 5;
  14. private const int SequenceBits = 12;
  15. private const long MaxWorkerId = -1L ^ (-1L << WorkerIdBits);
  16. private const long MaxDatacenterId = -1L ^ (-1L << DatacenterIdBits);
  17. private const int WorkerIdShift = SequenceBits;
  18. private const int DatacenterIdShift = SequenceBits + WorkerIdBits;
  19. private const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits;
  20. private const long SequenceMask = -1L ^ (-1L << SequenceBits);
  21. private long _sequence = 0L;
  22. private long _lastTimestamp = -1L;
  23. /// <summary>
  24. ///10位的数据机器位中的高位
  25. /// </summary>
  26. public long WorkerId { get; protected set; }
  27. /// <summary>
  28. /// 10位的数据机器位中的低位
  29. /// </summary>
  30. public long DatacenterId { get; protected set; }
  31. private readonly object _lock = new object();
  32. /// <summary>
  33. /// 基于Twitter的snowflake算法
  34. /// </summary>
  35. /// <param name="workerId">10位的数据机器位中的高位,默认不应该超过5位(5byte)</param>
  36. /// <param name="datacenterId"> 10位的数据机器位中的低位,默认不应该超过5位(5byte)</param>
  37. /// <param name="sequence">初始序列</param>
  38. public Snowflake(long workerId, long datacenterId, long sequence = 0L)
  39. {
  40. WorkerId = workerId;
  41. DatacenterId = datacenterId;
  42. _sequence = sequence;
  43. if (workerId > MaxWorkerId || workerId < 0)
  44. {
  45. throw new ArgumentException($"worker Id can't be greater than {MaxWorkerId} or less than 0");
  46. }
  47. if (datacenterId > MaxDatacenterId || datacenterId < 0)
  48. {
  49. throw new ArgumentException($"datacenter Id can't be greater than {MaxDatacenterId} or less than 0");
  50. }
  51. }
  52. public long CurrentId { get; private set; }
  53. /// <summary>
  54. /// 获取下一个Id,该方法线程安全
  55. /// </summary>
  56. /// <returns></returns>
  57. public long NextId()
  58. {
  59. lock (_lock)
  60. {
  61. var timestamp = DateTimeHelper.GetUnixTimeStamp(DateTime.Now);
  62. if (timestamp < _lastTimestamp)
  63. {
  64. //TODO 是否可以考虑直接等待?
  65. throw new Exception(
  66. $"Clock moved backwards or wrapped around. Refusing to generate id for {_lastTimestamp - timestamp} ticks");
  67. }
  68. if (_lastTimestamp == timestamp)
  69. {
  70. _sequence = (_sequence + 1) & SequenceMask;
  71. if (_sequence == 0)
  72. {
  73. timestamp = TilNextMillis(_lastTimestamp);
  74. }
  75. }
  76. else
  77. {
  78. _sequence = 0;
  79. }
  80. _lastTimestamp = timestamp;
  81. CurrentId = ((timestamp - TwEpoch) << TimestampLeftShift) |
  82. (DatacenterId << DatacenterIdShift) |
  83. (WorkerId << WorkerIdShift) | _sequence;
  84. return CurrentId;
  85. }
  86. }
  87. private long TilNextMillis(long lastTimestamp)
  88. {
  89. var timestamp = DateTimeHelper.GetUnixTimeStamp(DateTime.Now);
  90. while (timestamp <= lastTimestamp)
  91. {
  92. timestamp = DateTimeHelper.GetUnixTimeStamp(DateTime.Now);
  93. }
  94. return timestamp;
  95. }
  96. }
  97. }