From 10eb5209a74e2d1ff09ea0433e61c2bcbb011505 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Fri, 13 Dec 2019 15:36:52 +0800 Subject: [PATCH] Code refactoring --- .../Internal/IMessageSender.Default.cs | 14 +--- .../Internal/ISubscribeDispatcher.Default.cs | 9 +-- src/DotNetCore.CAP/Processor/RetryBehavior.cs | 79 ------------------- 3 files changed, 8 insertions(+), 94 deletions(-) delete mode 100644 src/DotNetCore.CAP/Processor/RetryBehavior.cs diff --git a/src/DotNetCore.CAP/Internal/IMessageSender.Default.cs b/src/DotNetCore.CAP/Internal/IMessageSender.Default.cs index 4bceda8..fae0aed 100644 --- a/src/DotNetCore.CAP/Internal/IMessageSender.Default.cs +++ b/src/DotNetCore.CAP/Internal/IMessageSender.Default.cs @@ -95,10 +95,8 @@ namespace DotNetCore.CAP.Internal { var needRetry = UpdateMessageForRetry(message); - if (message.ExpiresAt != null) - { - message.ExpiresAt = DateTime.Now.AddDays(15); - } + message.ExpiresAt = message.Added.AddDays(15); + await _dataStorage.ChangePublishStateAsync(message, StatusName.Failed); return needRetry; @@ -106,12 +104,8 @@ namespace DotNetCore.CAP.Internal private bool UpdateMessageForRetry(MediumMessage message) { - var retryBehavior = RetryBehavior.DefaultRetry; - var retries = ++message.Retries; - message.ExpiresAt = message.Added.AddSeconds(retryBehavior.RetryIn(retries)); - - var retryCount = Math.Min(_options.Value.FailedRetryCount, retryBehavior.RetryCount); + var retryCount = Math.Min(_options.Value.FailedRetryCount, 3); if (retries >= retryCount) { if (retries == _options.Value.FailedRetryCount) @@ -194,7 +188,7 @@ namespace DotNetCore.CAP.Internal s_diagnosticListener.Write(CapDiagnosticListenerNames.ErrorPublish, eventData); } - } + } #endregion } diff --git a/src/DotNetCore.CAP/Internal/ISubscribeDispatcher.Default.cs b/src/DotNetCore.CAP/Internal/ISubscribeDispatcher.Default.cs index 468f495..30d57d6 100644 --- a/src/DotNetCore.CAP/Internal/ISubscribeDispatcher.Default.cs +++ b/src/DotNetCore.CAP/Internal/ISubscribeDispatcher.Default.cs @@ -128,6 +128,8 @@ namespace DotNetCore.CAP.Internal var needRetry = UpdateMessageForRetry(message); + message.ExpiresAt = message.Added.AddDays(15); + await _dataStorage.ChangeReceiveStateAsync(message, StatusName.Failed); return needRetry; @@ -135,12 +137,9 @@ namespace DotNetCore.CAP.Internal private bool UpdateMessageForRetry(MediumMessage message) { - var retryBehavior = RetryBehavior.DefaultRetry; - var retries = ++message.Retries; - message.ExpiresAt = message.Added.AddSeconds(retryBehavior.RetryIn(retries)); - var retryCount = Math.Min(_options.FailedRetryCount, retryBehavior.RetryCount); + var retryCount = Math.Min(_options.FailedRetryCount, 3); if (retries >= retryCount) { if (retries == _options.FailedRetryCount) @@ -197,7 +196,7 @@ namespace DotNetCore.CAP.Internal catch (Exception ex) { var e = new SubscriberExecutionFailedException(ex.Message, ex); - + TracingError(tracingTimestamp, message.Origin, descriptor.MethodInfo, e); throw e; diff --git a/src/DotNetCore.CAP/Processor/RetryBehavior.cs b/src/DotNetCore.CAP/Processor/RetryBehavior.cs deleted file mode 100644 index 05115fc..0000000 --- a/src/DotNetCore.CAP/Processor/RetryBehavior.cs +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) .NET Core Community. All rights reserved. -// Licensed under the MIT License. See License.txt in the project root for license information. - -using System; - -namespace DotNetCore.CAP.Processor -{ - public class RetryBehavior - { - public static readonly int DefaultRetryCount; - public static readonly Func DefaultRetryInThunk; - - public static readonly RetryBehavior DefaultRetry; - public static readonly RetryBehavior NoRetry; - - // ReSharper disable once InconsistentNaming - private static readonly Random _random = new Random(); - - private readonly Func _retryInThunk; - - static RetryBehavior() - { - DefaultRetryCount = 3; - DefaultRetryInThunk = retries => - (int) Math.Round(Math.Pow(retries - 1, 4) + 3 + _random.Next(30) * retries); - - DefaultRetry = new RetryBehavior(true); - NoRetry = new RetryBehavior(false); - } - - public RetryBehavior(bool retry) - : this(retry, DefaultRetryCount, DefaultRetryInThunk) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// Whether to retry. - /// The maximum retry count. - /// The retry in function to use. - public RetryBehavior(bool retry, int retryCount, Func retryInThunk) - { - if (retry) - { - if (retryCount < 0) - { - throw new ArgumentOutOfRangeException(nameof(retryCount), "Can't be negative."); - } - } - - Retry = retry; - RetryCount = retryCount; - _retryInThunk = retryInThunk ?? DefaultRetryInThunk; - } - - public Random Random => _random; - - /// - /// Gets whether to retry or disable retrying. - /// - public bool Retry { get; } - - /// - /// Gets the maximum retry count. - /// - public int RetryCount { get; } - - /// - /// Returns the seconds to delay before retrying again. - /// - /// The current retry count. - /// The seconds to delay. - public int RetryIn(int retries) - { - return _retryInThunk(retries); - } - } -} \ No newline at end of file