Skip to content

Commit 82375db

Browse files
author
rstam
committed
Changed EndPoint property of MongoServerInstance to a method because it sometimes does a DNS lookup and it is bad practice for properties to block.
1 parent 59ec66b commit 82375db

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

Driver/Core/MongoServerInstance.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class MongoServerInstance
4646
private MongoServerBuildInfo _buildInfo;
4747
private Exception _connectException;
4848
private MongoConnectionPool _connectionPool;
49-
private IPEndPoint _endPoint;
49+
private IPEndPoint _ipEndPoint;
5050
private bool _isArbiter;
5151
private CommandResult _isMasterResult;
5252
private bool _isPassive;
@@ -116,24 +116,6 @@ public MongoConnectionPool ConnectionPool
116116
get { return _connectionPool; }
117117
}
118118

119-
/// <summary>
120-
/// Gets the IP end point of this server instance.
121-
/// </summary>
122-
public IPEndPoint EndPoint
123-
{
124-
get {
125-
// use a lock free algorithm because DNS lookups are rare and concurrent lookups are tolerable
126-
// the intermediate variable is important to avoid race conditions
127-
var endPoint = _endPoint;
128-
if (endPoint == null)
129-
{
130-
endPoint = _address.ToIPEndPoint(_server.Settings.AddressFamily);
131-
_endPoint = endPoint;
132-
}
133-
return endPoint;
134-
}
135-
}
136-
137119
/// <summary>
138120
/// Gets whether this server instance is an arbiter instance.
139121
/// </summary>
@@ -215,6 +197,22 @@ public MongoServerState State
215197
}
216198

217199
// public methods
200+
/// <summary>
201+
/// Gets the IP end point of this server instance.
202+
/// </summary>
203+
public IPEndPoint GetIPEndPoint()
204+
{
205+
// use a lock free algorithm because DNS lookups are rare and concurrent lookups are tolerable
206+
// the intermediate variable is important to avoid race conditions
207+
var ipEndPoint = _ipEndPoint;
208+
if (ipEndPoint == null)
209+
{
210+
ipEndPoint = _address.ToIPEndPoint(_server.Settings.AddressFamily);
211+
_ipEndPoint = ipEndPoint;
212+
}
213+
return ipEndPoint;
214+
}
215+
218216
/// <summary>
219217
/// Checks whether the server is alive (throws an exception if not).
220218
/// </summary>

Driver/Internal/MongoConnection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,12 @@ internal void Open()
354354
throw new InvalidOperationException("Open called more than once.");
355355
}
356356

357-
var endPoint = _serverInstance.EndPoint;
358-
var tcpClient = new TcpClient(endPoint.AddressFamily);
357+
var ipEndPoint = _serverInstance.GetIPEndPoint();
358+
var tcpClient = new TcpClient(ipEndPoint.AddressFamily);
359359
tcpClient.NoDelay = true; // turn off Nagle
360360
tcpClient.ReceiveBufferSize = MongoDefaults.TcpReceiveBufferSize;
361361
tcpClient.SendBufferSize = MongoDefaults.TcpSendBufferSize;
362-
tcpClient.Connect(endPoint);
362+
tcpClient.Connect(ipEndPoint);
363363

364364
_tcpClient = tcpClient;
365365
_state = MongoConnectionState.Open;

0 commit comments

Comments
 (0)