Skip to content

Commit 75684df

Browse files
committed
default values
1 parent 752d861 commit 75684df

File tree

8 files changed

+57
-83
lines changed

8 files changed

+57
-83
lines changed

ManagedCode.Orleans.RateLimiting.Core/Attributes/ConcurrencyLimiterAttribute.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class ConcurrencyLimiterAttribute : Attribute, ILimiterAttribute<Concurre
2424
/// Must be set to a value >= 0 by the time these options are passed to the constructor of <see cref="ConcurrencyLimiter"/>.
2525
/// </param>
2626
/// <param name="queueProcessingOrder">Determines the behaviour of <see cref="RateLimiter.AcquireAsync"/> when not enough resources can be leased.</param>
27-
public ConcurrencyLimiterAttribute(KeyType keyType = KeyType.GrainId, string? key = null,
28-
int? permitLimit = null,
29-
int? queueLimit = null,
27+
public ConcurrencyLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
28+
int permitLimit = default,
29+
int queueLimit = default,
3030
QueueProcessingOrder queueProcessingOrder = QueueProcessingOrder.OldestFirst)
3131
{
3232
Key = key;
@@ -38,12 +38,15 @@ public ConcurrencyLimiterAttribute(KeyType keyType = KeyType.GrainId, string? ke
3838
KeyType = KeyType.Key;
3939
}
4040

41-
if (permitLimit.HasValue || queueLimit.HasValue)
41+
int? permitLimitNullable = permitLimit > 0 ? permitLimit : null;
42+
int? queueLimitNullable = queueLimit >= 0 ? queueLimit : null;
43+
44+
if (permitLimitNullable.HasValue || queueLimitNullable.HasValue)
4245
{
4346
Options = new ConcurrencyLimiterOptions()
4447
{
45-
PermitLimit = permitLimit ?? 1,
46-
QueueLimit = queueLimit ?? 1,
48+
PermitLimit = permitLimitNullable ?? 1,
49+
QueueLimit = queueLimitNullable ?? 1,
4750
QueueProcessingOrder = queueProcessingOrder
4851
};
4952
}

ManagedCode.Orleans.RateLimiting.Core/Attributes/FixedWindowRateLimiterAttribute.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public class FixedWindowRateLimiterAttribute : Attribute, ILimiterAttribute<Fixe
3232
/// Must be set to a value > 0 by the time these options are passed to the constructor of <see cref="FixedWindowRateLimiter"/>.
3333
/// </param>
3434
/// <param name="queueProcessingOrder">Determines the behaviour of <see cref="RateLimiter.AcquireAsync"/> when not enough resources can be leased.</param>
35-
public FixedWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string? key = null,
36-
TimeSpan? window = null,
37-
int? permitLimit = null,
38-
int? queueLimit = null,
35+
public FixedWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
36+
TimeSpan window = default,
37+
int permitLimit = default,
38+
int queueLimit = default,
3939
bool autoReplenishment = true,
4040
QueueProcessingOrder queueProcessingOrder = QueueProcessingOrder.OldestFirst)
4141
{
@@ -48,13 +48,17 @@ public FixedWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string
4848
KeyType = KeyType.Key;
4949
}
5050

51-
if (permitLimit.HasValue || queueLimit.HasValue || window.HasValue)
51+
int? permitLimitNullable = permitLimit > 0 ? permitLimit : null;
52+
int? queueLimitNullable = queueLimit >= 0 ? queueLimit : null;
53+
TimeSpan? windowNullable = window > TimeSpan.Zero ? window : null;
54+
55+
if (permitLimitNullable.HasValue || queueLimitNullable.HasValue || windowNullable.HasValue)
5256
{
5357
Options = new FixedWindowRateLimiterOptions()
5458
{
55-
Window = window ?? TimeSpan.FromSeconds(1),
56-
PermitLimit = permitLimit ?? 1,
57-
QueueLimit = queueLimit ?? 1,
59+
Window = windowNullable ?? TimeSpan.FromSeconds(1),
60+
PermitLimit = permitLimitNullable ?? 1,
61+
QueueLimit = queueLimitNullable ?? 1,
5862
AutoReplenishment = autoReplenishment,
5963
QueueProcessingOrder = queueProcessingOrder
6064
};

ManagedCode.Orleans.RateLimiting.Core/Attributes/SlidingWindowRateLimiterAttribute.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public class SlidingWindowRateLimiterAttribute : Attribute, ILimiterAttribute<Sl
3636
/// Must be set to a value > 0 by the time these options are passed to the constructor of <see cref="SlidingWindowRateLimiter"/>.
3737
/// </param>
3838
/// <param name="queueProcessingOrder">Determines the behaviour of <see cref="RateLimiter.AcquireAsync"/> when not enough resources can be leased.</param>
39-
public SlidingWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string? key = null,
40-
TimeSpan? window = null,
41-
int? permitLimit = null,
42-
int? queueLimit = null,
43-
int? segmentsPerWindow = null,
39+
public SlidingWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
40+
TimeSpan window = default,
41+
int permitLimit = default,
42+
int queueLimit = default,
43+
int segmentsPerWindow = default,
4444
bool autoReplenishment = true,
4545
QueueProcessingOrder queueProcessingOrder = QueueProcessingOrder.OldestFirst)
4646
{
@@ -53,14 +53,20 @@ public SlidingWindowRateLimiterAttribute(KeyType keyType = KeyType.GrainId, stri
5353
KeyType = KeyType.Key;
5454
}
5555

56-
if (permitLimit.HasValue || queueLimit.HasValue || window.HasValue || segmentsPerWindow.HasValue)
56+
int? permitLimitNullable = permitLimit > 0 ? permitLimit : null;
57+
int? queueLimitNullable = queueLimit >= 0 ? queueLimit : null;
58+
int? segmentsPerWindowNullable = segmentsPerWindow >= 0 ? segmentsPerWindow : null;
59+
TimeSpan? windowNullable = window > TimeSpan.Zero ? window : null;
60+
61+
62+
if (permitLimitNullable.HasValue || queueLimitNullable.HasValue || windowNullable.HasValue || segmentsPerWindowNullable.HasValue)
5763
{
5864
Options = new SlidingWindowRateLimiterOptions()
5965
{
60-
Window = window ?? TimeSpan.FromSeconds(1),
61-
PermitLimit = permitLimit ?? 1,
62-
QueueLimit = queueLimit ?? 1,
63-
SegmentsPerWindow = segmentsPerWindow ?? 1,
66+
Window = windowNullable ?? TimeSpan.FromSeconds(1),
67+
PermitLimit = permitLimitNullable ?? 1,
68+
QueueLimit = queueLimitNullable ?? 1,
69+
SegmentsPerWindow = segmentsPerWindowNullable ?? 1,
6470
AutoReplenishment = autoReplenishment,
6571
QueueProcessingOrder = queueProcessingOrder
6672
};

ManagedCode.Orleans.RateLimiting.Core/Attributes/TokenBucketRateLimiterAttribute.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public class TokenBucketRateLimiterAttribute : Attribute, ILimiterAttribute<Toke
3636
/// Must be set to a value > 0 by the time these options are passed to the constructor of <see cref="TokenBucketRateLimiter"/>.
3737
/// </param>
3838
/// <param name="queueProcessingOrder">Determines the behaviour of <see cref="RateLimiter.AcquireAsync"/> when not enough resources can be leased.</param>
39-
public TokenBucketRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string? key = null,
40-
TimeSpan? replenishmentPeriod = null,
41-
int? tokensPerPeriod = null,
42-
int? queueLimit = null,
43-
int? tokenLimit = null,
39+
public TokenBucketRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string key = default,
40+
TimeSpan replenishmentPeriod = default,
41+
int tokensPerPeriod = default,
42+
int queueLimit = default,
43+
int tokenLimit = default,
4444
bool autoReplenishment = true,
4545
QueueProcessingOrder queueProcessingOrder = QueueProcessingOrder.OldestFirst)
4646
{
@@ -53,14 +53,19 @@ public TokenBucketRateLimiterAttribute(KeyType keyType = KeyType.GrainId, string
5353
KeyType = KeyType.Key;
5454
}
5555

56-
if (tokensPerPeriod.HasValue || queueLimit.HasValue || replenishmentPeriod.HasValue || tokenLimit.HasValue)
56+
int? tokensPerPeriodNullable = tokensPerPeriod > 0 ? tokensPerPeriod : null;
57+
int? queueLimitNullable = queueLimit >= 0 ? queueLimit : null;
58+
int? tokenLimitNullable = tokenLimit >= 0 ? tokenLimit : null;
59+
TimeSpan? replenishmentPeriodNullable = replenishmentPeriod > TimeSpan.Zero ? replenishmentPeriod : null;
60+
61+
if (tokensPerPeriodNullable.HasValue || queueLimitNullable.HasValue || tokenLimitNullable.HasValue || replenishmentPeriodNullable.HasValue)
5762
{
5863
Options = new TokenBucketRateLimiterOptions()
5964
{
60-
ReplenishmentPeriod = replenishmentPeriod ?? TimeSpan.FromSeconds(1),
61-
TokensPerPeriod = tokensPerPeriod ?? 1,
62-
QueueLimit = queueLimit ?? 1,
63-
TokenLimit = tokenLimit ?? 1,
65+
ReplenishmentPeriod = replenishmentPeriodNullable ?? TimeSpan.FromSeconds(1),
66+
TokensPerPeriod = tokensPerPeriodNullable ?? 1,
67+
QueueLimit = queueLimitNullable ?? 1,
68+
TokenLimit = tokenLimitNullable ?? 1,
6469
AutoReplenishment = autoReplenishment,
6570
QueueProcessingOrder = queueProcessingOrder
6671
};

ManagedCode.Orleans.RateLimiting.Tests/Cluster/Grains/FirstLimitingGrain.cs renamed to ManagedCode.Orleans.RateLimiting.Tests/Cluster/Grains/ConcurrencyLimiterGrain.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains
66
{
7-
public class FirstLimitingGrain : Grain, IFirstLimitingGrain
7+
public class ConcurrencyLimiterGrain : Grain, IConcurrencyLimiterGrain
88
{
9-
[ConcurrencyLimiter] //GrainId, default options
9+
[ConcurrencyLimiter] //GrainId, default options PermitLimit = 10; QueueLimit = 15;
1010
public Task<string> Do()
1111
{
12-
1312
return Task.FromResult("Do");
1413
}
1514

16-
[ConcurrencyLimiter(KeyType.Key, "go")] //GrainId, default options
15+
[ConcurrencyLimiter(KeyType.Key, "go")] //GrainId, default options PermitLimit = 10; QueueLimit = 15;
1716
public Task<string> Go()
1817
{
1918
return Task.FromResult("Go");
2019
}
2120

21+
[ConcurrencyLimiter(KeyType.GrainType, permitLimit:5, queueLimit:5)]
2222
public Task<string> Take()
2323
{
2424
return Task.FromResult("Take");

ManagedCode.Orleans.RateLimiting.Tests/Cluster/Grains/Interfaces/IFirstLimitingGrain.cs renamed to ManagedCode.Orleans.RateLimiting.Tests/Cluster/Grains/Interfaces/IConcurrencyLimiterGrain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace ManagedCode.Orleans.RateLimiting.Tests.Cluster.Grains.Interfaces
22
{
3-
public interface IFirstLimitingGrain : IGrainWithStringKey
3+
public interface IConcurrencyLimiterGrain : IGrainWithStringKey
44
{
55
Task<string> Do();
66
Task<string> Go();

ManagedCode.Orleans.RateLimiting.Tests/Cluster/Grains/SecondLimitingGrain.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

ManagedCode.Orleans.RateLimiting.Tests/Cluster/Grains/ThirdLimitingGrain.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)