Skip to content

Commit 39ae59f

Browse files
Improve scalability when using gxhttpclient (#972)
* Refactored gxhttpclient implementation to utilize a singleton instance for reusing the connection pool of httpclient instances. Avoided using CookieContainer of handlers, as they are shared across the pooled connections. Instead, managed sending and receiving cookies as headers. * Change public method by internal. * Honor property Maximum pool size per route. * Define const for DEFAULT_HTTPCLIENT_MAX_PER_ROUTE. * Rename SINGLETON_HTTPCLIENT to HTTPCLIENT_SINGLETON to maintain consistency with other keys, such as HTTPCLIENT_MAX_PER_ROUTE * Add trace for HttpClient. Ignore cookies in container that are invalid for the current url. * Avoid InvalidOperationException in HttpRequest.GetValue when there is no HasFormContentType. Use async operations when possible for httpclient connectionProperties was not caching empty values * Prepare for async calls to httpclient. Enable Microsoft.VisualStudio.Threading.Analyzers * Fix build error. * Revert changes related to async and keep only the httpclient singleton changes in this branch. This reverts commit c855419. Revert "Prepare for async calls to httpclient." This reverts commit cd6c42e. Revert "Avoid InvalidOperationException in HttpRequest.GetValue when there is no HasFormContentType." This reverts commit 4f77cb3. * Consider SslOptions when creating the key for SocketsHttpHandler, as the ServicePointManager.ServerCertificateValidationCallback may change after the first handler has been created. Therefore, subsequent calls cannot reuse the same handler. * Add ToStringAsync and ExecuteAsync methods to HttpClient. * ExecuteAsync must be public as it is used from generated code. * Remove unnecessary call to ReceiveData (sync method) in ToStringAsync
1 parent e94c5ec commit 39ae59f

File tree

4 files changed

+796
-143
lines changed

4 files changed

+796
-143
lines changed

dotnet/src/dotnetframework/GxClasses/Core/gxconfig.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ public class Preferences
848848
const string DEFAULT_DS = "Default";
849849
static int httpclient_max_per_route = -1;
850850
static int sessionTimeout = -1;
851+
static int singletonHttpClient = -1;
851852
internal static string AppMainNamespace
852853
{
853854
get
@@ -1443,6 +1444,18 @@ internal static bool WrapSingleApiOutput
14431444
return DefaultWrapSingleApiOutput;
14441445
}
14451446
}
1447+
1448+
internal static bool SingletonHttpClient()
1449+
{
1450+
if (singletonHttpClient == -1)
1451+
{
1452+
if (Config.GetValueOrEnvironmentVarOf("HTTPCLIENT_SINGLETON", out string sValue) && int.TryParse(sValue, out int value))
1453+
singletonHttpClient = value;
1454+
else
1455+
singletonHttpClient = 1;
1456+
}
1457+
return singletonHttpClient==1;
1458+
}
14461459
internal static string CorsAllowedOrigins()
14471460
{
14481461
if (Config.GetValueOf("CORS_ALLOW_ORIGIN", out string corsOrigin))
@@ -1484,27 +1497,27 @@ public static string GetDefaultTheme()
14841497
else
14851498
return "";
14861499
}
1487-
1488-
public static int GetHttpClientMaxConnectionPerRoute()
1500+
internal const int DEFAULT_HTTPCLIENT_MAX_PER_ROUTE= 1000;
1501+
internal static int GetHttpClientMaxConnectionPerRoute()
14891502
{
14901503
if (httpclient_max_per_route == -1)
14911504
{
14921505
try
14931506
{
14941507
string strmax;
1495-
if (Config.GetValueOf("HTTPCLIENT_MAX_PER_ROUTE", out strmax))
1508+
if (Config.GetValueOrEnvironmentVarOf("HTTPCLIENT_MAX_PER_ROUTE", out strmax))
14961509
{
14971510
httpclient_max_per_route = Convert.ToInt32(strmax);
14981511
}
14991512
else
15001513
{
1501-
httpclient_max_per_route = 1000;
1514+
httpclient_max_per_route = DEFAULT_HTTPCLIENT_MAX_PER_ROUTE;
15021515
}
15031516
}
15041517
catch (Exception ex)
15051518
{
15061519
GXLogging.Error(log, "HttpClientMaxPerRoute error", ex);
1507-
httpclient_max_per_route = 1000;
1520+
httpclient_max_per_route = DEFAULT_HTTPCLIENT_MAX_PER_ROUTE;
15081521
}
15091522
}
15101523
return httpclient_max_per_route;

0 commit comments

Comments
 (0)