/// <exception cref="SequenceCouldNotBeFoundException">Sequence could not be found</exception>
        /// <exception cref="MaximumValueReachedException">The maximum sequence value has been reached and cycle is false</exception>
        /// <exception cref="MinimumValueReachedException">The minimum sequence value has been reached and cycle is false</exception>
        /// <exception cref="MaxRetryAttemptReachedException">The maximum number of retries has been reached</exception>
        public async Task<long> NextAsync(SequenceKey sequenceKey)
        {
            if (sequenceKey == null) throw new ArgumentNullException("sequenceKey");
            if (sequenceKey.Value == null) throw new ArgumentNullException("sequenceKey");

            return await ExecAsync(sequenceKey, 0);
            
        }
Beispiel #2
0
        /// <exception cref="SequenceCouldNotBeFoundException">Sequence could not be found</exception>
        /// <exception cref="MaximumValueReachedException">The maximum sequence value has been reached and cycle is false</exception>
        /// <exception cref="MinimumValueReachedException">The minimum sequence value has been reached and cycle is false</exception>
        /// <exception cref="MaxRetryAttemptReachedException">The maximum number of retries has been reached</exception>
        public async Task <long> NextAsync(SequenceKey sequenceKey)
        {
            if (sequenceKey == null)
            {
                throw new ArgumentNullException("sequenceKey");
            }
            if (sequenceKey.Value == null)
            {
                throw new ArgumentNullException("sequenceKey");
            }

            return(await ExecAsync(sequenceKey, 0));
        }
        private async Task<TryGetSequenceValueResult> TryGetSequenceValue(SequenceKey sequenceKey)
        {
            var sequence = await StateProvider.GetAsync(sequenceKey);

            if (sequence == null)
            {
                throw new SequenceCouldNotBeFoundException();
            }


            var result = new TryGetSequenceValueResult
            {
                Value = sequence.StartAt
            };

            if (sequence.Increment == 0)
            {
                result.Result = true;
                return result;
            }


            

            var sequenceValue =  sequence.CurrentValue + sequence.Increment;

            sequenceValue = CycleOrFailIfGreaterThanMaximum(sequence, sequenceValue);

            sequenceValue = CycleOrFailIfLessThanMinimum(sequence, sequenceValue);

            sequence.CurrentValue = sequenceValue;

            var updateResult = await StateProvider.UpdateAsync(sequenceKey, sequence);

            if (!updateResult)
            {
                return result;
            }

            result.Value = sequence.CurrentValue;

            result.Result = true;

            return result;

        }
Beispiel #4
0
        private async Task <TryGetSequenceValueResult> TryGetSequenceValue(SequenceKey sequenceKey)
        {
            var sequence = await StateProvider.GetAsync(sequenceKey);

            if (sequence == null)
            {
                throw new SequenceCouldNotBeFoundException();
            }


            var result = new TryGetSequenceValueResult
            {
                Value = sequence.StartAt
            };

            if (sequence.Increment == 0)
            {
                result.Result = true;
                return(result);
            }



            var sequenceValue = sequence.CurrentValue + sequence.Increment;

            sequenceValue = CycleOrFailIfGreaterThanMaximum(sequence, sequenceValue);

            sequenceValue = CycleOrFailIfLessThanMinimum(sequence, sequenceValue);

            sequence.CurrentValue = sequenceValue;

            var updateResult = await StateProvider.UpdateAsync(sequenceKey, sequence);

            if (!updateResult)
            {
                return(result);
            }

            result.Value = sequence.CurrentValue;

            result.Result = true;

            return(result);
        }
        private async Task<long> ExecAsync(SequenceKey sequenceKey, int retryAttempt)
        {
            var result = await TryGetSequenceValue(sequenceKey);

            if (!result.Result)
            {
                if (retryAttempt < MaxNumberOfAttempts)
                {
                    retryAttempt++;

                    result.Value = await ExecAsync(sequenceKey, retryAttempt);
                }
                else
                {
                    throw new MaxRetryAttemptReachedException(MaxNumberOfAttempts);
                }
            }

            return result.Value;
        }
Beispiel #6
0
        private async Task <long> ExecAsync(SequenceKey sequenceKey, int retryAttempt)
        {
            var result = await TryGetSequenceValue(sequenceKey);

            if (!result.Result)
            {
                if (retryAttempt < MaxNumberOfAttempts)
                {
                    retryAttempt++;

                    result.Value = await ExecAsync(sequenceKey, retryAttempt);
                }
                else
                {
                    throw new MaxRetryAttemptReachedException(MaxNumberOfAttempts);
                }
            }

            return(result.Value);
        }