Commit ebc25f50 authored by justcoding121's avatar justcoding121

optional prefetch

parent b6bdf046
...@@ -24,7 +24,6 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -24,7 +24,6 @@ namespace Titanium.Web.Proxy.Examples.Basic
public ProxyTestController() public ProxyTestController()
{ {
proxyServer = new ProxyServer(); proxyServer = new ProxyServer();
proxyServer.EnableConnectionPool = true;
// generate root certificate without storing it in file system // generate root certificate without storing it in file system
//proxyServer.CertificateManager.CreateRootCertificate(false); //proxyServer.CertificateManager.CreateRootCertificate(false);
......
...@@ -152,11 +152,14 @@ namespace Titanium.Web.Proxy ...@@ -152,11 +152,14 @@ namespace Titanium.Web.Proxy
SslStream sslStream = null; SslStream sslStream = null;
//don't pass cancellation token here if (EnableTcpServerConnectionPrefetch)
//it could cause floating server connections when client exits {
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(this, connectArgs, //don't pass cancellation token here
isConnect: true, applicationProtocols: null, noCache: false, //it could cause floating server connections when client exits
cancellationToken: CancellationToken.None); prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(this, connectArgs,
isConnect: true, applicationProtocols: null, noCache: false,
cancellationToken: CancellationToken.None);
}
try try
{ {
...@@ -204,10 +207,10 @@ namespace Titanium.Web.Proxy ...@@ -204,10 +207,10 @@ namespace Titanium.Web.Proxy
decryptSsl = false; decryptSsl = false;
} }
if(!decryptSsl) if (!decryptSsl)
{ {
await tcpConnectionFactory.Release(prefetchConnectionTask, true); await tcpConnectionFactory.Release(prefetchConnectionTask, true);
prefetchConnectionTask = null; prefetchConnectionTask = null;
} }
} }
......
...@@ -161,9 +161,19 @@ namespace Titanium.Web.Proxy ...@@ -161,9 +161,19 @@ namespace Titanium.Web.Proxy
/// <summary> /// <summary>
/// Should we enable experimental server connection pool? /// Should we enable experimental server connection pool?
/// Defaults to disable. /// Defaults to true.
/// </summary> /// </summary>
public bool EnableConnectionPool { get; set; } public bool EnableConnectionPool { get; set; } = true;
/// <summary>
/// Should we enable tcp server connection prefetching?
/// When enabled, as soon as we receive a client connection we concurrently initiate
/// corresponding server connection process using CONNECT hostname or SNI hostname on a separate task so that after parsing client request
/// we will have the server connection immediately ready or in the process of getting ready.
/// If a server connection is available in cache then this prefetch task will immediatly return with the available connection from cache.
/// Defaults to true.
/// </summary>
public bool EnableTcpServerConnectionPrefetch { get; set; } = true;
/// <summary> /// <summary>
/// Buffer size in bytes used throughout this proxy. /// Buffer size in bytes used throughout this proxy.
...@@ -186,14 +196,14 @@ namespace Titanium.Web.Proxy ...@@ -186,14 +196,14 @@ namespace Titanium.Web.Proxy
public int MaxCachedConnections { get; set; } = 2; public int MaxCachedConnections { get; set; } = 2;
/// <summary> /// <summary>
/// Number of seconds to linger when Tcp connection is in TIME_WAIT state. /// Number of seconds to linger when Tcp connection is in TIME_WAIT state.
/// Default value is 30. /// Default value is 30.
/// </summary> /// </summary>
public int TcpTimeWaitSeconds { get; set; } = 30; public int TcpTimeWaitSeconds { get; set; } = 30;
/// <summary> /// <summary>
/// Should we reuse client/server tcp sockets. /// Should we reuse client/server tcp sockets.
/// Default is true (disabled for linux/macOS due to bug in .Net core). /// Default is true (disabled for linux/macOS due to bug in .Net core).
/// </summary> /// </summary>
public bool ReuseSocket { get; set; } = true; public bool ReuseSocket { get; set; } = true;
......
...@@ -63,13 +63,16 @@ namespace Titanium.Web.Proxy ...@@ -63,13 +63,16 @@ namespace Titanium.Web.Proxy
if (endPoint.DecryptSsl && args.DecryptSsl) if (endPoint.DecryptSsl && args.DecryptSsl)
{ {
//don't pass cancellation token here if(EnableTcpServerConnectionPrefetch)
//it could cause floating server connections when client exits {
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(httpsHostName, endPoint.Port, //don't pass cancellation token here
httpVersion: null, isHttps: true, applicationProtocols: null, isConnect: false, //it could cause floating server connections when client exits
proxyServer: this, upStreamEndPoint: UpStreamEndPoint, externalProxy: UpStreamHttpsProxy, prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(httpsHostName, endPoint.Port,
noCache: false, cancellationToken: CancellationToken.None); httpVersion: null, isHttps: true, applicationProtocols: null, isConnect: false,
proxyServer: this, upStreamEndPoint: UpStreamEndPoint, externalProxy: UpStreamHttpsProxy,
noCache: false, cancellationToken: CancellationToken.None);
}
SslStream sslStream = null; SslStream sslStream = null;
//do client authentication using fake certificate //do client authentication using fake certificate
......
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