Commit 67f9e30b authored by justcoding121's avatar justcoding121

remove polly dependency #458 ?

parent 863ddc93
using Polly; using System;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.Network.Tcp; using Titanium.Web.Proxy.Network.Tcp;
...@@ -11,14 +10,11 @@ namespace Titanium.Web.Proxy.Network ...@@ -11,14 +10,11 @@ namespace Titanium.Web.Proxy.Network
private readonly TcpConnectionFactory tcpConnectionFactory; private readonly TcpConnectionFactory tcpConnectionFactory;
private TcpServerConnection currentConnection; private TcpServerConnection currentConnection;
private Policy policy;
internal RetryPolicy(int retries, TcpConnectionFactory tcpConnectionFactory) internal RetryPolicy(int retries, TcpConnectionFactory tcpConnectionFactory)
{ {
this.retries = retries; this.retries = retries;
this.tcpConnectionFactory = tcpConnectionFactory; this.tcpConnectionFactory = tcpConnectionFactory;
policy = getRetryPolicy();
} }
/// <summary> /// <summary>
...@@ -32,14 +28,14 @@ namespace Titanium.Web.Proxy.Network ...@@ -32,14 +28,14 @@ namespace Titanium.Web.Proxy.Network
Func<Task<TcpServerConnection>> generator, TcpServerConnection initialConnection) Func<Task<TcpServerConnection>> generator, TcpServerConnection initialConnection)
{ {
currentConnection = initialConnection; currentConnection = initialConnection;
Exception exception = null;
bool @continue = true; bool @continue = true;
Exception exception = null;
try var attempts = retries;
while (attempts >= 0)
{ {
//retry on error with polly policy
//do not use polly context to store connection; it does not save states b/w attempts try
await policy.ExecuteAsync(async () =>
{ {
//setup connection //setup connection
currentConnection = currentConnection as TcpServerConnection ?? currentConnection = currentConnection as TcpServerConnection ??
...@@ -47,23 +43,27 @@ namespace Titanium.Web.Proxy.Network ...@@ -47,23 +43,27 @@ namespace Titanium.Web.Proxy.Network
//try //try
@continue = await action(currentConnection); @continue = await action(currentConnection);
}); }
catch (T ex)
{
exception = ex;
await onRetry(ex);
}
if(exception == null)
{
break;
}
exception = null;
attempts--;
} }
catch (Exception e) { exception = e; }
return new RetryResult(currentConnection, exception, @continue); return new RetryResult(currentConnection, exception, @continue);
} }
//get the policy
private Policy getRetryPolicy()
{
return Policy.Handle<T>()
.RetryAsync(retries,
onRetryAsync: onRetry);
}
//before retry clear connection //before retry clear connection
private async Task onRetry(Exception ex, int attempt) private async Task onRetry(Exception ex)
{ {
if (currentConnection != null) if (currentConnection != null)
{ {
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Polly" Version="6.0.1" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.2" /> <PackageReference Include="Portable.BouncyCastle" Version="1.8.2" />
<PackageReference Include="StreamExtended" Version="1.0.179" /> <PackageReference Include="StreamExtended" Version="1.0.179" />
</ItemGroup> </ItemGroup>
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
<dependencies> <dependencies>
<dependency id="StreamExtended" version="1.0.179" /> <dependency id="StreamExtended" version="1.0.179" />
<dependency id="Portable.BouncyCastle" version="1.8.2" /> <dependency id="Portable.BouncyCastle" version="1.8.2" />
<dependency id="Polly" version="6.0.1"/>
</dependencies> </dependencies>
</metadata> </metadata>
<files> <files>
......
...@@ -3,5 +3,4 @@ ...@@ -3,5 +3,4 @@
<packages> <packages>
<package id="Portable.BouncyCastle" version="1.8.2" targetFramework="net45" /> <package id="Portable.BouncyCastle" version="1.8.2" targetFramework="net45" />
<package id="StreamExtended" version="1.0.179" targetFramework="net45" /> <package id="StreamExtended" version="1.0.179" targetFramework="net45" />
<package id="Polly" version="6.0.1" targetFramework="net45" />
</packages> </packages>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment