Skip to content

Conversation

@SxxSolar
Copy link

更新 IdUtil.SnowflakeId()

/// <summary>
/// 生成Snowflake ID
/// </summary>
/// <returns>生成的Snowflake ID</returns>
public static long SnowflakeId()
{
	lock (lockObj)
	{
		long timestamp = DateTime.UtcNow.Ticks - epochTicks;

		if (timestamp < lastTimestamp)
		{
			throw new Exception("时钟向后移动,拒绝生成Snowflake ID");
		}

		if (timestamp == lastTimestamp)
		{
			sequence = (sequence + 1) & sequenceMask;

			if (sequence == 0)
			{
				timestamp = NextMillis(lastTimestamp);
			}
		}
		else
		{
			sequence = 0L;
		}

		lastTimestamp = timestamp;

		return ((timestamp << (int)(workerIdBits + sequenceBits)) |
		   (datacenterId << (int)sequenceBits) |
		   (workerId << (int)sequenceBits) |
		   sequence) & long.MaxValue; // 添加 & long.MaxValue
	}
}

修复 生成Snowflake ID为负数的问题
添加测试方法

更新 IdUtil.SnowflakeId()
修复 生成Snowflake ID为负数的问题
添加测试方法
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant