Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 421e259

Browse files
committed
return/continue check in retry
1 parent f386106 commit 421e259

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.WebSession.Response,
203203
{
204204
decryptSsl = false;
205205
}
206+
207+
if(!decryptSsl)
208+
{
209+
await tcpConnectionFactory.Release(prefetchConnectionTask, true);
210+
prefetchConnectionTask = null;
211+
}
206212
}
207213

208214
if (cancellationTokenSource.IsCancellationRequested)
@@ -250,8 +256,6 @@ await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool, BufferSize,
250256
(buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); },
251257
(buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); },
252258
connectArgs.CancellationTokenSource, ExceptionFunc);
253-
254-
255259
}
256260
finally
257261
{

Titanium.Web.Proxy/Network/RetryPolicy.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ internal class RetryPolicy<T> where T : Exception
1111
private readonly TcpConnectionFactory tcpConnectionFactory;
1212

1313
private TcpServerConnection currentConnection;
14-
private Exception exception;
15-
1614
private Policy policy;
1715

1816
internal RetryPolicy(int retries, TcpConnectionFactory tcpConnectionFactory)
@@ -26,15 +24,16 @@ internal RetryPolicy(int retries, TcpConnectionFactory tcpConnectionFactory)
2624
/// <summary>
2725
/// Execute and retry the given action until retry number of times.
2826
/// </summary>
29-
/// <param name="action">The action to retry.</param>
27+
/// <param name="action">The action to retry with return value specifying whether caller should continue execution.</param>
3028
/// <param name="generator">The Tcp connection generator to be invoked to get new connection for retry.</param>
3129
/// <param name="initialConnection">Initial Tcp connection to use.</param>
3230
/// <returns>Returns the latest connection used and the latest exception if any.</returns>
33-
internal async Task<RetryResult> ExecuteAsync(Func<TcpServerConnection, Task> action,
31+
internal async Task<RetryResult> ExecuteAsync(Func<TcpServerConnection, Task<bool>> action,
3432
Func<Task<TcpServerConnection>> generator, TcpServerConnection initialConnection)
3533
{
3634
currentConnection = initialConnection;
37-
exception = null;
35+
Exception exception = null;
36+
bool @continue = true;
3837

3938
try
4039
{
@@ -46,30 +45,32 @@ await policy.ExecuteAsync(async () =>
4645
currentConnection = currentConnection as TcpServerConnection ??
4746
await generator();
4847
//try
49-
await action(currentConnection);
48+
@continue = await action(currentConnection);
5049

5150
});
5251
}
5352
catch (Exception e) { exception = e; }
5453

55-
return new RetryResult(currentConnection, exception);
54+
return new RetryResult(currentConnection, exception, @continue);
5655
}
5756

5857
//get the policy
5958
private Policy getRetryPolicy()
6059
{
6160
return Policy.Handle<T>()
6261
.RetryAsync(retries,
63-
onRetryAsync: async (ex, i, context) =>
64-
{
65-
if (currentConnection != null)
66-
{
67-
//close connection on error
68-
await tcpConnectionFactory.Release(currentConnection, true);
69-
currentConnection = null;
70-
}
62+
onRetryAsync: onRetry);
63+
}
7164

72-
});
65+
//before retry clear connection
66+
private async Task onRetry(Exception ex, int attempt)
67+
{
68+
if (currentConnection != null)
69+
{
70+
//close connection on error
71+
await tcpConnectionFactory.Release(currentConnection, true);
72+
currentConnection = null;
73+
}
7374
}
7475
}
7576

@@ -78,11 +79,13 @@ internal class RetryResult
7879
internal bool IsSuccess => Exception == null;
7980
internal TcpServerConnection LatestConnection { get; }
8081
internal Exception Exception { get; }
82+
internal bool Continue { get; }
8183

82-
internal RetryResult(TcpServerConnection lastConnection, Exception exception)
84+
internal RetryResult(TcpServerConnection lastConnection, Exception exception, bool @continue)
8385
{
8486
LatestConnection = lastConnection;
8587
Exception = exception;
88+
Continue = @continue;
8689
}
8790
}
8891
}

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ await handleWebSocketUpgrade(httpCmd, args, request,
220220
response, clientStream, clientStreamWriter,
221221
serverConnection, cancellationTokenSource, cancellationToken);
222222
closeServerConnection = true;
223-
return;
223+
return false;
224224
}
225225

226226
// construct the web request that we are going to issue on behalf of the client.
227227
await handleHttpSessionRequestInternal(serverConnection, args);
228+
return true;
228229

229230
}, generator, connection);
230231

@@ -237,6 +238,11 @@ await handleWebSocketUpgrade(httpCmd, args, request,
237238
throw result.Exception;
238239
}
239240

241+
if(!result.Continue)
242+
{
243+
return;
244+
}
245+
240246
//user requested
241247
if (args.WebSession.CloseServerConnection)
242248
{

0 commit comments

Comments
 (0)