Skip to content

Commit f2e06fb

Browse files
authored
Merge pull request #4 from PandaTechAM/development
Assembly name fix in rate limit
2 parents 0751ed3 + 1ee3c90 commit f2e06fb

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/DistributedCache/DistributedCache.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.2.1</Version>
11+
<Version>1.2.2</Version>
1212
<PackageId>Pandatech.DistributedCache</PackageId>
1313
<Title>Pandatech Distributed Cache</Title>
1414
<PackageTags>Pandatech, library, redis, distributed locks, cache</PackageTags>

src/DistributedCache/Services/Implementations/RedisRateLimitService.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Diagnostics;
2+
using System.Reflection;
23
using DistributedCache.Dtos;
34
using DistributedCache.Enums;
45
using DistributedCache.Helpers;
@@ -17,15 +18,14 @@ public class RedisRateLimitService(
1718
private readonly IRedisDatabase _redisDatabase = redisClient.GetDefaultDatabase();
1819
private readonly CacheConfigurationOptions _config = options.Value;
1920

20-
private readonly string _moduleName = Assembly.GetCallingAssembly()
21-
.GetName().Name!;
22-
2321
public async ValueTask<RateLimitState> RateLimitAsync(RateLimitConfiguration rateLimitConfiguration,
2422
CancellationToken cancellationToken = default)
2523
{
24+
var assemblyName = GetCallingAssemblyName();
25+
2626
var key = _config.KeyPrefixForIsolation == KeyPrefix.None
2727
? KeyFormatHelper.GetPrefixedKey(rateLimitConfiguration.GetKey())
28-
: KeyFormatHelper.GetPrefixedKey(rateLimitConfiguration.GetKey(), _moduleName);
28+
: KeyFormatHelper.GetPrefixedKey(rateLimitConfiguration.GetKey(), assemblyName);
2929

3030

3131
var lockValue = Guid.NewGuid()
@@ -94,4 +94,18 @@ public async ValueTask<RateLimitState> RateLimitAsync(RateLimitConfiguration rat
9494
await lockService.ReleaseLockAsync(key, lockValue);
9595
}
9696
}
97+
98+
private static string GetCallingAssemblyName()
99+
{
100+
var stackTrace = new StackTrace();
101+
foreach (var frame in stackTrace.GetFrames())
102+
{
103+
var method = frame.GetMethod();
104+
if (method != null && method.DeclaringType != typeof(RedisRateLimitService))
105+
{
106+
return method.DeclaringType!.Assembly.GetName().Name!;
107+
}
108+
}
109+
return Assembly.GetExecutingAssembly().GetName().Name!;
110+
}
97111
}

0 commit comments

Comments
 (0)