Skip to content

Commit b3020b5

Browse files
committed
Added options to MongoClient settings
1 parent 6696184 commit b3020b5

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

src/MongoDB.Driver/MongoClientSettings.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public class MongoClientSettings : IEquatable<MongoClientSettings>, IInheritable
5959
private TimeSpan _maxConnectionLifeTime;
6060
private int _maxConnectionPoolSize;
6161
private int _minConnectionPoolSize;
62+
private string _proxyHost;
63+
private int? _proxyPort;
64+
private string _proxyUsername;
65+
private string _proxyPassword;
6266
private ReadConcern _readConcern;
6367
private UTF8Encoding _readEncoding;
6468
private ReadPreference _readPreference;
@@ -110,6 +114,10 @@ public MongoClientSettings()
110114
_maxConnectionLifeTime = MongoDefaults.MaxConnectionLifeTime;
111115
_maxConnectionPoolSize = MongoDefaults.MaxConnectionPoolSize;
112116
_minConnectionPoolSize = MongoDefaults.MinConnectionPoolSize;
117+
_proxyHost = null;
118+
_proxyPort = null;
119+
_proxyUsername = null;
120+
_proxyPassword = null;
113121
_readConcern = ReadConcern.Default;
114122
_readEncoding = null;
115123
_readPreference = ReadPreference.Primary;
@@ -428,6 +436,64 @@ public int MinConnectionPoolSize
428436
}
429437
}
430438

