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

Commit ebc25f5

Browse files
committed
optional prefetch
1 parent b6bdf04 commit ebc25f5

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class ProxyTestController
2424
public ProxyTestController()
2525
{
2626
proxyServer = new ProxyServer();
27-
proxyServer.EnableConnectionPool = true;
2827
// generate root certificate without storing it in file system
2928
//proxyServer.CertificateManager.CreateRootCertificate(false);
3029

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,14 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.WebSession.Response,
152152

153153
SslStream sslStream = null;
154154

155-
//don't pass cancellation token here
156-
//it could cause floating server connections when client exits
157-
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(this, connectArgs,
158-
isConnect: true, applicationProtocols: null, noCache: false,
159-
cancellationToken: CancellationToken.None);
155+
if (EnableTcpServerConnectionPrefetch)
156+
{
157+
//don't pass cancellation token here
158+
//it could cause floating server connections when client exits
159+
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(this, connectArgs,
160+
isConnect: true, applicationProtocols: null, noCache: false,
161+
cancellationToken: CancellationToken.None);
162+
}
160163

161164
try
162165
{
@@ -204,10 +207,10 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.WebSession.Response,
204207
decryptSsl = false;
205208
}
206209

207-
if(!decryptSsl)
210+
if (!decryptSsl)
208211
{
209212
await tcpConnectionFactory.Release(prefetchConnectionTask, true);
210-
prefetchConnectionTask = null;
213+
prefetchConnectionTask = null;
211214
}
212215
}
213216

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,19 @@ public ProxyServer(string rootCertificateName, string rootCertificateIssuerName,
161161

162162
/// <summary>
163163
/// Should we enable experimental server connection pool?
164-
/// Defaults to disable.
164+
/// Defaults to true.
165165
/// </summary>
166-
public bool EnableConnectionPool { get; set; }
166+
public bool EnableConnectionPool { get; set; } = true;
167+
168+
/// <summary>
169+
/// Should we enable tcp server connection prefetching?
170+
/// When enabled, as soon as we receive a client connection we concurrently initiate
171+
/// corresponding server connection process using CONNECT hostname or SNI hostname on a separate task so that after parsing client request
172+
/// we will have the server connection immediately ready or in the process of getting ready.
173+
/// If a server connection is available in cache then this prefetch task will immediatly return with the available connection from cache.
174+
/// Defaults to true.
175+
/// </summary>
176+
public bool EnableTcpServerConnectionPrefetch { get; set; } = true;
167177

168178
/// <summary>
169179
/// Buffer size in bytes used throughout this proxy.
@@ -186,14 +196,14 @@ public ProxyServer(string rootCertificateName, string rootCertificateIssuerName,
186196
public int MaxCachedConnections { get; set; } = 2;
187197

188198
/// <summary>
189-
/// Number of seconds to linger when Tcp connection is in TIME_WAIT state.
190-
/// Default value is 30.
199+
/// Number of seconds to linger when Tcp connection is in TIME_WAIT state.
200+
/// Default value is 30.
191201
/// </summary>
192202
public int TcpTimeWaitSeconds { get; set; } = 30;
193203

194204
/// <summary>
195-
/// Should we reuse client/server tcp sockets.
196-
/// Default is true (disabled for linux/macOS due to bug in .Net core).
205+
/// Should we reuse client/server tcp sockets.
206+
/// Default is true (disabled for linux/macOS due to bug in .Net core).
197207
/// </summary>
198208
public bool ReuseSocket { get; set; } = true;
199209

src/Titanium.Web.Proxy/TransparentClientHandler.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ private async Task handleClient(TransparentProxyEndPoint endPoint, TcpClientConn
6363

6464
if (endPoint.DecryptSsl && args.DecryptSsl)
6565
{
66-
//don't pass cancellation token here
67-
//it could cause floating server connections when client exits
68-
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(httpsHostName, endPoint.Port,
69-
httpVersion: null, isHttps: true, applicationProtocols: null, isConnect: false,
70-
proxyServer: this, upStreamEndPoint: UpStreamEndPoint, externalProxy: UpStreamHttpsProxy,
71-
noCache: false, cancellationToken: CancellationToken.None);
72-
66+
if(EnableTcpServerConnectionPrefetch)
67+
{
68+
//don't pass cancellation token here
69+
//it could cause floating server connections when client exits
70+
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(httpsHostName, endPoint.Port,
71+
httpVersion: null, isHttps: true, applicationProtocols: null, isConnect: false,
72+
proxyServer: this, upStreamEndPoint: UpStreamEndPoint, externalProxy: UpStreamHttpsProxy,
73+
noCache: false, cancellationToken: CancellationToken.None);
74+
}
75+
7376
SslStream sslStream = null;
7477

7578
//do client authentication using fake certificate

0 commit comments

Comments
 (0)