@@ -58,7 +58,7 @@ | |||||
1.js Number 类型最大数值:9007199254740992,本算法在保持并发性能(5W+/0.01s)和最大64个 WorkerId(6bit)的同时,能用70年才到 js Number Max 值。 | 1.js Number 类型最大数值:9007199254740992,本算法在保持并发性能(5W+/0.01s)和最大64个 WorkerId(6bit)的同时,能用70年才到 js Number Max 值。 | ||||
2.增加WorkerId位数到8bit(128节点)时,15年达到 js Number Max 值。 | |||||
2.增加WorkerId位数到8bit(256节点)时,15年达到 js Number Max 值。 | |||||
3.极致性能:500W/1s。 | 3.极致性能:500W/1s。 | ||||
@@ -167,14 +167,14 @@ | |||||
``` | ``` | ||||
// 全局初始化设置WorkerId,默认最大2^16-1。(初始化过程全局只需一次,且必须最先设置) | // 全局初始化设置WorkerId,默认最大2^16-1。(初始化过程全局只需一次,且必须最先设置) | ||||
var options = new IdGeneratorOptions(){ WorkerId = 1}; | var options = new IdGeneratorOptions(){ WorkerId = 1}; | ||||
YidHelper.SetIdGenerator(options); | |||||
IdHelper.SetIdGenerator(options); | |||||
// 初始化以后,就可以在需要的地方调用方法生成ID。 | // 初始化以后,就可以在需要的地方调用方法生成ID。 | ||||
var newId = YidHelper.NextId(); | |||||
var newId = IdHelper.NextId(); | |||||
// 可通过 YidHelper.IdGenInstance 订阅 GenIdActionAsync 事件。 | |||||
// 可通过 IdHelper.IdGenInstance 订阅 GenIdActionAsync 事件。 | |||||
``` | ``` | ||||
如果基于DI框架集成,可以参考 YidHelper 去管理 IdGenerator 对象,必须使用**单例**模式。 | |||||
如果基于DI框架集成,可以参考 IdHelper 去管理 IdGenerator 对象,必须使用**单例**模式。 | |||||
#### options说明 | #### options说明 | ||||
``` | ``` | ||||
@@ -44,13 +44,13 @@ namespace Yitter.OrgSystem.TestA | |||||
WorkerId = 1, | WorkerId = 1, | ||||
TopOverCostCount = 2000, | TopOverCostCount = 2000, | ||||
//WorkerIdBitLength = 6, | |||||
//SeqBitLength = 6, | |||||
WorkerIdBitLength = 10, | |||||
SeqBitLength = 6, | |||||
//MinSeqNumber = 11, | //MinSeqNumber = 11, | ||||
//MaxSeqNumber = 200, | //MaxSeqNumber = 200, | ||||
//StartTime = DateTime.Now.AddYears(-1), | |||||
StartTime = DateTime.Now.AddYears(-5), | |||||
}; | }; | ||||
// ++++++++++++++++++++++++++++++++ | // ++++++++++++++++++++++++++++++++ | ||||
@@ -59,7 +59,7 @@ namespace Yitter.OrgSystem.TestA | |||||
IdGeneratorOptions options1 = (newConfig); | IdGeneratorOptions options1 = (newConfig); | ||||
if (IdGen == null) | if (IdGen == null) | ||||
{ | { | ||||
IdGen = new YidGenerator(options1); | |||||
IdGen = new DefaultIdGenerator(options1); | |||||
} | } | ||||
if (outputLog) | if (outputLog) | ||||
@@ -105,7 +105,7 @@ namespace Yitter.OrgSystem.TestA | |||||
}; | }; | ||||
Console.WriteLine("Gen:" + i); | Console.WriteLine("Gen:" + i); | ||||
var idGen2 = new YidGenerator(options); | |||||
var idGen2 = new DefaultIdGenerator(options); | |||||
var test = new GenTest(idGen2, genIdCount, i); | var test = new GenTest(idGen2, genIdCount, i); | ||||
if (outputLog) | if (outputLog) | ||||
@@ -13,8 +13,15 @@ namespace Yitter.IdGenerator | |||||
{ | { | ||||
public interface IIdGenerator | public interface IIdGenerator | ||||
{ | { | ||||
/// <summary> | |||||
/// 生成过程中产生的事件 | |||||
/// </summary> | |||||
Action<OverCostActionArg> GenIdActionAsync { get; set; } | Action<OverCostActionArg> GenIdActionAsync { get; set; } | ||||
/// <summary> | |||||
/// 生成新的long型Id | |||||
/// </summary> | |||||
/// <returns></returns> | |||||
long NewLong(); | long NewLong(); | ||||
// Guid NewGuid(); | // Guid NewGuid(); |
@@ -13,13 +13,35 @@ using System.Text; | |||||
namespace Yitter.IdGenerator | namespace Yitter.IdGenerator | ||||
{ | { | ||||
/// <summary> | |||||
/// Id生成时回调参数 | |||||
/// </summary> | |||||
public class OverCostActionArg | public class OverCostActionArg | ||||
{ | { | ||||
/// <summary> | |||||
/// 事件类型 | |||||
/// 1-开始,2-结束,8-漂移 | |||||
/// </summary> | |||||
public int ActionType { get; set; } | public int ActionType { get; set; } | ||||
/// <summary> | |||||
/// 时间戳 | |||||
/// </summary> | |||||
public long TimeTick { get; set; } | public long TimeTick { get; set; } | ||||
/// <summary> | |||||
/// 机器码 | |||||
/// </summary> | |||||
public ushort WorkerId { get; set; } | public ushort WorkerId { get; set; } | ||||
/// <summary> | |||||
/// 漂移计算次数 | |||||
/// </summary> | |||||
public int OverCostCountInOneTerm { get; set; } | public int OverCostCountInOneTerm { get; set; } | ||||
/// <summary> | |||||
/// 漂移期间生产ID个数 | |||||
/// </summary> | |||||
public int GenCountInOneTerm { get; set; } | public int GenCountInOneTerm { get; set; } | ||||
/// <summary> | |||||
/// 漂移周期 | |||||
/// </summary> | |||||
public int TermIndex { get; set; } | public int TermIndex { get; set; } | ||||
public OverCostActionArg(ushort workerId, long timeTick, int actionType = 0, int overCostCountInOneTerm = 0, int genCountWhenOverCost = 0,int index=0) | public OverCostActionArg(ushort workerId, long timeTick, int actionType = 0, int overCostCountInOneTerm = 0, int genCountWhenOverCost = 0,int index=0) |
@@ -14,7 +14,10 @@ using System.Threading; | |||||
namespace Yitter.IdGenerator | namespace Yitter.IdGenerator | ||||
{ | { | ||||
public class YidGenerator : IIdGenerator | |||||
/// <summary> | |||||
/// 默认实现 | |||||
/// </summary> | |||||
public class DefaultIdGenerator : IIdGenerator | |||||
{ | { | ||||
private ISnowWorker _SnowWorker { get; set; } | private ISnowWorker _SnowWorker { get; set; } | ||||
@@ -25,7 +28,7 @@ namespace Yitter.IdGenerator | |||||
} | } | ||||
public YidGenerator(IdGeneratorOptions options) | |||||
public DefaultIdGenerator(IdGeneratorOptions options) | |||||
{ | { | ||||
if (options == null) | if (options == null) | ||||
{ | { | ||||
@@ -45,24 +48,24 @@ namespace Yitter.IdGenerator | |||||
var maxWorkerIdNumber = Math.Pow(2, options.WorkerIdBitLength) - 1; | var maxWorkerIdNumber = Math.Pow(2, options.WorkerIdBitLength) - 1; | ||||
if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber) | if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber) | ||||
{ | { | ||||
throw new ApplicationException("WorkerId is error. (range:[1, " + maxWorkerIdNumber + "]"); | |||||
throw new ApplicationException("WorkerId error. (range:[1, " + maxWorkerIdNumber + "]"); | |||||
} | } | ||||
if (options.SeqBitLength < 2 || options.SeqBitLength > 21) | if (options.SeqBitLength < 2 || options.SeqBitLength > 21) | ||||
{ | { | ||||
throw new ApplicationException("SeqBitLength is error. (range:[2, 21])"); | |||||
throw new ApplicationException("SeqBitLength error. (range:[2, 21])"); | |||||
} | } | ||||
var maxSeqNumber = Math.Pow(2, options.SeqBitLength) - 1; | var maxSeqNumber = Math.Pow(2, options.SeqBitLength) - 1; | ||||
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) | if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) | ||||
{ | { | ||||
throw new ApplicationException("MaxSeqNumber is error. (range:[1, " + maxSeqNumber + "]"); | |||||
throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]"); | |||||
} | } | ||||
var maxValue = maxSeqNumber - 2; | var maxValue = maxSeqNumber - 2; | ||||
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue) | if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue) | ||||
{ | { | ||||
throw new ApplicationException("MinSeqNumber is error. (range:[5, " + maxValue + "]"); | |||||
throw new ApplicationException("MinSeqNumber error. (range:[5, " + maxValue + "]"); | |||||
} | } | ||||
switch (options.Method) | switch (options.Method) |
@@ -16,17 +16,11 @@ namespace Yitter.IdGenerator | |||||
/// <summary> | /// <summary> | ||||
/// 这是一个调用的例子,默认情况下,单机集成者可以直接使用 NewId()。 | /// 这是一个调用的例子,默认情况下,单机集成者可以直接使用 NewId()。 | ||||
/// </summary> | /// </summary> | ||||
public class YidHelper | |||||
public class IdHelper | |||||
{ | { | ||||
private static IIdGenerator _IdGenInstance = null; | private static IIdGenerator _IdGenInstance = null; | ||||
public static IIdGenerator IdGenInstance | |||||
{ | |||||
get | |||||
{ | |||||
return _IdGenInstance; | |||||
} | |||||
} | |||||
public static IIdGenerator IdGenInstance => _IdGenInstance; | |||||
/// <summary> | /// <summary> | ||||
/// 设置参数,建议程序初始化时执行一次 | /// 设置参数,建议程序初始化时执行一次 | ||||
@@ -34,7 +28,7 @@ namespace Yitter.IdGenerator | |||||
/// <param name="options"></param> | /// <param name="options"></param> | ||||
public static void SetIdGenerator(IdGeneratorOptions options) | public static void SetIdGenerator(IdGeneratorOptions options) | ||||
{ | { | ||||
_IdGenInstance = new YidGenerator(options); | |||||
_IdGenInstance = new DefaultIdGenerator(options); | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -47,7 +41,9 @@ namespace Yitter.IdGenerator | |||||
{ | { | ||||
if (_IdGenInstance == null) | if (_IdGenInstance == null) | ||||
{ | { | ||||
_IdGenInstance = new YidGenerator(new IdGeneratorOptions() { WorkerId = 1 }); | |||||
_IdGenInstance = new DefaultIdGenerator( | |||||
new IdGeneratorOptions() { WorkerId = 1 } | |||||
); | |||||
} | } | ||||
return _IdGenInstance.NewLong(); | return _IdGenInstance.NewLong(); |
@@ -12,7 +12,4 @@ | |||||
<WarningLevel>5</WarningLevel> | <WarningLevel>5</WarningLevel> | ||||
</PropertyGroup> | </PropertyGroup> | ||||
<ItemGroup> | |||||
</ItemGroup> | |||||
</Project> | </Project> |