439+
/// <summary>
440+
/// Gets or sets the proxy host.
441+
/// </summary>
442+
public string ProxyHost
443+
{
444+
get => _proxyHost;
445+
set
446+
{
447+
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
448+
_proxyHost = Ensure.IsNotNullOrEmpty(value, nameof(ProxyHost));
449+
}
450+
}
451+
452+
/// <summary>
453+
/// Gets or sets the proxy port.
454+
/// </summary>
455+
public int? ProxyPort
456+
{
457+
get => _proxyPort;
458+
set
459+
{
460+
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
461+
462+
if (value is < 0 or > 65535)
463+
{
464+
throw new MongoConfigurationException("ProxyPort must be between 0 and 65535.");
465+
}
466+
467+
_proxyPort = value;
468+
}
469+
}
470+
471+
/// <summary>
472+
/// Gets or sets the proxy username.
473+
/// </summary>
474+
public string ProxyUsername
475+
{
476+
get => _proxyUsername;
477+
set
478+
{
479+
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
480+
_proxyUsername = Ensure.IsNotNullOrEmpty(value, nameof(ProxyUsername));
481+
}
482+
}
483+
484+
/// <summary>
485+
/// Gets or sets the proxy password.
486+
/// </summary>
487+
public string ProxyPassword
488+
{
489+
get => _proxyPassword;
490+
set
491+
{
492+
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
493+
_proxyPassword = Ensure.IsNotNullOrEmpty(value, nameof(ProxyPassword));
494+
}
495+
}
496+
431497
/// <summary>
432498
/// Gets or sets the read concern.
433499
/// </summary>
@@ -863,6 +929,10 @@ public static MongoClientSettings FromUrl(MongoUrl url)
863929
clientSettings.MaxConnectionLifeTime = url.MaxConnectionLifeTime;
864930
clientSettings.MaxConnectionPoolSize = ConnectionStringConversions.GetEffectiveMaxConnections(url.MaxConnectionPoolSize);
865931
clientSettings.MinConnectionPoolSize = url.MinConnectionPoolSize;
932+
clientSettings.ProxyHost = url.ProxyHost;
933+
clientSettings.ProxyPort = url.ProxyPort;
934+
clientSettings.ProxyUsername = url.ProxyUsername;
935+
clientSettings.ProxyPassword = url.ProxyPassword;
866936
clientSettings.ReadConcern = new ReadConcern(url.ReadConcernLevel);
867937
clientSettings.ReadEncoding = null; // ReadEncoding must be provided in code
868938
clientSettings.ReadPreference = (url.ReadPreference == null) ? ReadPreference.Primary : url.ReadPreference;
@@ -920,6 +990,10 @@ public MongoClientSettings Clone()
920990
clone._maxConnectionLifeTime = _maxConnectionLifeTime;
921991
clone._maxConnectionPoolSize = _maxConnectionPoolSize;
922992
clone._minConnectionPoolSize = _minConnectionPoolSize;
993+
clone._proxyHost = _proxyHost;
994+
clone._proxyPort = _proxyPort;
995+
clone._proxyUsername = _proxyUsername;
996+
clone._proxyPassword = _proxyPassword;
923997
clone._readConcern = _readConcern;
924998
clone._readEncoding = _readEncoding;
925999
clone._readPreference = _readPreference;
@@ -989,6 +1063,10 @@ public override bool Equals(object obj)
9891063
_maxConnectionLifeTime == rhs._maxConnectionLifeTime &&
9901064
_maxConnectionPoolSize == rhs._maxConnectionPoolSize &&
9911065
_minConnectionPoolSize == rhs._minConnectionPoolSize &&
1066+
_proxyHost == rhs._proxyHost &&
1067+
_proxyPort == rhs._proxyPort &&
1068+
_proxyUsername == rhs._proxyUsername &&
1069+
_proxyPassword == rhs._proxyPassword &&
9921070
object.Equals(_readEncoding, rhs._readEncoding) &&
9931071
object.Equals(_readConcern, rhs._readConcern) &&
9941072
object.Equals(_readPreference, rhs._readPreference) &&
@@ -1076,6 +1154,10 @@ public override int GetHashCode()
10761154
.Hash(_maxConnectionLifeTime)
10771155
.Hash(_maxConnectionPoolSize)
10781156
.Hash(_minConnectionPoolSize)
1157+
.Hash(_proxyHost)
1158+
.Hash(_proxyPort)
1159+
.Hash(_proxyUsername)
1160+
.Hash(_proxyPassword)
10791161
.Hash(_readConcern)
10801162
.Hash(_readEncoding)
10811163
.Hash(_readPreference)
@@ -1145,6 +1227,22 @@ public override string ToString()
11451227
sb.AppendFormat("MaxConnectionLifeTime={0};", _maxConnectionLifeTime);
11461228
sb.AppendFormat("MaxConnectionPoolSize={0};", _maxConnectionPoolSize);
11471229
sb.AppendFormat("MinConnectionPoolSize={0};", _minConnectionPoolSize);
1230+
if (_proxyHost != null)
1231+
{
1232+
sb.AppendFormat("ProxyHost={0};", _proxyHost);
1233+
}
1234+
if (_proxyPort != null)
1235+
{
1236+
sb.AppendFormat("ProxyPort={0};", _proxyPort.Value);
1237+
}
1238+
if (_proxyUsername != null)
1239+
{
1240+
sb.AppendFormat("ProxyUsername={0};", _proxyUsername);
1241+
}
1242+
if (_proxyPassword != null)
1243+
{
1244+
sb.AppendFormat("ProxyPassword={0};", _proxyPassword);
1245+
}
11481246
if (_readEncoding != null)
11491247
{
11501248
sb.Append("ReadEncoding=UTF8Encoding;");
@@ -1297,6 +1395,29 @@ private void ThrowIfSettingsAreInvalid()
12971395
throw new InvalidOperationException("Load balanced mode cannot be used with direct connection.");
12981396
}
12991397
}
1398+
1399+
if (string.IsNullOrEmpty(_proxyHost))
1400+
{
1401+
if (_proxyPort is not null)
1402+
{
1403+
throw new InvalidOperationException("ProxyPort cannot be specified without ProxyHost.");
1404+
}
1405+
1406+
if (!string.IsNullOrEmpty(_proxyUsername))
1407+
{
1408+
throw new InvalidOperationException("ProxyUsername cannot be specified without ProxyHost.");
1409+
}
1410+
1411+
if (!string.IsNullOrEmpty(_proxyPassword))
1412+
{
1413+
throw new InvalidOperationException("ProxyPassword cannot be specified without ProxyHost.");
1414+
}
1415+
}
1416+
1417+
if (string.IsNullOrEmpty(_proxyUsername) != string.IsNullOrEmpty(_proxyPassword))
1418+
{
1419+
throw new InvalidOperationException("ProxyUsername and ProxyPassword must both be specified or neither.");
1420+
}
13001421
}
13011422
}
13021423
}

0 commit comments

Comments
 (0)