Skip to content

Commit f819fb5

Browse files
author
rstam
committed
Revert "CSHARP-663: Move caching of proxy objects to
MongoServerProxyFactory." This change has been rescheduled for version 2.0. This reverts commit 207a824.
1 parent 027eaf4 commit f819fb5

24 files changed

+359
-636
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 MongoServerProxySettings _settings;
30+
private MongoServerSettings _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 MongoServerProxySettings _settings;
45+
private readonly MongoServerSettings _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(MongoServerProxySettings settings, MongoServerAddress address)
65+
internal MongoServerInstance(MongoServerSettings 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 MongoServerProxySettings Settings
295+
public MongoServerSettings Settings
296296
{
297297
get { return _settings; }
298298
}

MongoDB.Driver/Communication/Proxies/DirectMongoServerProxy.cs

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

3634
// constructors
3735
/// <summary>
3836
/// Initializes a new instance of the <see cref="DirectMongoServerProxy"/> class.
3937
/// </summary>
40-
/// <param name="sequentialId">The sequential id.</param>
4138
/// <param name="settings">The settings.</param>
42-
public DirectMongoServerProxy(int sequentialId, MongoServerProxySettings settings)
39+
public DirectMongoServerProxy(MongoServerSettings settings)
4340
{
44-
_sequentialId = sequentialId;
4541
_settings = settings;
4642
_instance = new MongoServerInstance(settings, settings.Servers.First());
4743
}
4844

4945
/// <summary>
5046
/// Initializes a new instance of the <see cref="DirectMongoServerProxy"/> class.
5147
/// </summary>
52-
/// <param name="sequentialId">The sequential id.</param>
5348
/// <param name="serverSettings">The server settings.</param>
5449
/// <param name="instance">The instance.</param>
5550
/// <param name="connectionAttempt">The connection attempt.</param>
56-
public DirectMongoServerProxy(int sequentialId, MongoServerProxySettings serverSettings, MongoServerInstance instance, int connectionAttempt)
51+
public DirectMongoServerProxy(MongoServerSettings serverSettings, MongoServerInstance instance, int connectionAttempt)
5752
{
58-
_sequentialId = sequentialId;
5953
_settings = serverSettings;
6054
_instance = instance;
6155
_connectionAttempt = connectionAttempt;
@@ -92,14 +86,6 @@ public ReadOnlyCollection<MongoServerInstance> Instances
9286
get { return new List<MongoServerInstance> { _instance }.AsReadOnly(); }
9387
}
9488

95-
/// <summary>
96-
/// Gets the sequential id assigned to this proxy.
97-
/// </summary>
98-
public int SequentialId
99-
{
100-
get { return _sequentialId; }
101-
}
102-
10389
/// <summary>
10490
/// Gets the state.
10591
/// </summary>

MongoDB.Driver/Communication/Proxies/DiscoveringMongoServerProxy.cs

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

3231
// volatile will ensure that our reads are not reordered such one could get placed before a write. This
@@ -40,12 +39,10 @@ internal sealed class DiscoveringMongoServerProxy : IMongoServerProxy
4039
/// <summary>
4140
/// Initializes a new instance of the <see cref="DiscoveringMongoServerProxy"/> class.
4241
/// </summary>
43-
/// <param name="sequentialId">The sequential id.</param>
4442
/// <param name="settings">The settings.</param>
45-
public DiscoveringMongoServerProxy(int sequentialId, MongoServerProxySettings settings)
43+
public DiscoveringMongoServerProxy(MongoServerSettings settings)
4644
{
4745
_state = MongoServerState.Disconnected;
48-
_sequentialId = sequentialId;
4946
_settings = settings;
5047
_instances = settings.Servers.Select(a => new MongoServerInstance(settings, a)).ToList().AsReadOnly();
5148
}
@@ -99,14 +96,6 @@ public ReadOnlyCollection<MongoServerInstance> Instances
9996
}
10097
}
10198

102-
/// <summary>
103-
/// Gets the sequential id assigned to this proxy.
104-
/// </summary>
105-
public int SequentialId
106-
{
107-
get { return _sequentialId; }
108-
}
109-
11099
/// <summary>
111100
/// Gets the state.
112101
/// </summary>
@@ -257,11 +246,11 @@ private void CreateActualProxy(MongoServerInstance instance, BlockingQueue<Mongo
257246
{
258247
if (instance.InstanceType == MongoServerInstanceType.ReplicaSetMember)
259248
{
260-
_serverProxy = new ReplicaSetMongoServerProxy(_sequentialId, _settings, _instances, connectionQueue, _connectionAttempt);
249+
_serverProxy = new ReplicaSetMongoServerProxy(_settings, _instances, connectionQueue, _connectionAttempt);
261250
}
262251
else if (instance.InstanceType == MongoServerInstanceType.ShardRouter)
263252
{
264-
_serverProxy = new ShardedMongoServerProxy(_sequentialId, _settings, _instances, connectionQueue, _connectionAttempt);
253+
_serverProxy = new ShardedMongoServerProxy(_settings, _instances, connectionQueue, _connectionAttempt);
265254
}
266255
else if (instance.InstanceType == MongoServerInstanceType.StandAlone)
267256
{
@@ -271,7 +260,7 @@ private void CreateActualProxy(MongoServerInstance instance, BlockingQueue<Mongo
271260
otherInstance.Disconnect();
272261
}
273262

274-
_serverProxy = new DirectMongoServerProxy(_sequentialId, _settings, instance, _connectionAttempt);
263+
_serverProxy = new DirectMongoServerProxy(_settings, instance, _connectionAttempt);
275264
}
276265
else
277266
{

MongoDB.Driver/Communication/Proxies/IMongoServerProxy.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ 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-
4641
/// <summary>
4742
/// Gets the state.
4843
/// </summary>

MongoDB.Driver/Communication/Proxies/MongoServerProxyFactory.cs

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

16-
using System.Collections.Generic;
1716
using System.Linq;
1817

1918
namespace MongoDB.Driver.Internal
@@ -23,67 +22,13 @@ namespace MongoDB.Driver.Internal
2322
/// </summary>
2423
internal class MongoServerProxyFactory
2524
{
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-
6525
// public methods
6626
/// <summary>
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).
27+
/// Creates an IMongoServerProxy of some type that depends on the server settings.
6828
/// </summary>
6929
/// <param name="settings">The settings.</param>
7030
/// <returns>An IMongoServerProxy.</returns>
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)
31+
public IMongoServerProxy Create(MongoServerSettings settings)
8732
{
8833
var connectionMode = settings.ConnectionMode;
8934
if (settings.ConnectionMode == ConnectionMode.Automatic)
@@ -101,13 +46,13 @@ private IMongoServerProxy CreateInstance(int sequentialId, MongoServerProxySetti
10146
switch (connectionMode)
10247
{
10348
case ConnectionMode.Direct:
104-
return new DirectMongoServerProxy(sequentialId, settings);
49+
return new DirectMongoServerProxy(settings);
10550
case ConnectionMode.ReplicaSet:
106-
return new ReplicaSetMongoServerProxy(sequentialId, settings);
51+
return new ReplicaSetMongoServerProxy(settings);
10752
case ConnectionMode.ShardRouter:
108-
return new ShardedMongoServerProxy(sequentialId, settings);
53+
return new ShardedMongoServerProxy(settings);
10954
default:
110-
return new DiscoveringMongoServerProxy(sequentialId, settings);
55+
return new DiscoveringMongoServerProxy(settings);
11156
}
11257
}
11358
}

0 commit comments

Comments
 (0)