Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
d90b6ad0
Commit
d90b6ad0
authored
May 16, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor retry
parent
0d313e0e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
89 additions
and
28 deletions
+89
-28
RetryPolicy.cs
Titanium.Web.Proxy/Network/RetryPolicy.cs
+77
-0
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+3
-17
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+9
-11
No files found.
Titanium.Web.Proxy/Network/RetryPolicy.cs
0 → 100644
View file @
d90b6ad0
using
Polly
;
using
System
;
using
System.Collections.Generic
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.Network.Tcp
;
namespace
Titanium.Web.Proxy.Network
{
internal
class
RetryPolicy
<
T
>
where
T
:
Exception
{
private
readonly
int
retries
;
private
readonly
TcpConnectionFactory
tcpConnectionFactory
;
internal
RetryPolicy
(
int
retries
,
TcpConnectionFactory
tcpConnectionFactory
)
{
this
.
retries
=
retries
;
this
.
tcpConnectionFactory
=
tcpConnectionFactory
;
}
//get the policy
private
Policy
getRetryPolicy
()
{
return
Policy
.
Handle
<
T
>()
.
RetryAsync
(
retries
,
onRetryAsync
:
async
(
ex
,
i
,
context
)
=>
{
if
(
context
[
"connection"
]
!=
null
)
{
//close connection on error
var
connection
=
(
TcpServerConnection
)
context
[
"connection"
];
await
tcpConnectionFactory
.
Release
(
connection
,
true
);
context
[
"connection"
]
=
null
;
}
});
}
/// <summary>
/// Execute and retry the given action until retry number of times.
/// </summary>
/// <param name="action">The action to 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>
/// <returns></returns>
internal
Task
ExecuteAsync
(
Func
<
TcpServerConnection
,
Task
>
action
,
Func
<
Task
<
TcpServerConnection
>>
generator
,
ref
TcpServerConnection
initialConnection
)
{
var
outerContext
=
new
Dictionary
<
string
,
object
>
{
{
"connection"
,
initialConnection
}
};
Task
result
;
try
{
result
=
getRetryPolicy
().
ExecuteAsync
(
async
(
context
)
=>
{
//setup connection
var
connection
=
context
[
"connection"
]
as
TcpServerConnection
??
await
generator
();
context
[
"connection"
]
=
connection
;
//retry
await
action
(
connection
);
},
outerContext
);
}
//all retries failed
catch
{
//update the original connection to last used connection
initialConnection
=
outerContext
[
"connection"
]
as
TcpServerConnection
;
throw
;
}
return
result
;
}
}
}
Titanium.Web.Proxy/ProxyServer.cs
View file @
d90b6ad0
...
@@ -7,7 +7,6 @@ using System.Security.Authentication;
...
@@ -7,7 +7,6 @@ using System.Security.Authentication;
using
System.Security.Cryptography.X509Certificates
;
using
System.Security.Cryptography.X509Certificates
;
using
System.Threading
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Polly
;
using
StreamExtended
;
using
StreamExtended
;
using
StreamExtended.Network
;
using
StreamExtended.Network
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.EventArguments
;
...
@@ -125,7 +124,7 @@ namespace Titanium.Web.Proxy
...
@@ -125,7 +124,7 @@ namespace Titanium.Web.Proxy
private
SystemProxyManager
systemProxySettingsManager
{
get
;
}
private
SystemProxyManager
systemProxySettingsManager
{
get
;
}
//Number of exception retries when connection pool is enabled.
//Number of exception retries when connection pool is enabled.
private
int
retries
=>
EnableConnectionPool
?
MaxCachedConnections
+
1
:
0
;
private
int
retries
=>
EnableConnectionPool
?
MaxCachedConnections
:
0
;
/// <summary>
/// <summary>
/// Is the proxy currently running?
/// Is the proxy currently running?
...
@@ -809,22 +808,9 @@ namespace Titanium.Web.Proxy
...
@@ -809,22 +808,9 @@ namespace Titanium.Web.Proxy
/// <summary>
/// <summary>
/// Connection retry policy when using connection pool.
/// Connection retry policy when using connection pool.
/// </summary>
/// </summary>
private
Policy
retryPolicy
<
T
>()
where
T
:
Exception
private
RetryPolicy
<
T
>
retryPolicy
<
T
>()
where
T
:
Exception
{
{
return
Policy
.
Handle
<
T
>()
return
new
RetryPolicy
<
T
>(
retries
,
tcpConnectionFactory
);
.
RetryAsync
(
retries
,
onRetryAsync
:
async
(
ex
,
i
,
context
)
=>
{
if
(
context
[
"connection"
]
!=
null
)
{
//close connection on error
var
connection
=
(
TcpServerConnection
)
context
[
"connection"
];
await
tcpConnectionFactory
.
Release
(
connection
,
true
);
context
[
"connection"
]
=
null
;
}
});
}
}
}
}
}
}
Titanium.Web.Proxy/RequestHandler.cs
View file @
d90b6ad0
...
@@ -204,30 +204,28 @@ namespace Titanium.Web.Proxy
...
@@ -204,30 +204,28 @@ namespace Titanium.Web.Proxy
connection
=
null
;
connection
=
null
;
}
}
//for connection pool retry fails until cache is exhausted
//a connection generator task with captured parameters via closure.
await
retryPolicy
<
ServerConnectionException
>().
ExecuteAsync
(
async
(
context
)
=>
Func
<
Task
<
TcpServerConnection
>>
generator
=
()
=>
getServerConnection
(
args
,
false
,
{
connection
=
context
[
"connection"
]
as
TcpServerConnection
??
await
getServerConnection
(
args
,
false
,
clientConnection
.
NegotiatedApplicationProtocol
,
clientConnection
.
NegotiatedApplicationProtocol
,
false
,
cancellationToken
);
false
,
cancellationToken
);
context
[
"connection"
]
=
connection
;
//for connection pool, retry fails until cache is exhausted.
await
retryPolicy
<
ServerConnectionException
>().
ExecuteAsync
(
async
(
serverConnection
)
=>
{
// if upgrading to websocket then relay the request without reading the contents
// if upgrading to websocket then relay the request without reading the contents
if
(
request
.
UpgradeToWebSocket
)
if
(
request
.
UpgradeToWebSocket
)
{
{
await
handleWebSocketUpgrade
(
httpCmd
,
args
,
request
,
await
handleWebSocketUpgrade
(
httpCmd
,
args
,
request
,
response
,
clientStream
,
clientStreamWriter
,
response
,
clientStream
,
clientStreamWriter
,
c
onnection
,
cancellationTokenSource
,
cancellationToken
);
serverC
onnection
,
cancellationTokenSource
,
cancellationToken
);
closeServerConnection
=
true
;
closeServerConnection
=
true
;
return
;
return
;
}
}
// 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
(
c
onnection
,
args
);
await
handleHttpSessionRequestInternal
(
serverC
onnection
,
args
);
},
new
Dictionary
<
string
,
object
>
{
{
"connection"
,
connection
}
}
);
},
generator
,
ref
connection
);
//user requested
//user requested
if
(
args
.
WebSession
.
CloseServerConnection
)
if
(
args
.
WebSession
.
CloseServerConnection
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment