Skip to content

Commit bdca17f

Browse files
committed
Use hash tags for keys
1 parent 608c9d3 commit bdca17f

File tree

7 files changed

+18
-13
lines changed

7 files changed

+18
-13
lines changed

src/RedisRateLimiting/Concurrency/RedisConcurrencyManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ internal class RedisConcurrencyManager
8686
then
8787
redis.call(""hincrby"", @stats_key, 'total_successful', 1)
8888
else
89-
if queued == false
89+
if queued == false and try_enqueue == 1
9090
then
9191
redis.call(""hincrby"", @stats_key, 'total_failed', 1)
9292
end
@@ -109,9 +109,9 @@ public RedisConcurrencyManager(
109109
_options = options;
110110
_connectionMultiplexer = options.ConnectionMultiplexerFactory!.Invoke();
111111

112-
RateLimitKey = new RedisKey($"rl:{partitionKey}");
113-
QueueRateLimitKey = new RedisKey($"rl:{partitionKey}:q");
114-
StatsRateLimitKey = new RedisKey($"rl:{partitionKey}:stats");
112+
RateLimitKey = new RedisKey($"rl:{{{partitionKey}}}");
113+
QueueRateLimitKey = new RedisKey($"rl:{{{partitionKey}}}:q");
114+
StatsRateLimitKey = new RedisKey($"rl:{{{partitionKey}}}:stats");
115115
}
116116

117117
internal async Task<RedisConcurrencyResponse> TryAcquireLeaseAsync(string requestId, bool tryEnqueue = false)

src/RedisRateLimiting/FixedWindow/RedisFixedWindowManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public RedisFixedWindowManager(
4242
_options = options;
4343
_connectionMultiplexer = options.ConnectionMultiplexerFactory!.Invoke();
4444

45-
RateLimitKey = new RedisKey($"rl:{partitionKey}");
46-
RateLimitExpireKey = new RedisKey($"rl:{partitionKey}:exp");
45+
RateLimitKey = new RedisKey($"rl:{{{partitionKey}}}");
46+
RateLimitExpireKey = new RedisKey($"rl:{{{partitionKey}}}:exp");
4747
}
4848

4949
internal async Task<RedisFixedWindowResponse> TryAcquireLeaseAsync()

src/RedisRateLimiting/RedisRateLimiting.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1515
<RepositoryType>git</RepositoryType>
1616
<RepositoryUrl>https://github.com/cristipufu/aspnetcore-redis-rate-limiting</RepositoryUrl>
17-
<Version>1.0.9</Version>
17+
<Version>1.0.10</Version>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
1919
</PropertyGroup>
2020

src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public RedisSlidingWindowManager(
3737
_options = options;
3838
_connectionMultiplexer = options.ConnectionMultiplexerFactory!.Invoke();
3939

40-
RateLimitKey = new RedisKey($"rl:{partitionKey}");
40+
RateLimitKey = new RedisKey($"rl:{{{partitionKey}}}");
4141
}
4242

4343
internal async Task<RedisSlidingWindowResponse> TryAcquireLeaseAsync(string requestId)

src/RedisRateLimiting/TokenBucket/RedisTokenBucketManager.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal class RedisTokenBucketManager
99
private readonly IConnectionMultiplexer _connectionMultiplexer;
1010
private readonly RedisTokenBucketRateLimiterOptions _options;
1111
private readonly RedisKey RateLimitKey;
12+
private readonly RedisKey RateLimitTimestampKey;
1213

1314
private static readonly LuaScript _redisScript = LuaScript.Prepare(
1415
@"local rate = tonumber(@tokens_per_period)
@@ -22,8 +23,7 @@ internal class RedisTokenBucketManager
2223
current_tokens = limit
2324
end
2425
25-
local timestamp_key = @rate_limit_key .. "":ts""
26-
local last_refreshed = tonumber(redis.call(""get"", timestamp_key))
26+
local last_refreshed = tonumber(redis.call(""get"", @timestamp_key))
2727
if last_refreshed == nil then
2828
last_refreshed = 0
2929
end
@@ -41,7 +41,7 @@ internal class RedisTokenBucketManager
4141
local ttl = math.floor(fill_time * 2)
4242
4343
redis.call(""setex"", @rate_limit_key, ttl, current_tokens)
44-
redis.call(""setex"", timestamp_key, ttl, now)
44+
redis.call(""setex"", @timestamp_key, ttl, now)
4545
4646
return { allowed, current_tokens }");
4747

@@ -52,7 +52,8 @@ public RedisTokenBucketManager(
5252
_options = options;
5353
_connectionMultiplexer = options.ConnectionMultiplexerFactory!.Invoke();
5454

55-
RateLimitKey = new RedisKey($"rl:{partitionKey}");
55+
RateLimitKey = new RedisKey($"rl:{{{partitionKey}}}");
56+
RateLimitTimestampKey = new RedisKey($"rl:{{{partitionKey}}}:ts");
5657
}
5758

5859
internal async Task<RedisTokenBucketResponse> TryAcquireLeaseAsync()
@@ -67,6 +68,7 @@ internal async Task<RedisTokenBucketResponse> TryAcquireLeaseAsync()
6768
new
6869
{
6970
rate_limit_key = RateLimitKey,
71+
timestamp_key = RateLimitTimestampKey,
7072
current_time = nowUnixTimeSeconds,
7173
tokens_per_period = _options.TokensPerPeriod,
7274
token_limit = _options.TokenLimit,
@@ -97,6 +99,7 @@ internal RedisTokenBucketResponse TryAcquireLease()
9799
new
98100
{
99101
rate_limit_key = RateLimitKey,
102+
timestamp_key = RateLimitTimestampKey,
100103
current_time = nowUnixTimeSeconds,
101104
tokens_per_period = _options.TokensPerPeriod,
102105
token_limit = _options.TokenLimit,

test/RedisRateLimiting.Tests/UnitTests/ConcurrencyUnitTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ public async Task QueueAvailableAfterQueueLimitHitAndResourcesBecomeAvailable()
190190
var lease = limiter.AttemptAcquire();
191191
var wait = limiter.AcquireAsync();
192192

193+
await Task.Delay(1000);
194+
193195
var failedLease = await limiter.AcquireAsync();
194196
Assert.False(failedLease.IsAcquired);
195197

test/RedisRateLimiting.Tests/UnitTests/TestFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public TestFixture()
2525

2626
public async Task ClearStatisticsAsync(string partitionKey)
2727
{
28-
var key = $"rl:{partitionKey}:stats";
28+
var key = $"rl:{{{partitionKey}}}:stats";
2929
var database = ConnectionMultiplexer.GetDatabase();
3030
await database.KeyDeleteAsync(key);
3131
}

0 commit comments

Comments
 (0)