Skip to content

Commit 207a824

Browse files
author
rstam
committed
CSHARP-663: Move caching of proxy objects to MongoServerProxyFactory.
1 parent f2e4a38 commit 207a824

24 files changed

+636
-359
lines changed

MongoDB.Driver/Communication/MongoConnectionPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class MongoConnectionPool
2727
{
2828
// private fields
2929
private object _connectionPoolLock = new object();
30-
private MongoServerSettings _settings;
30+
private MongoServerProxySettings _settings;
3131
private MongoServerInstance _serverInstance;
3232
private int _poolSize;
3333
private List<MongoConnection> _availableConnections = new List<MongoConnection>();

MongoDB.Driver/Communication/MongoServerInstance.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public sealed class MongoServerInstance
4242

4343
// private fields
4444
private readonly object _serverInstanceLock = new object();
45-
private readonly MongoServerSettings _settings;
45+
private readonly MongoServerProxySettings _settings;
4646
private readonly MongoConnectionPool _connectionPool;
4747
private readonly PingTimeAggregator _pingTimeAggregator;
4848
private MongoServerAddress _address;
@@ -62,7 +62,7 @@ public sealed class MongoServerInstance
6262
/// </summary>
6363
/// <param name="settings">The settings.</param>
6464
/// <param name="address">The address.</param>
65-
internal MongoServerInstance(MongoServerSettings settings, MongoServerAddress address)
65+
internal MongoServerInstance(MongoServerProxySettings settings, MongoServerAddress address)
6666
{
6767
_settings = settings;
6868
_address = address;
@@ -292,7 +292,7 @@ public int SequentialId
292292
/// <summary>
293293
/// Gets the server for this server instance.
294294
/// </summary>
295-
public MongoServerSettings Settings
295+
public MongoServerProxySettings Settings
296296
{
297297
get { return _settings; }
298298
}

MongoDB.Driver/Communication/Proxies/DirectMongoServerProxy.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,35 @@ internal sealed class DirectMongoServerProxy : IMongoServerProxy
2727
{
2828
// private fields
2929
private readonly object _stateLock = new object();
30-
private readonly MongoServerSettings _settings;
30+
private readonly int _sequentialId;
31+
private readonly MongoServerProxySettings _settings;
3132
private readonly MongoServerInstance _instance;
33+
3234
private int _connectionAttempt;
3335

3436
// constructors
3537
/// <summary>
3638
/// Initializes a new instance of the <see cref="DirectMongoServerProxy"/> class.
3739
/// </summary>
40+
/// <param name="sequentialId">The sequential id.</param>
3841
/// <param name="settings">The settings.</param>
39-
public DirectMongoServerProxy(MongoServerSettings settings)
42+
public DirectMongoServerProxy(int sequentialId, MongoServerProxySettings settings)
4043
{
44+
_sequentialId = sequentialId;
4145
_settings = settings;
4246
_instance = new MongoServerInstance(settings, settings.Servers.First());
4347
}
4448

4549
/// <summary>
4650
/// Initializes a new instance of the <see cref="DirectMongoServerProxy"/> class.
4751
/// </summary>
52+
/// <param name="sequentialId">The sequential id.</param>
4853
/// <param name="serverSettings">The server settings.</param>
4954
/// <param name="instance">The instance.</param>
5055
/// <param name="connectionAttempt">The connection attempt.</param>
51-
public DirectMongoServerProxy(MongoServerSettings serverSettings, MongoServerInstance instance, int connectionAttempt)
56+
public DirectMongoServerProxy(int sequentialId, MongoServerProxySettings serverSettings, MongoServerInstance instance, int connectionAttempt)
5257
{
58+
_sequentialId = sequentialId;
5359
_settings = serverSettings;
5460
_instance = instance;
5561
_connectionAttempt = connectionAttempt;
@@ -86,6 +92,14 @@ public ReadOnlyCollection<MongoServerInstance> Instances
8692
get { return new List<MongoServerInstance> { _instance }.AsReadOnly(); }
8793
}
8894

95+
/// <summary>
96+
/// Gets the sequential id assigned to this proxy.
97+
/// </summary>
98+
public int SequentialId
99+
{
100+
get { return _sequentialId; }
101+
}
102+
89103
/// <summary>
90104
/// Gets the state.
91105
/// </summary>

MongoDB.Driver/Communication/Proxies/DiscoveringMongoServerProxy.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ namespace MongoDB.Driver.Internal
2525
internal sealed class DiscoveringMongoServerProxy : IMongoServerProxy
2626
{
2727
private readonly object _lock = new object();
28-
private readonly MongoServerSettings _settings;
28+
private readonly int _sequentialId;
29+
private readonly MongoServerProxySettings _settings;
2930
private readonly ReadOnlyCollection<MongoServerInstance> _instances;
3031

3132
// volatile will ensure that our reads are not reordered such one could get placed before a write. This
@@ -39,10 +40,12 @@ internal sealed class DiscoveringMongoServerProxy : IMongoServerProxy
3940
/// <summary>
4041
/// Initializes a new instance of the <see cref="DiscoveringMongoServerProxy"/> class.
4142
/// </summary>
43+
/// <param name="sequentialId">The sequential id.</param>
4244
/// <param name="settings">The settings.</param>
43-
public DiscoveringMongoServerProxy(MongoServerSettings settings)
45+
public DiscoveringMongoServerProxy(int sequentialId, MongoServerProxySettings settings)
4446
{
4547
_state = MongoServerState.Disconnected;
48+
_sequentialId = sequentialId;
4649
_settings = settings;
4750
_instances = settings.Servers.Select(a => new MongoServerInstance(settings, a)).ToList().AsReadOnly();
4851
}
@@ -96,6 +99,14 @@ public ReadOnlyCollection<MongoServerInstance> Instances
9699
}
97100
}
98101

102+
/// <summary>
103+
/// Gets the sequential id assigned to this proxy.
104+
/// </summary>
105+
public int SequentialId
106+
{
107+
get { return _sequentialId; }
108+
}
109+
99110
/// <summary>
100111
/// Gets the state.
101112
/// </summary>
@@ -246,11 +257,11 @@ private void CreateActualProxy(MongoServerInstance instance, BlockingQueue<Mongo
246257
{
247258
if (instance.InstanceType == MongoServerInstanceType.ReplicaSetMember)
248259
{
249-
_serverProxy = new ReplicaSetMongoServerProxy(_settings, _instances, connectionQueue, _connectionAttempt);
260+
_serverProxy = new ReplicaSetMongoServerProxy(_sequentialId, _settings, _instances, connectionQueue, _connectionAttempt);
250261
}
251262
else if (instance.InstanceType == MongoServerInstanceType.ShardRouter)
252263
{
253-
_serverProxy = new ShardedMongoServerProxy(_settings, _instances, connectionQueue, _connectionAttempt);
264+
_serverProxy = new ShardedMongoServerProxy(_sequentialId, _settings, _instances, connectionQueue, _connectionAttempt);
254265
}
255266
else if (instance.InstanceType == MongoServerInstanceType.StandAlone)
256267
{
@@ -260,7 +271,7 @@ private void CreateActualProxy(MongoServerInstance instance, BlockingQueue<Mongo
260271
otherInstance.Disconnect();
261272
}
262273

263-
_serverProxy = new DirectMongoServerProxy(_settings, instance, _connectionAttempt);
274+
_serverProxy = new DirectMongoServerProxy(_sequentialId, _settings, instance, _connectionAttempt);
264275
}
265276
else
266277
{

MongoDB.Driver/Communication/Proxies/IMongoServerProxy.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ internal interface IMongoServerProxy
3838
/// </summary>
3939
ReadOnlyCollection<MongoServerInstance> Instances { get; }
4040

41+
/// <summary>
42+
/// Gets the sequential id assigned to this proxy.
43+
/// </summary>
44+
int SequentialId { get; }
45+
4146
/// <summary>
4247
/// Gets the state.
4348
/// </summary>

MongoDB.Driver/Communication/Proxies/MongoServerProxyFactory.cs

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System.Collections.Generic;
1617
using System.Linq;
1718

1819
namespace MongoDB.Driver.Internal
@@ -22,13 +23,67 @@ namespace MongoDB.Driver.Internal
2223
/// </summary>
2324
internal class MongoServerProxyFactory
2425
{
26+
// private static fields
27+
private static readonly MongoServerProxyFactory __instance = new MongoServerProxyFactory();
28+
29+
// private fields
30+
private readonly object _lock = new object();
31+
private readonly Dictionary<MongoServerProxySettings, IMongoServerProxy> _proxies = new Dictionary<MongoServerProxySettings, IMongoServerProxy>();
32+
33+
private int _nextSequentialId = 1;
34+
35+
// public static properties
36+
/// <summary>
37+
/// Gets the default instance.
38+
/// </summary>
39+
/// <value>
40+
/// The default instance.
41+
/// </value>
42+
public static MongoServerProxyFactory Instance
43+
{
44+
get { return __instance; }
45+
}
46+
47+
// public properties
48+
/// <summary>
49+
/// Gets the proxy count.
50+
/// </summary>
51+
/// <value>
52+
/// The proxy count.
53+
/// </value>
54+
public int ProxyCount
55+
{
56+
get
57+
{
58+
lock (_lock)
59+
{
60+
return _proxies.Count;
61+
}
62+
}
63+
}
64+
2565
// public methods
2666
/// <summary>
27-
/// Creates an IMongoServerProxy of some type that depends on the server settings.
67+
/// Creates an IMongoServerProxy of some type that depends on the settings (or returns an existing one if one has already been created with these settings).
2868
/// </summary>
2969
/// <param name="settings">The settings.</param>
3070
/// <returns>An IMongoServerProxy.</returns>
31-
public IMongoServerProxy Create(MongoServerSettings settings)
71+
public IMongoServerProxy Create(MongoServerProxySettings settings)
72+
{
73+
lock (_lock)
74+
{
75+
IMongoServerProxy proxy;
76+
if (!_proxies.TryGetValue(settings, out proxy))
77+
{
78+
proxy = CreateInstance(_nextSequentialId++, settings);
79+
_proxies.Add(settings, proxy);
80+
}
81+
return proxy;
82+
}
83+
}
84+
85+
// private methods
86+
private IMongoServerProxy CreateInstance(int sequentialId, MongoServerProxySettings settings)
3287
{
3388
var connectionMode = settings.ConnectionMode;
3489
if (settings.ConnectionMode == ConnectionMode.Automatic)
@@ -46,13 +101,13 @@ public IMongoServerProxy Create(MongoServerSettings settings)
46101
switch (connectionMode)
47102
{
48103
case ConnectionMode.Direct:
49-
return new DirectMongoServerProxy(settings);
104+
return new DirectMongoServerProxy(sequentialId, settings);
50105
case ConnectionMode.ReplicaSet:
51-
return new ReplicaSetMongoServerProxy(settings);
106+
return new ReplicaSetMongoServerProxy(sequentialId, settings);
52107
case ConnectionMode.ShardRouter:
53-
return new ShardedMongoServerProxy(settings);
108+
return new ShardedMongoServerProxy(sequentialId, settings);
54109
default:
55-
return new DiscoveringMongoServerProxy(settings);
110+
return new DiscoveringMongoServerProxy(sequentialId, settings);
56111
}
57112
}
58113
}

0 commit comments

Comments
 (0)