public async Task AddOutgoingAsync <TSupplierType>(string outgoingPrefix, TSupplierType supplierType, CancellationToken cancellationToken = default) { try { outgoingPrefix.NotNullOrWhiteSpace(nameof(outgoingPrefix)); supplierType.NotNull(nameof(supplierType)); var strSupplierType = supplierType.ConvertTo <string>(); Logger.SetProperty(LogConstants.SupplierType, strSupplierType); var limitSettings = await LimitSettingService.GetFromCacheBySupplierTypeAsync(strSupplierType, cancellationToken).ConfigureAwait(false); //Can be null foreach (var limitSetting in limitSettings) { var key = outgoingPrefix + $"{limitSetting.Id}_{Guid.NewGuid().ToString("N")}"; var expirationMinutes = Convert.ToInt32(limitSetting.LimitDuration.TotalMinutes); await CacheManager.SetAsync(key, string.Empty, expirationMinutes, cancellationToken).ConfigureAwait(false); } } catch (Exception ex) when(Logger.LogErrorIfNotBefore(ex, "Exception ocurred in {MethodName}", nameof(AddOutgoingAsync))) { throw; } }
public async Task <bool> IsLimitationReachedAsync <TSupplierType>(string outgoingPrefix, TSupplierType supplierType, CancellationToken cancellationToken = default) { try { outgoingPrefix.NotNullOrWhiteSpace(nameof(outgoingPrefix)); supplierType.NotNull(nameof(supplierType)); var hashCode = outgoingPrefix.GetInvariantHashCode(); Logger.SetProperty(LogConstants.OutgoingRequestHashCode, hashCode); Logger.SetProperty(LogConstants.SupplierType, supplierType.ConvertTo <string>()); var limitSettings = await LimitSettingService.GetFromCacheBySupplierTypeAsync(supplierType, cancellationToken).ConfigureAwait(false); foreach (var limitSetting in limitSettings) { var limitDurationDateTime = SystemClock.DateTimeNow.AddHours(-limitSetting.LimitDurationHours); int count; using (await AsyncLock.LockAsync()) count = await OutgoingRequestStore.TableNoTracking.CountAsync(p => p.HashCode == hashCode && p.CreatedAt >= limitDurationDateTime).ConfigureAwait(false); var isLimitationReached = count >= limitSetting.RequestLimit; if (isLimitationReached) { Logger.SetProperty(LogConstants.IsLimitationReached, true); Logger.SetProperty(LogConstants.OutgoingRequestCount, count); Logger.SetProperty(LogConstants.LimitSetting, limitSetting, true); return(true); } } Logger.SetProperty(LogConstants.IsLimitationReached, false); return(false); } catch (Exception ex) when(Logger.LogErrorIfNotBefore(ex, "Exception ocurred in {MethodName}", nameof(IsLimitationReachedAsync))) { throw; } }