diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN.Abp.OperationRateLimiting.Domain.Shared.csproj b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN.Abp.OperationRateLimiting.Domain.Shared.csproj new file mode 100644 index 000000000..bf42175c6 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN.Abp.OperationRateLimiting.Domain.Shared.csproj @@ -0,0 +1,33 @@ + + + + + + + net10.0 + LINGYUN.Abp.OperationRateLimiting.Domain.Shared + LINGYUN.Abp.OperationRateLimiting.Domain.Shared + false + false + false + enable + + + + + + + + + + + + + + + + + + + + diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/AbpOperationRateLimitingDomainSharedModule.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/AbpOperationRateLimitingDomainSharedModule.cs new file mode 100644 index 000000000..6d21aa8a2 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/AbpOperationRateLimitingDomainSharedModule.cs @@ -0,0 +1,34 @@ +using LINGYUN.Abp.OperationRateLimiting.Management; +using LINGYUN.Abp.OperationRateLimiting.Management.Localization; +using Volo.Abp.Localization; +using Volo.Abp.Localization.ExceptionHandling; +using Volo.Abp.Modularity; +using Volo.Abp.VirtualFileSystem; + +namespace LINGYUN.Abp.OperationRateLimiting; + +[DependsOn(typeof(AbpLocalizationModule))] +public class AbpOperationRateLimitingDomainSharedModule : AbpModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.FileSets.AddEmbedded(); + }); + + Configure(options => + { + options.Resources + .Add("en") + .AddVirtualJson("/LINGYUN/Abp/OperationRateLimiting/Management/Localization/Resources"); + }); + + Configure(options => + { + options.MapCodeNamespace( + AbpOperationRateLimitingManagementErrorCodes.Namespace, + typeof(OperationRateLimitingManagementResource)); + }); + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/AbpOperationRateLimitingManagementErrorCodes.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/AbpOperationRateLimitingManagementErrorCodes.cs new file mode 100644 index 000000000..520783f9f --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/AbpOperationRateLimitingManagementErrorCodes.cs @@ -0,0 +1,10 @@ +namespace LINGYUN.Abp.OperationRateLimiting.Management; + +public static class AbpOperationRateLimitingManagementErrorCodes +{ + public const string Namespace = "LINGYUN.Abp.OperationRateLimiting.Management"; + + public const string PolicyNameAlreadyExists = Namespace + ":010001"; + + public const string InvalidPolicy = Namespace + ":010002"; +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/Localization/OperationRateLimitingManagementResource.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/Localization/OperationRateLimitingManagementResource.cs new file mode 100644 index 000000000..2321ce012 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/Localization/OperationRateLimitingManagementResource.cs @@ -0,0 +1,8 @@ +using Volo.Abp.Localization; + +namespace LINGYUN.Abp.OperationRateLimiting.Management.Localization; + +[LocalizationResourceName("OperationRateLimitingManagement")] +public class OperationRateLimitingManagementResource +{ +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/Localization/Resources/en.json b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/Localization/Resources/en.json new file mode 100644 index 000000000..5351071df --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/Localization/Resources/en.json @@ -0,0 +1,12 @@ +{ + "culture": "en", + "texts": { + "Permission:OperationRateLimiting": "Operation rate limiting", + "Permission:Policies": "Managing rate limiting policies", + "Permission:Create": "Create", + "Permission:Update": "Update", + "Permission:Delete": "Delete", + "LINGYUN.Abp.OperationRateLimiting.Management:010001": "A rate limiting policy with the same name already exists.", + "LINGYUN.Abp.OperationRateLimiting.Management:010002": "The rate limiting policy is invalid. Check the algorithm, partition and rule values." + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/Localization/Resources/zh-Hans.json b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/Localization/Resources/zh-Hans.json new file mode 100644 index 000000000..aa28cab6f --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/Management/Localization/Resources/zh-Hans.json @@ -0,0 +1,12 @@ +{ + "culture": "zh-Hans", + "texts": { + "Permission:OperationRateLimiting": "操作限流", + "Permission:Policies": "限流策略管理", + "Permission:Create": "创建", + "Permission:Update": "更新", + "Permission:Delete": "删除", + "LINGYUN.Abp.OperationRateLimiting.Management:010001": "已存在同名的限流策略。", + "LINGYUN.Abp.OperationRateLimiting.Management:010002": "限流策略无效,请检查算法、分区以及规则参数。" + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyRecordConsts.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyRecordConsts.cs new file mode 100644 index 000000000..0f9cd5d63 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyRecordConsts.cs @@ -0,0 +1,10 @@ +namespace LINGYUN.Abp.OperationRateLimiting; + +public static class OperationRateLimitingPolicyRecordConsts +{ + public const int MaxNameLength = 128; + + public const int MaxDisplayNameLength = 256; + + public const int MaxErrorCodeLength = 256; +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingRuleRecordConsts.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingRuleRecordConsts.cs new file mode 100644 index 000000000..53109da3f --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain.Shared/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingRuleRecordConsts.cs @@ -0,0 +1,12 @@ +namespace LINGYUN.Abp.OperationRateLimiting; + +public static class OperationRateLimitingRuleRecordConsts +{ + public const int MaxNameLength = 128; + + public const int MaxAlgorithmNameLength = 128; + + public const int MaxPartitionResolverNameLength = 128; + + public const int MaxErrorCodeLength = 256; +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN.Abp.OperationRateLimiting.Domain.csproj b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN.Abp.OperationRateLimiting.Domain.csproj new file mode 100644 index 000000000..08af2cab9 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN.Abp.OperationRateLimiting.Domain.csproj @@ -0,0 +1,28 @@ + + + + + + + net10.0 + LINGYUN.Abp.OperationRateLimiting.Domain + LINGYUN.Abp.OperationRateLimiting.Domain + false + false + false + enable + + + + + + + + + + + + + + + diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/AbpOperationRateLimitingDomainModule.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/AbpOperationRateLimitingDomainModule.cs new file mode 100644 index 000000000..52971923a --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/AbpOperationRateLimitingDomainModule.cs @@ -0,0 +1,14 @@ +using Volo.Abp.Caching; +using Volo.Abp.Domain; +using Volo.Abp.Modularity; + +namespace LINGYUN.Abp.OperationRateLimiting; + +[DependsOn( + typeof(AbpDddDomainModule), + typeof(AbpCachingModule), + typeof(AbpOperationRateLimitingModule), + typeof(AbpOperationRateLimitingDomainSharedModule))] +public class AbpOperationRateLimitingDomainModule : AbpModule +{ +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/DynamicOperationRateLimitingPolicyStore.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/DynamicOperationRateLimitingPolicyStore.cs new file mode 100644 index 000000000..883303e09 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/DynamicOperationRateLimitingPolicyStore.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Distributed; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; +using Volo.Abp.MultiTenancy; + +namespace LINGYUN.Abp.OperationRateLimiting; + +/// +/// Loads the dynamic policies from the database and caches them. +/// +/// +/// The cache is invalidated explicitly with (for example after +/// an administrative update) and expires automatically after a few minutes. +/// Because the cache key contains the current tenant, invalidation only affects the current tenant. +/// +public class DynamicOperationRateLimitingPolicyStore : + IDynamicOperationRateLimitingPolicyStore, + ITransientDependency +{ + protected IOperationRateLimitingPolicyRecordRepository Repository { get; } + + protected IDistributedCache Cache { get; } + + protected ICurrentTenant CurrentTenant { get; } + + public DynamicOperationRateLimitingPolicyStore( + IOperationRateLimitingPolicyRecordRepository repository, + IDistributedCache cache, + ICurrentTenant currentTenant) + { + Repository = repository; + Cache = cache; + CurrentTenant = currentTenant; + } + + public virtual async Task> GetPoliciesAsync( + CancellationToken cancellationToken = default) + { + var cacheKey = GetCacheKey(); + var cacheItem = await Cache.GetAsync(cacheKey, token: cancellationToken); + + if (cacheItem == null) + { + var records = await Repository.GetListAsync( + includeDetails: true, + cancellationToken: cancellationToken); + + cacheItem = new OperationRateLimitingPolicyCacheItem + { + Policies = records + .Select(record => record.ToPolicy()) + .ToList() + }; + + await Cache.SetAsync( + cacheKey, + cacheItem, + new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5) + }, + token: cancellationToken); + } + + return cacheItem.Policies; + } + + public virtual async Task GetOrNullAsync( + string name, + CancellationToken cancellationToken = default) + { + var policies = await GetPoliciesAsync(cancellationToken); + + return policies.FirstOrDefault(policy => string.Equals(policy.Name, name, StringComparison.OrdinalIgnoreCase)); + } + + public virtual async Task InvalidateCacheAsync(CancellationToken cancellationToken = default) + { + await Cache.RemoveAsync(GetCacheKey(), token: cancellationToken); + } + + protected virtual string GetCacheKey() + { + return $"{CurrentTenant.Id?.ToString() ?? AbpOperationRateLimitingConsts.HostTenantName}:all"; + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/IOperationRateLimitingPolicyRecordRepository.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/IOperationRateLimitingPolicyRecordRepository.cs new file mode 100644 index 000000000..2e606af6d --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/IOperationRateLimitingPolicyRecordRepository.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.Specifications; + +namespace LINGYUN.Abp.OperationRateLimiting; + +public interface IOperationRateLimitingPolicyRecordRepository : IRepository +{ + Task FindByNameAsync( + string name, + bool includeDetails = true, + CancellationToken cancellationToken = default); + + Task GetCountAsync( + ISpecification specification, + CancellationToken cancellationToken = default); + + Task> GetListAsync( + ISpecification specification, + string? sorting = $"{nameof(OperationRateLimitingPolicyRecord.CreationTime)} DESC", + int maxResultCount = 10, + int skipCount = 0, + CancellationToken cancellationToken = default); +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyCacheItem.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyCacheItem.cs new file mode 100644 index 000000000..6a2fcab7e --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyCacheItem.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using Volo.Abp.Caching; + +namespace LINGYUN.Abp.OperationRateLimiting; + +[CacheName(AbpOperationRateLimitingConsts.PolicyCacheName)] +public class OperationRateLimitingPolicyCacheItem +{ + public List Policies { get; set; } = new(); +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyRecord.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyRecord.cs new file mode 100644 index 000000000..d885d0bc1 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyRecord.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using Volo.Abp; +using Volo.Abp.Domain.Entities.Auditing; +using Volo.Abp.MultiTenancy; + +namespace LINGYUN.Abp.OperationRateLimiting; + +/// +/// Persisted definition of an operation rate limiting policy. +/// +/// +/// Rules are stored in the child table and merged +/// with the statically defined policies by . +/// +public class OperationRateLimitingPolicyRecord : FullAuditedAggregateRoot, IMultiTenant +{ + public virtual Guid? TenantId { get; protected set; } + + /// + /// Unique (per tenant) name of the policy. + /// + public virtual string Name { get; protected set; } = default!; + + public virtual string? DisplayName { get; set; } + + public virtual bool IsEnabled { get; set; } = true; + + public virtual string? ErrorCode { get; set; } + + /// + /// Rules of the policy. + /// + public virtual ICollection Rules { get; protected set; } + + protected OperationRateLimitingPolicyRecord() + { + Rules = new Collection(); + } + + public OperationRateLimitingPolicyRecord(Guid id, string name, Guid? tenantId = null) + : base(id) + { + Check.NotNullOrWhiteSpace(name, nameof(name)); + + Name = name; + TenantId = tenantId; + + Rules = new Collection(); + } + + /// + /// Replaces all rules of the policy. The caller owns the new rule instances. + /// + public virtual void SetRules(IEnumerable rules) + { + Rules.Clear(); + + foreach (var rule in rules) + { + rule.PolicyId = Id; + Rules.Add(rule); + } + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyRecordExtensions.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyRecordExtensions.cs new file mode 100644 index 000000000..943d4f8dc --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingPolicyRecordExtensions.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace LINGYUN.Abp.OperationRateLimiting; + +public static class OperationRateLimitingPolicyRecordExtensions +{ + /// + /// Maps a persisted policy record (with its rules) to the runtime policy definition. + /// + public static OperationRateLimitingPolicy ToPolicy(this OperationRateLimitingPolicyRecord record) + { + return new OperationRateLimitingPolicy(record.Name) + { + DisplayName = record.DisplayName, + ErrorCode = record.ErrorCode, + IsEnabled = record.IsEnabled, + Rules = record.Rules + .OrderBy(rule => rule.SortOrder) + .Select(rule => rule.ToDefinition()) + .ToList() + }; + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingRuleRecord.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingRuleRecord.cs new file mode 100644 index 000000000..902b041cb --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingRuleRecord.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using Volo.Abp.Domain.Entities; +using Volo.Abp.MultiTenancy; + +namespace LINGYUN.Abp.OperationRateLimiting; + +/// +/// Persisted rule of an operation rate limiting policy. +/// +public class OperationRateLimitingRuleRecord : Entity, IMultiTenant +{ + public virtual Guid? TenantId { get; internal set; } + + public Guid PolicyId { get; internal set; } + + /// + /// Optional stable rule name used as the counter key. + /// + public string? Name { get; set; } + + public string Algorithm { get; set; } = OperationRateLimitingAlgorithms.FixedWindow; + + public TimeSpan Duration { get; set; } = TimeSpan.FromMinutes(1); + + public int MaxCount { get; set; } = 1; + + /// + /// Order of the rule inside the policy. + /// + public int SortOrder { get; set; } + + public OperationRateLimitingPartitionType PartitionType { get; set; } = OperationRateLimitingPartitionType.Parameter; + + public string? PartitionResolverName { get; set; } + + public bool IsMultiTenancyEnabled { get; set; } + + public string? ErrorCode { get; set; } + + /// + /// Algorithm specific values (for example SlidingWindow:Segments). + /// + public Dictionary Properties { get; set; } = new(); + + protected OperationRateLimitingRuleRecord() + { + } + + public OperationRateLimitingRuleRecord(Guid id, Guid policyId, string algorithm, Guid? tenantId = null) + : base(id) + { + PolicyId = policyId; + Algorithm = algorithm; + TenantId = tenantId; + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingRuleRecordExtensions.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingRuleRecordExtensions.cs new file mode 100644 index 000000000..b3b20bf37 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.Domain/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingRuleRecordExtensions.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; + +namespace LINGYUN.Abp.OperationRateLimiting; + +public static class OperationRateLimitingRuleRecordExtensions +{ + public static OperationRateLimitingRuleDefinition ToDefinition(this OperationRateLimitingRuleRecord record) + { + return new OperationRateLimitingRuleDefinition + { + Name = record.Name, + Algorithm = record.Algorithm, + Duration = record.Duration, + MaxCount = record.MaxCount, + PartitionType = record.PartitionType, + PartitionResolverName = record.PartitionResolverName, + IsMultiTenancyEnabled = record.IsMultiTenancyEnabled, + ErrorCode = record.ErrorCode, + Properties = new Dictionary(record.Properties, StringComparer.Ordinal) + }; + } + + public static OperationRateLimitingRuleRecord ToRecord( + this OperationRateLimitingRuleDefinition definition, + Guid id, + Guid policyId, + int sortOrder) + { + return new OperationRateLimitingRuleRecord(id, policyId, definition.Algorithm) + { + Name = definition.Name, + Duration = definition.Duration, + MaxCount = definition.MaxCount, + SortOrder = sortOrder, + PartitionType = definition.PartitionType, + PartitionResolverName = definition.PartitionResolverName, + IsMultiTenancyEnabled = definition.IsMultiTenancyEnabled, + ErrorCode = definition.ErrorCode, + Properties = new Dictionary(definition.Properties, StringComparer.Ordinal) + }; + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore.csproj b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore.csproj new file mode 100644 index 000000000..87b3e3708 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore.csproj @@ -0,0 +1,26 @@ + + + + + + + net10.0 + LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore + LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore + false + false + false + enable + + + + + + + + + + + + + diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/AbpOperationRateLimitingEntityFrameworkCoreModule.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/AbpOperationRateLimitingEntityFrameworkCoreModule.cs new file mode 100644 index 000000000..5023635c2 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/AbpOperationRateLimitingEntityFrameworkCoreModule.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Modularity; + +namespace LINGYUN.Abp.OperationRateLimiting; + +[DependsOn( + typeof(AbpEntityFrameworkCoreModule), + typeof(AbpOperationRateLimitingDomainModule))] +public class AbpOperationRateLimitingEntityFrameworkCoreModule : AbpModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddAbpDbContext(options => + { + options.AddRepository(); + }); + + context.Services.AddTransient< + IOperationRateLimitingPolicyRecordRepository, + EfCoreOperationRateLimitingPolicyRecordRepository>(); + + context.Services.AddTransient< + IDynamicOperationRateLimitingPolicyStore, + DynamicOperationRateLimitingPolicyStore>(); + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/EfCoreOperationRateLimitingPolicyRecordRepository.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/EfCoreOperationRateLimitingPolicyRecordRepository.cs new file mode 100644 index 000000000..1face381b --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/EfCoreOperationRateLimitingPolicyRecordRepository.cs @@ -0,0 +1,63 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Dynamic.Core; +using System.Threading; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Specifications; + +namespace LINGYUN.Abp.OperationRateLimiting; + +public class EfCoreOperationRateLimitingPolicyRecordRepository : + EfCoreRepository, + IOperationRateLimitingPolicyRecordRepository +{ + public EfCoreOperationRateLimitingPolicyRecordRepository( + IDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + } + + public async override Task> WithDetailsAsync() + { + return (await GetQueryableAsync()).Include(policy => policy.Rules); + } + + public async virtual Task FindByNameAsync( + string name, + bool includeDetails = true, + CancellationToken cancellationToken = default) + { + return await FindAsync( + policy => policy.Name == name, + includeDetails, + cancellationToken); + } + + public async virtual Task GetCountAsync( + ISpecification specification, + CancellationToken cancellationToken = default) + { + return await (await GetQueryableAsync()) + .Where(specification.ToExpression()) + .CountAsync(GetCancellationToken(cancellationToken)); + } + + public async virtual Task> GetListAsync( + ISpecification specification, + string? sorting = $"{nameof(OperationRateLimitingPolicyRecord.CreationTime)} DESC", + int maxResultCount = 10, + int skipCount = 0, + CancellationToken cancellationToken = default) + { + return await (await GetQueryableAsync()) + .Include(policy => policy.Rules) + .Where(specification.ToExpression()) + .OrderBy(!sorting.IsNullOrWhiteSpace() ? sorting : $"{nameof(OperationRateLimitingPolicyRecord.CreationTime)} DESC") + .PageBy(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/IOperationRateLimitingDbContext.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/IOperationRateLimitingDbContext.cs new file mode 100644 index 000000000..52a0e83e2 --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/IOperationRateLimitingDbContext.cs @@ -0,0 +1,11 @@ +using Microsoft.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace LINGYUN.Abp.OperationRateLimiting; + +public interface IOperationRateLimitingDbContext : IEfCoreDbContext +{ + DbSet OperationRateLimitingPolicies { get; } + + DbSet OperationRateLimitingPolicyRules { get; } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingDbContext.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingDbContext.cs new file mode 100644 index 000000000..f8a76377e --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingDbContext.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace LINGYUN.Abp.OperationRateLimiting; + +/// +/// Dedicated DbContext of the operation rate limiting module. It can be used by a DbMigrator +/// or by applications that do not want to add the entities to their main DbContext. +/// +public class OperationRateLimitingDbContext : + AbpDbContext, + IOperationRateLimitingDbContext +{ + public DbSet OperationRateLimitingPolicies { get; set; } = default!; + + public DbSet OperationRateLimitingPolicyRules { get; set; } = default!; + + public OperationRateLimitingDbContext(DbContextOptions options) + : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); + + builder.ConfigureOperationRateLimiting(); + } +} diff --git a/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingDbContextModelCreatingExtensions.cs b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingDbContextModelCreatingExtensions.cs new file mode 100644 index 000000000..ae175e52e --- /dev/null +++ b/aspnet-core/modules/operation-rate-limiting/LINGYUN.Abp.OperationRateLimiting.EntityFrameworkCore/LINGYUN/Abp/OperationRateLimiting/OperationRateLimitingDbContextModelCreatingExtensions.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; +using Volo.Abp; +using Volo.Abp.EntityFrameworkCore.Modeling; + +namespace LINGYUN.Abp.OperationRateLimiting; + +public static class OperationRateLimitingDbContextModelCreatingExtensions +{ + /// + /// Adds the operation rate limiting tables to a DbContext. + /// + public static void ConfigureOperationRateLimiting(this ModelBuilder builder) + { + Check.NotNull(builder, nameof(builder)); + + builder.Entity(entity => + { + entity.ToTable("OperationRateLimitingPolicies"); + entity.ConfigureByConvention(); + + entity.Property(policy => policy.Name) + .IsRequired() + .HasMaxLength(OperationRateLimitingPolicyRecordConsts.MaxNameLength); + + entity.Property(policy => policy.DisplayName) + .HasMaxLength(OperationRateLimitingPolicyRecordConsts.MaxDisplayNameLength); + + entity.Property(policy => policy.ErrorCode) + .HasMaxLength(OperationRateLimitingPolicyRecordConsts.MaxErrorCodeLength); + + entity.HasIndex(policy => new { policy.TenantId, policy.Name }).IsUnique(); + + entity.HasMany(policy => policy.Rules) + .WithOne() + .HasForeignKey(rule => rule.PolicyId) + .OnDelete(DeleteBehavior.Cascade); + }); + + builder.Entity(entity => + { + entity.ToTable("OperationRateLimitingPolicyRules"); + entity.ConfigureByConvention(); + + entity.Property(rule => rule.Algorithm) + .IsRequired() + .HasMaxLength(OperationRateLimitingRuleRecordConsts.MaxAlgorithmNameLength); + + entity.Property(rule => rule.Name) + .HasMaxLength(OperationRateLimitingRuleRecordConsts.MaxNameLength); + + entity.Property(rule => rule.PartitionResolverName) + .HasMaxLength(OperationRateLimitingRuleRecordConsts.MaxPartitionResolverNameLength); + + entity.Property(rule => rule.ErrorCode) + .HasMaxLength(OperationRateLimitingRuleRecordConsts.MaxErrorCodeLength); + + entity.HasIndex(rule => new { rule.PolicyId, rule.SortOrder }); + + var propertiesComparer = new ValueComparer>( + (left, right) => ReferenceEquals(left, right) || + (left != null && right != null && left.SequenceEqual(right)), + properties => properties == null + ? 0 + : properties.Aggregate( + 0, + (hash, property) => HashCode.Combine(hash, property.Key.GetHashCode(), property.Value.GetHashCode())), + properties => new Dictionary(properties, StringComparer.Ordinal)); + + entity.Property(rule => rule.Properties) + .HasConversion( + properties => JsonSerializer.Serialize(properties, JsonSerializerOptions.Default), + json => JsonSerializer.Deserialize>(json, JsonSerializerOptions.Default) + ?? new Dictionary()) + .Metadata.SetValueComparer(propertiesComparer); + }); + } +}