public async Task <K> RetryFuncAsync <K>(Func <T, Task <K> > action) { var trial = 0; while (trial < _options.Retries) { try { var timeoutTimer = new System.Timers.Timer(_options.ClientTimeout.TotalMilliseconds); timeoutTimer.Elapsed += (source, e) => throw new TimeoutException(); timeoutTimer.Start(); var result = await action(await _proxyConnectionHandler.GetProxyAsync()); timeoutTimer.Stop(); return(result); } catch (Exception ex) { if (trial == _options.Retries - 1) { throw new RetryPolicyException("The host is not reachable!", ex); } } await _proxyConnectionHandler.ForceReconnectAsync(); trial++; await Task.Delay(_options.DelayBetweenRetries); } return(default);
public async Task <K> RetryFuncAsync <K>(Func <T, Task <K> > action) { var trial = 0; while (trial < _options.Retries) { try { var tokenSource = new CancellationTokenSource(_options.ClientTimeout); return(await Task.Run(async() => await action(await _proxyConnectionHandler.GetProxyAsync()), tokenSource.Token)); } catch (TaskCanceledException) { if (trial == _options.Retries - 1) { throw new RetryPolicyException("The maximum number of retries was reached while sending this request."); } } catch (ScuException) { throw; } catch (Exception ex) { if (trial == _options.Retries - 1) { throw; } } await _proxyConnectionHandler.ForceReconnectAsync(); trial++; await Task.Delay(_options.DelayBetweenRetries); } return(default);