From 3d8cb5ea7a98da5cfc2db4fa6d6615be9cc399ca Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Hussien Date: Sun, 20 Oct 2024 20:23:25 +0300 Subject: [PATCH 1/2] Add IDisposable interface to the SnowflakeIdService --- LICENSE.txt | 7 ++++ .../SnowflakeIdServiceExtensions.cs | 10 +---- src/core/ISnowflakeService.cs | 9 +---- src/core/SnowflakOptions.cs | 8 +--- src/core/SnowflakeId.cs | 8 +--- src/core/SnowflakeIdConfig.cs | 9 +---- src/core/SnowflakeIdService.cs | 39 ++++++++++++------- 7 files changed, 39 insertions(+), 51 deletions(-) create mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..1bbb90b --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,7 @@ +/* + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + */ \ No newline at end of file diff --git a/src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs b/src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs index 087fef0..d91db16 100644 --- a/src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs +++ b/src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs @@ -1,17 +1,9 @@ -/* -GNU GENERAL PUBLIC LICENSE -Version 3, 29 June 2007 -Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. -Everyone is permitted to copy and distribute verbatim copies -of this license document, but changing it is not allowed. -*/ +// Read more about the licenses under the root of the project in the LICENSE.txt file./ using System; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection; using SnowflakeId.Core; -using Microsoft.Extensions.Logging.Abstractions; - namespace SnowflakeId.DependencyInjection { diff --git a/src/core/ISnowflakeService.cs b/src/core/ISnowflakeService.cs index 0844188..117b7fb 100644 --- a/src/core/ISnowflakeService.cs +++ b/src/core/ISnowflakeService.cs @@ -1,11 +1,4 @@ -/* - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - */ - +// Read more about the licenses under the root of the project in the LICENSE.txt file. using System; using System.Threading.Tasks; diff --git a/src/core/SnowflakOptions.cs b/src/core/SnowflakOptions.cs index 8ac4bac..1931ad3 100644 --- a/src/core/SnowflakOptions.cs +++ b/src/core/SnowflakOptions.cs @@ -1,10 +1,4 @@ -/* - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - */ +// Read more about the licenses under the root of the project in the LICENSE.txt file. namespace SnowflakeId.Core { diff --git a/src/core/SnowflakeId.cs b/src/core/SnowflakeId.cs index b7ee71f..d101df8 100644 --- a/src/core/SnowflakeId.cs +++ b/src/core/SnowflakeId.cs @@ -1,10 +1,4 @@ -/* - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - */ +// Read more about the licenses under the root of the project in the LICENSE.txt file. using System; diff --git a/src/core/SnowflakeIdConfig.cs b/src/core/SnowflakeIdConfig.cs index b6fa36a..4796938 100644 --- a/src/core/SnowflakeIdConfig.cs +++ b/src/core/SnowflakeIdConfig.cs @@ -1,12 +1,7 @@ -/* - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - */ +// Read more about the licenses under the root of the project in the LICENSE.txt file. using System; + namespace SnowflakeId.Core { internal class SnowflakeIdConfig diff --git a/src/core/SnowflakeIdService.cs b/src/core/SnowflakeIdService.cs index 50e0aba..643acfd 100644 --- a/src/core/SnowflakeIdService.cs +++ b/src/core/SnowflakeIdService.cs @@ -1,11 +1,4 @@ -/* - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - */ - +// Read more about the licenses under the root of the project in the LICENSE.txt file. using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -16,7 +9,7 @@ Everyone is permitted to copy and distribute verbatim copies namespace SnowflakeId.Core { - public class SnowflakeIdService : ISnowflakeService + public class SnowflakeIdService : ISnowflakeService, IDisposable { // Lock Token private readonly object threadLock = new object(); @@ -37,7 +30,8 @@ public class SnowflakeIdService : ISnowflakeService /// When generating the Id I use the Epoch that start at 1970 Jan 1s ( Unix Time ) /// public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); - private readonly static SemaphoreSlim _sem = new SemaphoreSlim(1); + + private bool _disposed; public SnowflakeIdService(IOptions options, ILogger logger) { _snowflakOptions = options.Value; @@ -92,14 +86,15 @@ public virtual long GenerateSnowflakeId() /// public virtual Task GenerateSnowflakeIdAsync(CancellationToken cancellationToken = default) { + SemaphoreSlim sem = new SemaphoreSlim(1, 1); try { - _sem.Wait(cancellationToken); + sem.Wait(cancellationToken); long currentTimestamp = getTimestamp(); if (currentTimestamp < _lastTimestamp) { - _logger.LogError("error in the server clock, thecurrent timestamp should be bigger than generated one, current timestamp is: {0}, and the last generated timestamp is: {1}", currentTimestamp, _lastTimestamp); + _logger.LogError("error in the server clock, the current timestamp should be bigger than generated one, current timestamp is: {0}, and the last generated timestamp is: {1}", currentTimestamp, _lastTimestamp); throw new InvalidOperationException("Error_In_The_Server_Clock"); } @@ -127,7 +122,7 @@ public virtual Task GenerateSnowflakeIdAsync(CancellationToken cancellatio } finally { - _sem.Release(); + sem.Release(); } @@ -234,5 +229,23 @@ private long waitToGetNextMillis(long currentTimestamp) } return currentTimestamp; } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!_disposed) + { + if (disposing) + { + // no-op. + } + _disposed = true; + } + } } } From b62aee4ab842c6d6649d99c540e5ccf5d00e7f2a Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Hussien Date: Fri, 15 Nov 2024 20:22:04 +0300 Subject: [PATCH 2/2] Update the library to target .NET 9.0 --- .../SnowflakeId.Example.csproj | 6 ++--- src/core/ISnowflakeService.cs | 2 +- src/core/SnowflakeId.Core.csproj | 23 ++++++++++++------- src/core/SnowflakeIdService.cs | 2 +- tests/SnowflakeId.Tests.csproj | 15 +++++++++--- tests/SnowflakeIdServiceTest.cs | 2 +- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/example/SnowflakeId.Example/SnowflakeId.Example.csproj b/example/SnowflakeId.Example/SnowflakeId.Example.csproj index 8153659..5db1e24 100644 --- a/example/SnowflakeId.Example/SnowflakeId.Example.csproj +++ b/example/SnowflakeId.Example/SnowflakeId.Example.csproj @@ -2,14 +2,14 @@ Exe - net6.0 + net9.0 disable disable - - + + diff --git a/src/core/ISnowflakeService.cs b/src/core/ISnowflakeService.cs index 117b7fb..ebb4915 100644 --- a/src/core/ISnowflakeService.cs +++ b/src/core/ISnowflakeService.cs @@ -24,7 +24,7 @@ public interface ISnowflakeService /// cancellationToken /// A new unique number that has a long type. /// - Task GenerateSnowflakeIdAsync(CancellationToken cancellationToken =default); + Task GenerateSnowflakeIdAsync(CancellationToken cancellationToken = default); /// /// A method caculated the generate date time for a given generated snowflake id. diff --git a/src/core/SnowflakeId.Core.csproj b/src/core/SnowflakeId.Core.csproj index 1748232..4ad8cda 100644 --- a/src/core/SnowflakeId.Core.csproj +++ b/src/core/SnowflakeId.Core.csproj @@ -1,7 +1,6 @@  - - net6.0;net7.0;net8.0 + net6.0;net7.0;net8.0;net9.0 disable disable Hussien.SnowflakeId @@ -11,16 +10,24 @@ https://github.com/Shoogn/SnowflakeId 3.0.0 git - SnowflakeId, twitterSnowflakeId, twitterSnowflake + SnowflakeId, UniqueSnowflakeId, UniqueId en-SD - This package implements the twitter's snowflakeId algorithm, - the source code is written in C# progeamming language, - and the main benefits of this library is to help anyone - wroks with Distrbuted Systems and needs a unique Ids for these Systems. + This package implements the twitter's snowflakeId algorithm, + the source code is written in C# programming language, + and the main benefits of this library is to help anyone + wroks with Distrbuted Systems to generate a unique Ids for these Systems or + for Primary Keys is the RDMS such as SQL Server, Oracle, MySQL; or for any other situation that reuired unique Ids. + + + + + + + @@ -46,4 +53,4 @@ - + \ No newline at end of file diff --git a/src/core/SnowflakeIdService.cs b/src/core/SnowflakeIdService.cs index 643acfd..9ea4b66 100644 --- a/src/core/SnowflakeIdService.cs +++ b/src/core/SnowflakeIdService.cs @@ -50,7 +50,7 @@ public virtual long GenerateSnowflakeId() if (currentTimestamp < _lastTimestamp) { - _logger.LogError("error in the server clock, thecurrent timestamp should be bigger than generated one, current timestamp is: {0}, and the last generated timestamp is: {1}", currentTimestamp, _lastTimestamp); + _logger.LogError("error in the server clock, the current timestamp should be bigger than generated one, current timestamp is: {0}, and the last generated timestamp is: {1}", currentTimestamp, _lastTimestamp); throw new InvalidOperationException("Error_In_The_Server_Clock"); } diff --git a/tests/SnowflakeId.Tests.csproj b/tests/SnowflakeId.Tests.csproj index bdafb9e..352fa19 100644 --- a/tests/SnowflakeId.Tests.csproj +++ b/tests/SnowflakeId.Tests.csproj @@ -1,7 +1,7 @@  - net6.0;net7.0;net8.0 + net6.0;net7.0;net8.0;net9.0 enable false @@ -10,12 +10,21 @@ - + - + + + + + + + + + + diff --git a/tests/SnowflakeIdServiceTest.cs b/tests/SnowflakeIdServiceTest.cs index bb51a70..34b0ef7 100644 --- a/tests/SnowflakeIdServiceTest.cs +++ b/tests/SnowflakeIdServiceTest.cs @@ -25,7 +25,7 @@ public void Can_Genertate_UniqueId() } [Fact] - public async void Can_Genertate_UniqueId_Asynchrony() + public async Task Can_Genertate_UniqueId_Asynchrony() { var services = new ServiceCollection(); services.AddLogging();