Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #2

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
*/
6 changes: 3 additions & 3 deletions example/SnowflakeId.Example/SnowflakeId.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 1 addition & 9 deletions src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
/*
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. <https://fsf.org/>
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
{
Expand Down
11 changes: 2 additions & 9 deletions src/core/ISnowflakeService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
/*
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. <https://fsf.org/>
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;
Expand All @@ -31,7 +24,7 @@ public interface ISnowflakeService
/// <param name="cancellationToken">cancellationToken</param>
/// <returns>A new unique number that has a long type.</returns>
/// <exception cref="InvalidOperationException"></exception>
Task<long> GenerateSnowflakeIdAsync(CancellationToken cancellationToken =default);
Task<long> GenerateSnowflakeIdAsync(CancellationToken cancellationToken = default);

/// <summary>
/// A method caculated the generate date time for a given generated snowflake id.
Expand Down
8 changes: 1 addition & 7 deletions src/core/SnowflakOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/*
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. <https://fsf.org/>
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
{
Expand Down
23 changes: 15 additions & 8 deletions src/core/SnowflakeId.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<PackageId>Hussien.SnowflakeId</PackageId>
Expand All @@ -11,16 +10,24 @@
<RepositoryUrl>https://github.com/Shoogn/SnowflakeId</RepositoryUrl>
<Version>3.0.0</Version>
<RepositoryType>git</RepositoryType>
<PackageTags>SnowflakeId, twitterSnowflakeId, twitterSnowflake</PackageTags>
<PackageTags>SnowflakeId, UniqueSnowflakeId, UniqueId</PackageTags>
<NeutralLanguage>en-SD</NeutralLanguage>
<Description>
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.
</Description>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
Expand All @@ -46,4 +53,4 @@
<InternalsVisibleTo Include="SnowflakeId.Tests"/>
</ItemGroup>

</Project>
</Project>
8 changes: 1 addition & 7 deletions src/core/SnowflakeId.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/*
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. <https://fsf.org/>
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;

Expand Down
9 changes: 2 additions & 7 deletions src/core/SnowflakeIdConfig.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/*
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. <https://fsf.org/>
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
Expand Down
41 changes: 27 additions & 14 deletions src/core/SnowflakeIdService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
/*
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2022 Mohammed Ahmed Hussien babiker Free Software Foundation, Inc. <https://fsf.org/>
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;
Expand All @@ -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();
Expand All @@ -37,7 +30,8 @@ public class SnowflakeIdService : ISnowflakeService
/// When generating the Id <see cref="SnowflakeIdService"/> I use the Epoch that start at 1970 Jan 1s ( Unix Time )
/// </summary>
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<SnowflakOptions> options, ILogger<SnowflakeIdService> logger)
{
_snowflakOptions = options.Value;
Expand All @@ -56,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");
}

Expand Down Expand Up @@ -92,14 +86,15 @@ public virtual long GenerateSnowflakeId()
/// <exception cref="InvalidOperationException"></exception>
public virtual Task<long> 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");
}

Expand Down Expand Up @@ -127,7 +122,7 @@ public virtual Task<long> GenerateSnowflakeIdAsync(CancellationToken cancellatio
}
finally
{
_sem.Release();
sem.Release();
}


Expand Down Expand Up @@ -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;
}
}
}
}
15 changes: 12 additions & 3 deletions tests/SnowflakeId.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>

<IsPackable>false</IsPackable>
Expand All @@ -10,12 +10,21 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion tests/SnowflakeIdServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading