You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB
C#

3 years ago
/*
* yitter(yitter@126.com)
* https://gitee.com/yitter/idgenerator
* MIT
* 使
*
*
*/
using System;
3 years ago
namespace Common.Snowflake
3 years ago
{
/// <summary>
/// 常规雪花算法
/// </summary>
internal class SnowWorkerM2 : SnowWorkerM1
{
public SnowWorkerM2(IdGeneratorOptions options) : base(options)
{
}
public override long NextId()
{
lock (_SyncLock)
{
long currentTimeTick = GetCurrentTimeTick();
if (_LastTimeTick == currentTimeTick)
{
if (_CurrentSeqNumber++ > MaxSeqNumber)
{
_CurrentSeqNumber = MinSeqNumber;
currentTimeTick = GetNextTimeTick();
}
}
else
{
_CurrentSeqNumber = MinSeqNumber;
}
if (currentTimeTick < _LastTimeTick)
{
throw new Exception(string.Format("Time error for {0} milliseconds", _LastTimeTick - currentTimeTick));
}
_LastTimeTick = currentTimeTick;
var result = ((currentTimeTick << _TimestampShift) + ((long)WorkerId << SeqBitLength) + _CurrentSeqNumber);
return result;
}
}
}
}