Skip to content

Commit a7548b0

Browse files
committed
Modified functionality of RestClient to make optimal use of connection pooling
1 parent bd94fee commit a7548b0

File tree

10 files changed

+371
-515
lines changed

10 files changed

+371
-515
lines changed

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/core/Authorize.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public class Authorize
1616
private readonly MerchantConfig _merchantConfig;
1717
LogUtility logUtility;
1818

19-
2019
public Authorize(MerchantConfig merchantConfig)
2120
{
2221
_merchantConfig = merchantConfig;
2322
Enumerations.ValidateRequestType(_merchantConfig.RequestType);
2423
Enumerations.SetRequestType(_merchantConfig);
2524
logUtility = new LogUtility();
25+
2626
if (_logger == null)
2727
{
2828
_logger = LogManager.GetCurrentClassLogger();
@@ -65,10 +65,10 @@ public HttpToken GetSignature()
6565

6666
if (_merchantConfig.IsPostRequest || _merchantConfig.IsPutRequest || _merchantConfig.IsPatchRequest)
6767
{
68-
logUtility.LogDebugMessage( _logger, $"digest: {signatureObj.Digest}");
68+
logUtility.LogDebugMessage(_logger, $"digest: {signatureObj.Digest}");
6969
}
7070

71-
logUtility.LogDebugMessage( _logger, $"Signature : {signatureObj.SignatureParam}");
71+
logUtility.LogDebugMessage(_logger, $"Signature : {signatureObj.SignatureParam}");
7272

7373
return signatureObj;
7474
}

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/core/MerchantConfig.cs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace AuthenticationSdk.core
1818
*============================================================================================*/
1919
public class MerchantConfig
2020
{
21-
public MerchantConfig(IReadOnlyDictionary<string, string> merchantConfigDictionary = null, Dictionary<string, bool> mapToControlMLEonAPI = null)
21+
public MerchantConfig(IReadOnlyDictionary<string, string> merchantConfigDictionary = null, Dictionary<string,bool> mapToControlMLEonAPI=null)
2222
{
2323
var _propertiesSetUsing = string.Empty;
2424

@@ -170,6 +170,10 @@ public MerchantConfig(IReadOnlyDictionary<string, string> merchantConfigDictiona
170170

171171
public string MleKeyAlias { get; set; }
172172

173+
public string MaxConnectionPoolSize { get; set; }
174+
175+
public string KeepAliveTime { get; set; }
176+
173177
#endregion
174178

175179
public void LogMerchantConfigurationProperties()
@@ -252,9 +256,27 @@ private void SetValuesFromAppConfig(NameValueCollection merchantConfigSection, D
252256
{
253257
MleKeyAlias = Constants.DefaultMleAliasForCert;
254258
}
259+
260+
if (merchantConfigSection["maxConnectionPoolSize"] != null)
261+
{
262+
MaxConnectionPoolSize = merchantConfigSection["maxConnectionPoolSize"];
263+
}
264+
else
265+
{
266+
MaxConnectionPoolSize = Constants.DefaultMaxConnectionPoolSize;
267+
}
268+
269+
if (merchantConfigSection["keepAliveTime"] != null)
270+
{
271+
KeepAliveTime = merchantConfigSection["keepAliveTime"];
272+
}
273+
else
274+
{
275+
KeepAliveTime = Constants.DefaultKeepAliveTime;
276+
}
255277
}
256278

257-
private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantConfigDictionary, Dictionary<string, bool> mapToControlMLEonAPI)
279+
private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantConfigDictionary, Dictionary<string,bool> mapToControlMLEonAPI)
258280
{
259281
var key = string.Empty;
260282

@@ -278,6 +300,7 @@ private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantC
278300
UseMetaKey = "false";
279301
}
280302
}
303+
281304
key = "intermediateHost";
282305
if (merchantConfigDictionary.ContainsKey(key))
283306
{
@@ -464,7 +487,7 @@ private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantC
464487
PemFileDirectory = merchantConfigDictionary["pemFileDirectory"];
465488
}
466489

467-
if (merchantConfigDictionary.ContainsKey("useMLEGlobally") && "true".Equals(merchantConfigDictionary["useMLEGlobally"], StringComparison.OrdinalIgnoreCase))
490+
if (merchantConfigDictionary.ContainsKey("useMLEGlobally") && "true".Equals(merchantConfigDictionary["useMLEGlobally"],StringComparison.OrdinalIgnoreCase))
468491
{
469492
UseMLEGlobally = bool.Parse(merchantConfigDictionary["useMLEGlobally"]);
470493
}
@@ -488,6 +511,24 @@ private void SetValuesUsingDictObj(IReadOnlyDictionary<string, string> merchantC
488511
{
489512
MleKeyAlias = Constants.DefaultMleAliasForCert;
490513
}
514+
515+
if (merchantConfigDictionary.ContainsKey("maxConnectionPoolSize"))
516+
{
517+
MaxConnectionPoolSize = merchantConfigDictionary["maxConnectionPoolSize"];
518+
}
519+
else
520+
{
521+
MaxConnectionPoolSize = Constants.DefaultMaxConnectionPoolSize;
522+
}
523+
524+
if (merchantConfigDictionary.ContainsKey("keepAliveTime"))
525+
{
526+
KeepAliveTime = merchantConfigDictionary["keepAliveTime"];
527+
}
528+
else
529+
{
530+
KeepAliveTime = Constants.DefaultKeepAliveTime;
531+
}
491532
}
492533
}
493534
catch (KeyNotFoundException err)

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/util/Cache.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static X509Certificate2Collection FetchCachedCertificate(string p12FilePa
4747
var cachedFilePath = Path.GetFullPath(p12FilePath);
4848
filePaths.Add(cachedFilePath);
4949
policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
50+
5051
var certificates = new X509Certificate2Collection();
5152
certificates.Import(p12FilePath, keyPassword, X509KeyStorageFlags.PersistKeySet);
5253

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/util/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@ public static class Constants
4141
public static readonly string LOG_REQUEST_BEFORE_MLE = "LOG_REQUEST_BEFORE_MLE: ";
4242

4343
public static readonly string LOG_REQUEST_AFTER_MLE = "LOG_REQUEST_AFTER_MLE: ";
44+
45+
public static readonly string DefaultMaxConnectionPoolSize = $"{int.MaxValue}";
46+
47+
// https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?view=net-9.0
48+
public static readonly string DefaultKeepAliveTime = "300000"; // Time in milliseconds
4449
}
4550
}

cybersource-rest-auth-netstandard/AuthenticationSdk/AuthenticationSdk/util/LogUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public string MaskSensitiveData(string str)
8585
catch (Exception e)
8686
{
8787
throw e;
88-
}
88+
}
8989
}
9090

9191
return str;

0 commit comments

Comments
 (0)