public async Task <CallLimitsResult> ProcessCallLimitsAsync(string clientId, string operation = null, bool checkRepeat = true, bool increaseCount = true)
        {
            operation = operation ?? DefaultOperationName;

            IReadOnlyCollection <DateTime> callDates = await _callTimeLimitsRepository.GetCallHistoryAsync(operation, clientId);

            if (callDates.Any())
            {
                if (callDates.Count >= _callsLimit)
                {
                    return(CallLimitsResult.Failed(CallLimitStatus.LimitExceed));
                }

                if (checkRepeat && DateTime.UtcNow - callDates.Last() < _repeatEnabledTimeSpan)
                {
                    return(CallLimitsResult.Failed(CallLimitStatus.CallTimeout));
                }
            }

            if (increaseCount)
            {
                await _callTimeLimitsRepository.InsertRecordAsync(operation, clientId);
            }

            return(CallLimitsResult.Success(callDates));
        }
예제 #2
0
        public async Task <IActionResult> CheckCallsLimit([FromBody] CheckOperationLimitRequest model)
        {
            try
            {
                CallLimitsResult callLimitsResult = await _callTimeLimitsService.ProcessCallLimitsAsync
                                                        (model.ClientId, model.Operation, model.RepeatCheck, false);

                return(Ok(callLimitsResult.Status));
            }
            catch (Exception exception)
            {
                _log.Error(exception, context: model);

                throw;
            }
        }