Commit e49020b4 authored by justcoding121's avatar justcoding121

return/continue check in retry

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