Skip to content

Commit ce47bdf

Browse files
author
DwGoing
committed
🦄 refactor: 调整雪花生成器时间戳
1 parent df90b05 commit ce47bdf

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Custom files
77
.DS_Store
88
Config.json
9+
build/
910

1011
# User-specific files
1112
*.rsuser

example/CoreExample/Program.cs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Autofac.Extras.DynamicProxy;
88
using NLog;
99
using DwFramework.Core;
10+
using DwFramework.Core.Generator;
1011
using DwFramework.Core.AOP;
1112

1213
namespace CoreExample
@@ -21,29 +22,38 @@ class A
2122

2223
static async Task Main(string[] args)
2324
{
24-
var host = new ServiceHost();
25-
host.AddJsonConfiguration("Config.json", reloadOnChange: true);
26-
host.ConfigureLogging(builder => builder.UserNLog());
27-
host.ConfigureContainer(builder =>
25+
var s = DateTime.Parse("2020.01.01");
26+
var g = new SnowflakeGenerator(0, s);
27+
while (true)
2828
{
29-
builder.Register(c => new LoggerInterceptor(invocation => (
30-
$"{invocation.TargetType.Name}InvokeLog",
31-
LogLevel.Debug,
32-
"\n========================================\n"
33-
+ $"Method:\t{invocation.Method}\n"
34-
+ $"Args:\t{string.Join('|', invocation.Arguments)}\n"
35-
+ $"Return:\t{invocation.ReturnValue}\n"
36-
+ "========================================"
37-
)));
38-
builder.RegisterType<A>().As<I>().EnableInterfaceInterceptors();
39-
builder.RegisterType<B>().As<I>().EnableInterfaceInterceptors();
40-
});
41-
host.OnHostStarted += provider =>
42-
{
43-
ServiceHost.ParseConfiguration<string>("ConnectionString");
44-
foreach (var item in provider.GetServices<I>()) item.Do(5, 6);
45-
};
46-
await host.RunAsync();
29+
var i = g.GenerateId();
30+
var x = SnowflakeGenerator.DecodeId(i, s);
31+
Console.WriteLine(i);
32+
}
33+
34+
// var host = new ServiceHost();
35+
// host.AddJsonConfiguration("Config.json", reloadOnChange: true);
36+
// host.ConfigureLogging(builder => builder.UserNLog());
37+
// host.ConfigureContainer(builder =>
38+
// {
39+
// builder.Register(c => new LoggerInterceptor(invocation => (
40+
// $"{invocation.TargetType.Name}InvokeLog",
41+
// LogLevel.Debug,
42+
// "\n========================================\n"
43+
// + $"Method:\t{invocation.Method}\n"
44+
// + $"Args:\t{string.Join('|', invocation.Arguments)}\n"
45+
// + $"Return:\t{invocation.ReturnValue}\n"
46+
// + "========================================"
47+
// )));
48+
// builder.RegisterType<A>().As<I>().EnableInterfaceInterceptors();
49+
// builder.RegisterType<B>().As<I>().EnableInterfaceInterceptors();
50+
// });
51+
// host.OnHostStarted += provider =>
52+
// {
53+
// ServiceHost.ParseConfiguration<string>("ConnectionString");
54+
// foreach (var item in provider.GetServices<I>()) item.Do(5, 6);
55+
// };
56+
// await host.RunAsync();
4757
}
4858
}
4959

src/DwFramework.Core/Extensions/DateTimeExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static class DateTimeExtension
1111
/// <param name="endTime"></param>
1212
/// <param name="isMilliseconds"></param>
1313
/// <returns></returns>
14-
public static long GetTimeDiff(this DateTime startTime, DateTime? endTime = null, bool isMilliseconds = false)
14+
public static long GetTimeDiff(this DateTime startTime, DateTime endTime, bool isMilliseconds = false)
1515
{
16-
var diffTime = (endTime ?? DateTime.Now).ToUniversalTime() - startTime.ToUniversalTime();
16+
var diffTime = endTime - startTime;
1717
if (isMilliseconds) return (long)diffTime.TotalMilliseconds;
1818
else return (long)diffTime.TotalSeconds;
1919
}

src/DwFramework.Core/Plugins/Generator/SnowflakeGenerator.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ namespace DwFramework.Core.Generator
88
/// </summary>
99
public sealed class SnowflakeGenerator
1010
{
11-
public sealed class SnowflakeIdInfo
11+
public record SnowflakeIdInfo
1212
{
13-
public long ID { get; }
14-
public DateTime StartTime { get; }
15-
public long Timestamp { get; }
13+
public long ID { get; init; }
14+
public DateTime StartTime { get; init; }
15+
public long Timestamp { get; init; }
1616
public DateTime Time => DateTime.UnixEpoch.AddSeconds(Timestamp);
17-
public long WorkId { get; }
18-
public long Sequence { get; }
17+
public long WorkId { get; init; }
18+
public long Sequence { get; init; }
1919

2020
/// <summary>
2121
/// 构造函数
@@ -71,7 +71,7 @@ public long GenerateId()
7171
{
7272
_currentSequence++;
7373
if (_currentSequence > MaxSequence) Thread.Sleep(1);
74-
var timestamp = StartTime.GetTimeDiff(DateTime.Now);
74+
var timestamp = StartTime.GetTimeDiff(DateTime.UtcNow);
7575
if (timestamp > MaxTimestamp) throw new Exception("时间戳容量不足,请调整StartTime");
7676
if (timestamp < _currentTimestamp) throw new Exception("时间获取异常,请检查服务器时间");
7777
else if (timestamp > _currentTimestamp)

0 commit comments

Comments
 (0)