Skip to content

Commit

Permalink
Merge branch 'hotfix/1.6.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
GregaMohorko committed Feb 8, 2024
2 parents b33b3df + a36417e commit 8063619
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/GM.Utility/GM.Utility/GM.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>GM.StrongNameKey.snk</AssemblyOriginatorKeyFile>
<Version>1.6.0.0</Version>
<Version>1.6.1.0</Version>
<Title>GM.Utility</Title>
<Authors>Gregor Mohorko</Authors>
<Company>Gregor Mohorko</Company>
Expand All @@ -16,9 +16,9 @@
<Description>Library with various static classes and tools that provide universally useful functions, extensions and utilities.</Description>
<Copyright>Copyright © Gregor Mohorko 2024</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReleaseNotes>Added CryptographyUtility.CreateRandomSalt.</PackageReleaseNotes>
<AssemblyVersion>1.6.0.0</AssemblyVersion>
<FileVersion>1.6.0.0</FileVersion>
<PackageReleaseNotes>Added additional logging to ThrottlerPerTime.</PackageReleaseNotes>
<AssemblyVersion>1.6.1.0</AssemblyVersion>
<FileVersion>1.6.1.0</FileVersion>
<RepositoryUrl>https://github.com/GregaMohorko/GM.Utility</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
Expand All @@ -28,7 +28,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>


Expand Down
21 changes: 20 additions & 1 deletion src/GM.Utility/GM.Utility/Throttling/ThrottlerPerTime.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
MIT License
Copyright (c) 2023 Gregor Mohorko
Copyright (c) 2024 Gregor Mohorko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -29,6 +29,7 @@ MIT License
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace GM.Utility.Throttling;
/// <summary>
Expand All @@ -49,17 +50,33 @@ public class ThrottlerPerTime
private volatile int _executionLogPosition;
private readonly DateTime[] _executionLog;

private readonly ILogger _logger;

/// <summary>
/// Creates a new instance of <see cref="ThrottlerPerTime"/>.
/// </summary>
/// <param name="time">The time frame.</param>
/// <param name="maxExecutions">The max number of executions that are allowed in the specified time frame. Zero means no limit.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when max executions is not non-negative.</exception>
public ThrottlerPerTime(TimeSpan time, int maxExecutions)
: this(time, maxExecutions, null)
{ }

/// <summary>
/// Creates a new instance of <see cref="ThrottlerPerTime"/>.
/// </summary>
/// <param name="time">The time frame.</param>
/// <param name="maxExecutions">The max number of executions that are allowed in the specified time frame. Zero means no limit.</param>
/// <param name="logger">Logger.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when max executions is not non-negative.</exception>
public ThrottlerPerTime(TimeSpan time, int maxExecutions, ILogger logger)
{
if(maxExecutions < 0) {
throw new ArgumentOutOfRangeException(nameof(maxExecutions), maxExecutions, "Should be non-negative.");
}

_logger = logger;

Time = time;
MaxExecutions = maxExecutions;

Expand All @@ -86,6 +103,7 @@ public async Task WaitExecutionLimit(CancellationToken ct)
nextAvailableExecutionAt = _executionLog[_executionLogPosition].Add(Time);
if(utcNow >= nextAvailableExecutionAt) {
// can execute now
_logger?.LogDebug("Position: {current}/{total}. Can execute now, since NextAvailableExecutionAt={nextAvailableExecutionAt} <= UtcNow={utcNow}.", _executionLogPosition, _executionLog.Length, nextAvailableExecutionAt, utcNow);

// log new execution
_executionLog[_executionLogPosition] = DateTime.UtcNow;
Expand All @@ -99,6 +117,7 @@ public async Task WaitExecutionLimit(CancellationToken ct)

// wait and try again at next available execution
var sleepTime = nextAvailableExecutionAt - utcNow;
_logger?.LogDebug("Position: {current}/{total}. Cannot execute now, since NextAvailableExecutionAt={nextAvailableExecutionAt} > UtcNow={utcNow}. Sleeping for {sleepTime}.", _executionLogPosition, _executionLog.Length, nextAvailableExecutionAt, utcNow, sleepTime);
await Task.Delay(sleepTime, ct);
}
}
Expand Down

0 comments on commit 8063619

Please sign in to comment.