Commit 421e2598 authored by justcoding121's avatar justcoding121

return/continue check in retry

parent f3861064
...@@ -203,6 +203,12 @@ namespace Titanium.Web.Proxy ...@@ -203,6 +203,12 @@ namespace Titanium.Web.Proxy
{ {
decryptSsl = false; decryptSsl = false;
} }
if(!decryptSsl)
{
await tcpConnectionFactory.Release(prefetchConnectionTask, true);
prefetchConnectionTask = null;
}
} }
if (cancellationTokenSource.IsCancellationRequested) if (cancellationTokenSource.IsCancellationRequested)
...@@ -250,8 +256,6 @@ namespace Titanium.Web.Proxy ...@@ -250,8 +256,6 @@ namespace Titanium.Web.Proxy
(buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); }, (buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); },
(buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); }, (buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); },
connectArgs.CancellationTokenSource, ExceptionFunc); connectArgs.CancellationTokenSource, ExceptionFunc);
} }
finally finally
{ {
......
...@@ -11,8 +11,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -11,8 +11,6 @@ namespace Titanium.Web.Proxy.Network
private readonly TcpConnectionFactory tcpConnectionFactory; private readonly TcpConnectionFactory tcpConnectionFactory;
private TcpServerConnection currentConnection; private TcpServerConnection currentConnection;
private Exception exception;
private Policy policy; private Policy policy;
internal RetryPolicy(int retries, TcpConnectionFactory tcpConnectionFactory) internal RetryPolicy(int retries, TcpConnectionFactory tcpConnectionFactory)
...@@ -26,15 +24,16 @@ namespace Titanium.Web.Proxy.Network ...@@ -26,15 +24,16 @@ namespace Titanium.Web.Proxy.Network
/// <summary> /// <summary>
/// Execute and retry the given action until retry number of times. /// Execute and retry the given action until retry number of times.
/// </summary> /// </summary>
/// <param name="action">The action to retry.</param> /// <param name="action">The action to retry with return value specifying whether caller should continue execution.</param>
/// <param name="generator">The Tcp connection generator to be invoked to get new connection for retry.</param> /// <param name="generator">The Tcp connection generator to be invoked to get new connection for retry.</param>
/// <param name="initialConnection">Initial Tcp connection to use.</param> /// <param name="initialConnection">Initial Tcp connection to use.</param>
/// <returns>Returns the latest connection used and the latest exception if any.</returns> /// <returns>Returns the latest connection used and the latest exception if any.</returns>
internal async Task<RetryResult> ExecuteAsync(Func<TcpServerConnection, Task> action, internal async Task<RetryResult> ExecuteAsync(Func<TcpServerConnection, Task<bool>> action,
Func<Task<TcpServerConnection>> generator, TcpServerConnection initialConnection) Func<Task<TcpServerConnection>> generator, TcpServerConnection initialConnection)
{ {
currentConnection = initialConnection; currentConnection = initialConnection;
exception = null; Exception exception = null;
bool @continue = true;
try try
{ {
...@@ -46,13 +45,13 @@ namespace Titanium.Web.Proxy.Network ...@@ -46,13 +45,13 @@ namespace Titanium.Web.Proxy.Network
currentConnection = currentConnection as TcpServerConnection ?? currentConnection = currentConnection as TcpServerConnection ??
await generator(); await generator();
//try //try
await action(currentConnection); @continue = await action(currentConnection);
}); });
} }
catch (Exception e) { exception = e; } catch (Exception e) { exception = e; }
return new RetryResult(currentConnection, exception); return new RetryResult(currentConnection, exception, @continue);
} }
//get the policy //get the policy
...@@ -60,16 +59,18 @@ namespace Titanium.Web.Proxy.Network ...@@ -60,16 +59,18 @@ namespace Titanium.Web.Proxy.Network
{ {
return Policy.Handle<T>() return Policy.Handle<T>()
.RetryAsync(retries, .RetryAsync(retries,
onRetryAsync: async (ex, i, context) => onRetryAsync: onRetry);
{ }
if (currentConnection != null)
{
//close connection on error
await tcpConnectionFactory.Release(currentConnection, true);
currentConnection = null;
}
}); //before retry clear connection
private async Task onRetry(Exception ex, int attempt)
{
if (currentConnection != null)
{
//close connection on error
await tcpConnectionFactory.Release(currentConnection, true);
currentConnection = null;
}
} }
} }
...@@ -78,11 +79,13 @@ namespace Titanium.Web.Proxy.Network ...@@ -78,11 +79,13 @@ namespace Titanium.Web.Proxy.Network
internal bool IsSuccess => Exception == null; internal bool IsSuccess => Exception == null;
internal TcpServerConnection LatestConnection { get; } internal TcpServerConnection LatestConnection { get; }
internal Exception Exception { get; } internal Exception Exception { get; }
internal bool Continue { get; }
internal RetryResult(TcpServerConnection lastConnection, Exception exception) internal RetryResult(TcpServerConnection lastConnection, Exception exception, bool @continue)
{ {
LatestConnection = lastConnection; LatestConnection = lastConnection;
Exception = exception; Exception = exception;
Continue = @continue;
} }
} }
} }
...@@ -220,11 +220,12 @@ namespace Titanium.Web.Proxy ...@@ -220,11 +220,12 @@ namespace Titanium.Web.Proxy
response, clientStream, clientStreamWriter, response, clientStream, clientStreamWriter,
serverConnection, cancellationTokenSource, cancellationToken); serverConnection, cancellationTokenSource, cancellationToken);
closeServerConnection = true; closeServerConnection = true;
return; return false;
} }
// construct the web request that we are going to issue on behalf of the client. // construct the web request that we are going to issue on behalf of the client.
await handleHttpSessionRequestInternal(serverConnection, args); await handleHttpSessionRequestInternal(serverConnection, args);
return true;
}, generator, connection); }, generator, connection);
...@@ -237,6 +238,11 @@ namespace Titanium.Web.Proxy ...@@ -237,6 +238,11 @@ namespace Titanium.Web.Proxy
throw result.Exception; throw result.Exception;
} }
if(!result.Continue)
{
return;
}
//user requested //user requested
if (args.WebSession.CloseServerConnection) if (args.WebSession.CloseServerConnection)
{ {
......
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