Skip to content

Commit

Permalink
GrainsRateLimiterTests
Browse files Browse the repository at this point in the history
  • Loading branch information
KSemenenko committed Apr 17, 2023
1 parent 75684df commit 8013708
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ConcurrencyLimiterAttribute : Attribute, ILimiterAttribute<Concurre
/// <param name="queueProcessingOrder">Determines the behaviour of <see cref="RateLimiter.AcquireAsync"/> when not enough resources can be leased.</param>
public ConcurrencyLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
int permitLimit = default,
int queueLimit = default,
int queueLimit = 0,
QueueProcessingOrder queueProcessingOrder = QueueProcessingOrder.OldestFirst)
{
Key = key;
Expand All @@ -39,14 +39,13 @@ public ConcurrencyLimiterAttribute(KeyType keyType = KeyType.GrainId, string key
}

int? permitLimitNullable = permitLimit > 0 ? permitLimit : null;
int? queueLimitNullable = queueLimit >= 0 ? queueLimit : null;

if (permitLimitNullable.HasValue || queueLimitNullable.HasValue)
if (permitLimitNullable.HasValue)
{
Options = new ConcurrencyLimiterOptions()
{
PermitLimit = permitLimitNullable ?? 1,
QueueLimit = queueLimitNullable ?? 1,
QueueLimit = queueLimit,
QueueProcessingOrder = queueProcessingOrder
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class FixedWindowRateLimiterAttribute : Attribute, ILimiterAttribute<Fixe
public FixedWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
TimeSpan window = default,
int permitLimit = default,
int queueLimit = default,
int queueLimit = 0,
bool autoReplenishment = true,
QueueProcessingOrder queueProcessingOrder = QueueProcessingOrder.OldestFirst)
{
Expand All @@ -49,16 +49,15 @@ public FixedWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string
}

int? permitLimitNullable = permitLimit > 0 ? permitLimit : null;
int? queueLimitNullable = queueLimit >= 0 ? queueLimit : null;
TimeSpan? windowNullable = window > TimeSpan.Zero ? window : null;

if (permitLimitNullable.HasValue || queueLimitNullable.HasValue || windowNullable.HasValue)
if (permitLimitNullable.HasValue || windowNullable.HasValue)
{
Options = new FixedWindowRateLimiterOptions()
{
Window = windowNullable ?? TimeSpan.FromSeconds(1),
PermitLimit = permitLimitNullable ?? 1,
QueueLimit = queueLimitNullable ?? 1,
QueueLimit = queueLimit,
AutoReplenishment = autoReplenishment,
QueueProcessingOrder = queueProcessingOrder
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class SlidingWindowRateLimiterAttribute : Attribute, ILimiterAttribute<Sl
public SlidingWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
TimeSpan window = default,
int permitLimit = default,
int queueLimit = default,
int queueLimit = 0,
int segmentsPerWindow = default,
bool autoReplenishment = true,
QueueProcessingOrder queueProcessingOrder = QueueProcessingOrder.OldestFirst)
Expand All @@ -54,18 +54,17 @@ public SlidingWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, stri
}

int? permitLimitNullable = permitLimit > 0 ? permitLimit : null;
int? queueLimitNullable = queueLimit >= 0 ? queueLimit : null;
int? segmentsPerWindowNullable = segmentsPerWindow >= 0 ? segmentsPerWindow : null;
TimeSpan? windowNullable = window > TimeSpan.Zero ? window : null;


if (permitLimitNullable.HasValue || queueLimitNullable.HasValue || windowNullable.HasValue || segmentsPerWindowNullable.HasValue)
if (permitLimitNullable.HasValue || windowNullable.HasValue || segmentsPerWindowNullable.HasValue)
{
Options = new SlidingWindowRateLimiterOptions()
{
Window = windowNullable ?? TimeSpan.FromSeconds(1),
PermitLimit = permitLimitNullable ?? 1,
QueueLimit = queueLimitNullable ?? 1,
QueueLimit = queueLimit,
SegmentsPerWindow = segmentsPerWindowNullable ?? 1,
AutoReplenishment = autoReplenishment,
QueueProcessingOrder = queueProcessingOrder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TokenBucketRateLimiterAttribute : Attribute, ILimiterAttribute<Toke
public TokenBucketRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
TimeSpan replenishmentPeriod = default,
int tokensPerPeriod = default,
int queueLimit = default,
int queueLimit = 0,
int tokenLimit = default,
bool autoReplenishment = true,
QueueProcessingOrder queueProcessingOrder = QueueProcessingOrder.OldestFirst)
Expand All @@ -54,17 +54,16 @@ public TokenBucketRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string
}

int? tokensPerPeriodNullable = tokensPerPeriod > 0 ? tokensPerPeriod : null;
int? queueLimitNullable = queueLimit >= 0 ? queueLimit : null;
int? tokenLimitNullable = tokenLimit >= 0 ? tokenLimit : null;
TimeSpan? replenishmentPeriodNullable = replenishmentPeriod > TimeSpan.Zero ? replenishmentPeriod : null;

if (tokensPerPeriodNullable.HasValue || queueLimitNullable.HasValue || tokenLimitNullable.HasValue || replenishmentPeriodNullable.HasValue)
if (tokensPerPeriodNullable.HasValue || tokenLimitNullable.HasValue || replenishmentPeriodNullable.HasValue)
{
Options = new TokenBucketRateLimiterOptions()
{
ReplenishmentPeriod = replenishmentPeriodNullable ?? TimeSpan.FromSeconds(1),
TokensPerPeriod = tokensPerPeriodNullable ?? 1,
QueueLimit = queueLimitNullable ?? 1,
QueueLimit = queueLimit,
TokenLimit = tokenLimitNullable ?? 1,
AutoReplenishment = autoReplenishment,
QueueProcessingOrder = queueProcessingOrder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using ManagedCode.Orleans.RateLimiting.Core.Interfaces;
Expand All @@ -8,6 +9,7 @@

namespace ManagedCode.Orleans.RateLimiting.Core.Models;

[DebuggerDisplay("IsAcquired = {IsAcquired}")]
public class OrleansRateLimitLease : IDisposable, IAsyncDisposable
{
private readonly GrainId _grainId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@
using System.Threading.RateLimiting;
using ManagedCode.Orleans.RateLimiting.Server.GrainCallFilter;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Orleans.Hosting;

namespace ManagedCode.Orleans.RateLimiting.Server.Extensions;

public static class SiloBuilderConcurrencyLimiterExtensions
{
public static ISiloBuilder AddOrleansConcurrencyLimiter(this ISiloBuilder siloBuilder,
ConcurrencyLimiterOptions defaultOptions)
Action<ConcurrencyLimiterOptions> defaultOptions)
{
siloBuilder.Services.AddSingleton(defaultOptions);
siloBuilder.Services.AddOptions<ConcurrencyLimiterOptions>().Configure(defaultOptions.Invoke);
siloBuilder.AddIncomingGrainCallFilter<ConcurrencyLimiterIncomingFilter>();
return siloBuilder;
}

public static ISiloBuilder AddOrleansConcurrencyLimiter(this ISiloBuilder siloBuilder,
Action<ConcurrencyLimiterOptions> defaultOptions)
{
var fixedWindowRateLimiterOptions = new ConcurrencyLimiterOptions();
defaultOptions.Invoke(fixedWindowRateLimiterOptions);
return siloBuilder.AddOrleansConcurrencyLimiter(fixedWindowRateLimiterOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ namespace ManagedCode.Orleans.RateLimiting.Server.Extensions;
public static class SiloBuilderFixedWindowRateLimiterExtensions
{
public static ISiloBuilder AddOrleansFixedWindowRateLimiter(this ISiloBuilder siloBuilder,
FixedWindowRateLimiterOptions defaultOptions)
Action<FixedWindowRateLimiterOptions> defaultOptions)
{
siloBuilder.Services.AddSingleton(defaultOptions);
siloBuilder.Services.AddOptions<FixedWindowRateLimiterOptions>().Configure(defaultOptions.Invoke);
siloBuilder.AddIncomingGrainCallFilter<FixedWindowRateLimiterIncomingFilter>();
return siloBuilder;
}

public static ISiloBuilder AddOrleansFixedWindowRateLimiter(this ISiloBuilder siloBuilder,
Action<FixedWindowRateLimiterOptions> defaultOptions)
{
var fixedWindowRateLimiterOptions = new FixedWindowRateLimiterOptions();
defaultOptions.Invoke(fixedWindowRateLimiterOptions);
return siloBuilder.AddOrleansFixedWindowRateLimiter(fixedWindowRateLimiterOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ namespace ManagedCode.Orleans.RateLimiting.Server.Extensions;
public static class SiloBuilderSlidingWindowRateLimiterExtensions
{
public static ISiloBuilder AddOrleansSlidingWindowRateLimiter(this ISiloBuilder siloBuilder,
SlidingWindowRateLimiterOptions defaultOptions)
Action<SlidingWindowRateLimiterOptions> defaultOptions)
{
siloBuilder.Services.AddSingleton(defaultOptions);
siloBuilder.Services.AddOptions<SlidingWindowRateLimiterOptions>().Configure(defaultOptions.Invoke);
siloBuilder.AddIncomingGrainCallFilter<SlidingWindowRateLimiterIncomingFilter>();
return siloBuilder;
}

public static ISiloBuilder AddOrleansSlidingWindowRateLimiter(this ISiloBuilder siloBuilder,
Action<SlidingWindowRateLimiterOptions> defaultOptions)
{
var fixedWindowRateLimiterOptions = new SlidingWindowRateLimiterOptions();
defaultOptions.Invoke(fixedWindowRateLimiterOptions);
return siloBuilder.AddOrleansSlidingWindowRateLimiter(fixedWindowRateLimiterOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ namespace ManagedCode.Orleans.RateLimiting.Server.Extensions;
public static class SiloBuilderTokenBucketRateLimiterExtensions
{
public static ISiloBuilder AddOrleansTokenBucketRateLimiter(this ISiloBuilder siloBuilder,
TokenBucketRateLimiterOptions defaultOptions)
Action<TokenBucketRateLimiterOptions> defaultOptions)
{
siloBuilder.Services.AddSingleton(defaultOptions);
siloBuilder.Services.AddOptions<TokenBucketRateLimiterOptions>().Configure(defaultOptions.Invoke);
siloBuilder.AddIncomingGrainCallFilter<TokenBucketRateLimiterIncomingFilter>();
return siloBuilder;
}

public static ISiloBuilder AddOrleansTokenBucketRateLimiter(this ISiloBuilder siloBuilder,
Action<TokenBucketRateLimiterOptions> defaultOptions)
{
var fixedWindowRateLimiterOptions = new TokenBucketRateLimiterOptions();
defaultOptions.Invoke(fixedWindowRateLimiterOptions);
return siloBuilder.AddOrleansTokenBucketRateLimiter(fixedWindowRateLimiterOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@

namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains
{
public class ConcurrencyLimiterGrain : Grain, IConcurrencyLimiterGrain
public class TestConcurrencyLimiterGrain : Grain, ITestConcurrencyLimiterGrain
{
[ConcurrencyLimiter] //GrainId, default options PermitLimit = 10; QueueLimit = 15;
public Task<string> Do()
public async Task<string> Do()
{
return Task.FromResult("Do");
await Task.Delay(TimeSpan.FromSeconds(5));
return "Do";
}

[ConcurrencyLimiter(KeyType.Key, "go")] //GrainId, default options PermitLimit = 10; QueueLimit = 15;
public Task<string> Go()
[ConcurrencyLimiter(KeyType.Key, "go")] //Key, default options PermitLimit = 10; QueueLimit = 15;
public async Task<string> Go()
{
return Task.FromResult("Go");
await Task.Delay(TimeSpan.FromSeconds(5));
return "Go";
}

[ConcurrencyLimiter(KeyType.GrainType, permitLimit:5, queueLimit:5)]
public Task<string> Take()
[ConcurrencyLimiter(KeyType.GrainType, permitLimit:2, queueLimit:1)]
public async Task<string> Take()
{
return Task.FromResult("Take");
await Task.Delay(TimeSpan.FromSeconds(5));
return "Take";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains.Interfaces
{
public interface IConcurrencyLimiterGrain : IGrainWithStringKey
public interface ITestConcurrencyLimiterGrain : IGrainWithStringKey
{
Task<string> Do();
Task<string> Go();
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8013708

Please sign in to comment.