Пример #1
0
        public TResult ExecuteAction <TResult>(
            RandomSourceFunc <TSource, TResult> func,
            ShouldRetryPredicate shouldRetry,
            OnError onError = null)
        {
            if (typeof(Task).IsAssignableFrom(typeof(TResult)))
            {
                throw new ArgumentException(
                          "Async action should be executed with ExecuteAsync.",
                          nameof(func));
            }
            for (var attemptCount = 1; ; attemptCount++)
            {
                var item = _chooseRandomly();
                try
                {
                    var result = func(item, attemptCount);

                    _updateWeight(item, true);

                    return(result);
                }
                catch (Exception e)
                {
                    onError?.Invoke(e, attemptCount);
                    _updateWeight(item, false);
                    if (!shouldRetry(attemptCount, e))
                    {
                        throw;
                    }
                }
            }
        }
Пример #2
0
        public async Task <TResult> ExecuteAsync <TResult>(
            RandomSourceAsyncFunc <TSource, TResult> taskFunc,
            ShouldRetryPredicate shouldRetry,
            OnError onError = null)
        {
            for (var attemptCount = 1; ; attemptCount++)
            {
                var item = _chooseRandomly();
                try
                {
                    var result = await taskFunc(item, attemptCount);

                    _updateWeight(item, true);

                    return(result);
                }
                catch (Exception e)
                {
                    onError?.Invoke(e, attemptCount);
                    _updateWeight(item, false);
                    if (!shouldRetry(attemptCount, e))
                    {
                        throw;
                    }
                }
            }
        }
Пример #3
0
        public async Task <IReadOnlyList <RetryActionResult <TSource, TResult> > > ExecuteAsyncWithDiag <TResult>(
            RandomSourceAsyncFunc <TSource, TResult> taskFunc,
            ShouldRetryPredicate shouldRetry,
            OnError onError = null)
        {
            var stopwatch = new Stopwatch();
            var results   = new List <RetryActionResult <TSource, TResult> >();

            for (var attemptCount = 1; ; attemptCount++)
            {
                var item = _chooseRandomly();
                try
                {
                    stopwatch.Restart();
                    var result = await taskFunc(item, attemptCount).ConfigureAwait(false);

                    _updateWeight(item, true);
                    results.Add(new RetryActionResult <TSource, TResult>(
                                    item, result, stopwatch.Elapsed, null, attemptCount));
                    break;
                }
                catch (Exception e)
                {
                    results.Add(new RetryActionResult <TSource, TResult>(
                                    item, default(TResult), stopwatch.Elapsed, e, attemptCount));
                    _updateWeight(item, false);
                    var isFailed = !shouldRetry(attemptCount, e);
                    if (isFailed)
                    {
                        break;
                    }
                }
            }
            return(results);
        }
Пример #4
0
        public GrpcChannelManager(string[] urls, TimeSpan?timeout, int maxRetry = 1)
        {
            _shouldRetry = GetRetryCountPredicate(maxRetry);
            _timeout     = timeout;

            var resourceDict = CreateResourceDictionary(urls.ToDictionary(x => x, x => WeightItem.CreateDefaultItem()));

            ResourceManager = new ResourceManager <GrpcResource>(resourceDict, new AgodaWeightManipulationStrategy());
        }
        public static Task <IReadOnlyList <RetryActionResult <TSource, TResult> > > ExecuteAsyncWithDiag <TSource, TResult>(
            this IResourceManager <TSource> mgr,
            RandomSourceAsyncFunc <TSource, TResult> taskFunc,
            ShouldRetryPredicate shouldRetry,
            OnError onError = null)
        {
            var retryAction = new RetryAction <TSource>(mgr.SelectRandomly, mgr.UpdateWeight);

            return(retryAction.ExecuteAsyncWithDiag(taskFunc, shouldRetry, onError));
        }
        // TODO: Test
        public static TResult ExecuteAction <TSource, TResult>(
            this IResourceManager <TSource> mgr,
            RandomSourceFunc <TSource, TResult> func,
            ShouldRetryPredicate shouldRetry,
            OnError onError = null)
        {
            var retryAction = new RetryAction <TSource>(mgr.SelectRandomly, mgr.UpdateWeight);

            return(retryAction.ExecuteAction(func, shouldRetry, onError));
        }
 public LoadBalancingCallInvoker(
     IResourceManager <GrpcResource> resourceManager,
     TimeSpan?timeout,
     ShouldRetryPredicate shouldRetry,
     OnError onError)
 {
     _resourceManager = resourceManager;
     _timeout         = timeout;
     _shouldRetry     = shouldRetry;
     _onError         = onError;
 }
Пример #8
0
        public GrpcChannelManager(
            IReadOnlyDictionary <string, WeightItem> resources,
            IWeightManipulationStrategy weightStrategy,
            TimeSpan timeout,
            ShouldRetryPredicate shouldRetry)
        {
            _shouldRetry = shouldRetry ?? throw new ArgumentNullException(nameof(shouldRetry));
            _timeout     = timeout;

            var resourceDict = CreateResourceDictionary(resources);

            ResourceManager = new ResourceManager <GrpcResource>(resourceDict, weightStrategy);
        }
 public RandomUrlHttpClient(
     HttpClient httpClient,
     string[] baseUrls,
     TimeSpan?timeout,
     Func <HttpResponseMessage, string, int> isErrorResponse,
     ShouldRetryPredicate shouldRetry)
 {
     HttpClient         = httpClient;
     UrlResourceManager = CreateResourceManager(baseUrls);
     Timeout            = timeout;
     if (isErrorResponse != null)
     {
         _isErrorResponse = isErrorResponse;
     }
     _shouldRetry = shouldRetry ?? throw new ArgumentNullException(nameof(shouldRetry));
 }