public async Task <IDisposable> WaitForReadiness(CancellationToken cancellationToken)
        {
            await _Semafore.WaitAsync(cancellationToken);

            var count = 0;
            var now = _Time.GetNow();
            var target = now - _TimeSpan;
            LinkedListNode <DateTime> element = _TimeStamps.First, last = null;

            while ((element != null) && (element.Value > target))
            {
                last    = element;
                element = element.Next;
                count++;
            }

            if (count < _Count)
            {
                return(new DisposeAction(OnEnded));
            }

            Debug.Assert(element == null);
            Debug.Assert(last != null);
            var timetoWait = last.Value.Add(_TimeSpan) - now;

            try
            {
                await _Time.GetDelay(timetoWait, cancellationToken);
            }
            catch (Exception)
            {
                _Semafore.Release();
                throw;
            }

            return(new DisposeAction(OnEnded));
        }
Esempio n. 2
0
        public SendOTPResponse SendOTP()
        {
            var otpCount = counter.GetOTP();
            var backOff  = dac.GetBackOff();

            var now = time.GetNow();

            if (now >= backOff.UnlockedTime)
            {
                var secound = GetUnLockedTimeSecound(otpCount);

                backOff.UnlockedTime = now.AddSeconds(secound);
                dac.UpdateBackOff(backOff);

                counter.IncrementOTP();

                var code = otp.GenerateOTP();

                return(new SendOTPResponse
                {
                    Code = code,
                    ReqAttempt = otpCount + 1,
                    ShouldSendOTP = true,
                    UnlockedTime = backOff.UnlockedTime
                });
            }
            else
            {
                return(new SendOTPResponse
                {
                    ShouldSendOTP = false,
                    UnlockedTime = backOff.UnlockedTime,
                    ReqAttempt = otpCount
                });
            }
        }