twitter的snowflake算法(C#版本)
时间: 2023-12-16 15:06:01 浏览: 148
以下是 C# 版本的 Twitter Snowflake 算法实现:
```csharp
public class Snowflake
{
private static readonly DateTime Twepoch = new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private const int WorkerIdBits = 5;
private const int DatacenterIdBits = 5;
private const int SequenceBits = 12;
private const long MaxWorkerId = -1L ^ (-1L << WorkerIdBits);
private const long MaxDatacenterId = -1L ^ (-1L << DatacenterIdBits);
private const long SequenceMask = -1L ^ (-1L << SequenceBits);
private const int WorkerIdShift = SequenceBits;
private const int DatacenterIdShift = SequenceBits + WorkerIdBits;
private const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits;
private readonly object _lock = new object();
private readonly long _workerId;
private readonly long _datacenterId;
private long _sequence = 0L;
private long _lastTimestamp = -1L;
public Snowflake(long workerId, long datacenterId)
{
if (workerId > MaxWorkerId || workerId < 0)
{
throw new ArgumentException($"worker Id can't be greater than {MaxWorkerId} or less than 0");
}
if (datacenterId > MaxDatacenterId || datacenterId < 0)
{
throw new ArgumentException($"datacenter Id can't be greater than {MaxDatacenterId} or less than 0");
}
_workerId = workerId;
_datacenterId = datacenterId;
}
public long NextId()
{
lock (_lock)
{
var timestamp = TimeGen();
if (_lastTimestamp == timestamp)
{
_sequence = (_sequence + 1) & SequenceMask;
if (_sequence == 0)
{
timestamp = TilNextMillis(_lastTimestamp);
}
}
else
{
_sequence = 0L;
}
if (timestamp < _lastTimestamp)
{
throw new Exception($"Clock moved backwards. Refusing to generate id for {_lastTimestamp - timestamp} milliseconds");
}
_lastTimestamp = timestamp;
return ((timestamp - Twepoch.Ticks) << TimestampLeftShift) |
(_datacenterId << DatacenterIdShift) |
(_workerId << WorkerIdShift) |
_sequence;
}
}
private static long TilNextMillis(long lastTimestamp)
{
var timestamp = TimeGen();
while (timestamp <= lastTimestamp)
{
timestamp = TimeGen();
}
return timestamp;
}
private static long TimeGen() => DateTime.UtcNow.Ticks;
}
```
使用示例:
```csharp
var snowflake = new Snowflake(workerId: 1, datacenterId: 1);
var id = snowflake.NextId();
```
在上面的示例中,`workerId` 和 `datacenterId` 分别表示工作节点 ID 和数据中心 ID,这两个参数需要根据实际情况进行配置。调用 `NextId()` 方法即可生成一个唯一的 Snowflake ID。
阅读全文