本文整理汇总了C#中MongoServerState类的典型用法代码示例。如果您正苦于以下问题:C# MongoServerState类的具体用法?C# MongoServerState怎么用?C# MongoServerState使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MongoServerState类属于命名空间,在下文中一共展示了MongoServerState类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DiscoveringMongoServerProxy
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="DiscoveringMongoServerProxy"/> class.
/// </summary>
/// <param name="sequentialId">The sequential id.</param>
/// <param name="settings">The settings.</param>
public DiscoveringMongoServerProxy(int sequentialId, MongoServerProxySettings settings)
{
_state = MongoServerState.Disconnected;
_sequentialId = sequentialId;
_settings = settings;
_instances = settings.Servers.Select(a => new MongoServerInstance(settings, a)).ToList().AsReadOnly();
}
示例2: MongoServerInstance
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="MongoServerInstance"/> class.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="address">The address.</param>
internal MongoServerInstance(MongoServerSettings settings, MongoServerAddress address)
{
_settings = settings;
_address = address;
_sequentialId = Interlocked.Increment(ref __nextSequentialId);
_state = MongoServerState.Disconnected;
_serverInfo = new ServerInformation
{
MaxDocumentSize = MongoDefaults.MaxDocumentSize,
MaxMessageLength = MongoDefaults.MaxMessageLength,
InstanceType = MongoServerInstanceType.Unknown
};
_connectionPool = new MongoConnectionPool(this);
_pingTimeAggregator = new PingTimeAggregator(5);
_permanentlyDisconnected = false;
// Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);
_stateVerificationAcquireConnectionOptions = new MongoConnectionPool.AcquireConnectionOptions
{
OkToAvoidWaitingByCreatingNewConnection = false,
OkToExceedMaxConnectionPoolSize = true,
OkToExceedWaitQueueSize = true,
WaitQueueTimeout = TimeSpan.FromSeconds(2)
};
}
示例3: MultipleInstanceMongoServerProxy
/// <summary>
/// Initializes a new instance of the <see cref="ShardedMongoServerProxy"/> class.
/// </summary>
/// <param name="server">The server.</param>
/// <param name="instances">The instances.</param>
/// <param name="connectionQueue">The state change queue.</param>
/// <param name="connectionAttempt">The connection attempt.</param>
/// <remarks>This constructor is used when the instances have already been instructed to connect.</remarks>
protected MultipleInstanceMongoServerProxy(MongoServer server, IEnumerable<MongoServerInstance> instances, BlockingQueue<MongoServerInstance> connectionQueue, int connectionAttempt)
{
_state = MongoServerState.Connecting;
_server = server;
_connectedInstances = new ConnectedInstanceCollection();
_connectionAttempt = connectionAttempt;
_outstandingInstanceConnections = connectionQueue.Count;
ThreadPool.QueueUserWorkItem(_ =>
{
while (connectionQueue.Count > 0)
{
var instance = connectionQueue.Dequeue();
Interlocked.Decrement(ref _outstandingInstanceConnections);
}
});
// It's important to have our own copy of this list because it might get modified during iteration.
_instances = instances.ToList();
foreach (var instance in instances)
{
instance.StateChanged += InstanceStateChanged;
ProcessInstanceStateChange(instance);
}
}
示例4: MongoServerInstance
private MongoServerState state; // always use property to set value so event gets raised
#endregion
#region constructors
internal MongoServerInstance(
MongoServer server,
MongoServerAddress address
) {
this.server = server;
this.address = address;
this.maxDocumentSize = MongoDefaults.MaxDocumentSize;
this.maxMessageLength = MongoDefaults.MaxMessageLength;
this.state = MongoServerState.Disconnected;
}
示例5: MongoServerInstance
private MongoServerState _state; // always use property to set value so event gets raised
// constructors
internal MongoServerInstance(MongoServer server, MongoServerAddress address)
{
_server = server;
_address = address;
_sequentialId = Interlocked.Increment(ref __nextSequentialId);
_maxDocumentSize = MongoDefaults.MaxDocumentSize;
_maxMessageLength = MongoDefaults.MaxMessageLength;
_state = MongoServerState.Disconnected;
_connectionPool = new MongoConnectionPool(this);
// Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);
}
示例6: MongoServerInstance
// constructors
internal MongoServerInstance(MongoServer server, MongoServerAddress address)
{
this.server = server;
this.address = address;
this.sequentialId = Interlocked.Increment(ref nextSequentialId);
this.maxDocumentSize = MongoDefaults.MaxDocumentSize;
this.maxMessageLength = MongoDefaults.MaxMessageLength;
this.state = MongoServerState.Disconnected;
this.connectionPool = new MongoConnectionPool(this);
this.tags = new HashSet<string>();
// Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);
}
示例7: MongoServerInstance
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="MongoServerInstance"/> class.
/// </summary>
/// <param name="settings">The settings.</param>
/// <param name="address">The address.</param>
internal MongoServerInstance(MongoServerSettings settings, MongoServerAddress address)
{
_settings = settings;
_address = address;
_sequentialId = Interlocked.Increment(ref __nextSequentialId);
_state = MongoServerState.Disconnected;
_serverInfo = new ServerInformation
{
MaxDocumentSize = MongoDefaults.MaxDocumentSize,
MaxMessageLength = MongoDefaults.MaxMessageLength,
InstanceType = MongoServerInstanceType.Unknown
};
_connectionPool = new MongoConnectionPool(this);
_pingTimeAggregator = new PingTimeAggregator(5);
_permanentlyDisconnected = false;
// Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);
}
示例8: DetermineServerState
/// <summary>
/// Determines the state of the server.
/// </summary>
/// <param name="currentState">State of the current.</param>
/// <param name="instances">The instances.</param>
/// <returns>The server state.</returns>
protected override MongoServerState DetermineServerState(MongoServerState currentState, IEnumerable<MongoServerInstance> instances)
{
if (!instances.Any())
{
return MongoServerState.Disconnected;
}
// the order of the tests is significant
// and resolves ambiguities when more than one state might match
if (currentState == MongoServerState.Disconnecting)
{
if (instances.All(i => i.State == MongoServerState.Disconnected))
{
return MongoServerState.Disconnected;
}
}
else
{
if (instances.All(i => i.State == MongoServerState.Disconnected))
{
return MongoServerState.Disconnected;
}
else if (instances.All(i => i.State == MongoServerState.Connected))
{
return MongoServerState.Connected;
}
else if (instances.Any(i => i.State == MongoServerState.Connecting))
{
return MongoServerState.Connecting;
}
else if (instances.Any(i => i.State == MongoServerState.Unknown))
{
return MongoServerState.Unknown;
}
else if (instances.Any(i => i.State == MongoServerState.Connected))
{
return MongoServerState.ConnectedToSubset;
}
throw new MongoInternalException("Unexpected server instance states.");
}
return currentState;
}
示例9: SetState
internal void SetState(MongoServerState state)
{
lock (_serverInstanceLock)
{
if (_state != state)
{
_state = state;
OnStateChanged();
}
}
}
示例10: EnsureInstanceManager
// private methods
private void EnsureInstanceManager(TimeSpan timeout)
{
if (_serverProxy == null)
{
lock (_lock)
{
if (_serverProxy == null)
{
_connectionAttempt++;
_state = MongoServerState.Connecting;
Discover(timeout);
}
}
}
}
示例11: Connect
/// <summary>
/// Connects to the instances respecting the timeout and readPreference.
/// </summary>
/// <param name="timeout">The timeout.</param>
/// <param name="readPreference">The read preference.</param>
public void Connect(TimeSpan timeout, ReadPreference readPreference)
{
try
{
EnsureInstanceManager(timeout);
}
catch
{
_state = MongoServerState.Disconnected;
throw;
}
_serverProxy.Connect(timeout, readPreference);
}
示例12: Connect
/// <summary>
/// Connects this instance.
/// </summary>
internal void Connect()
{
// Console.WriteLine("MongoServerInstance[{0}]: Connect() called.", sequentialId);
lock (_serverInstanceLock)
{
if (_permanentlyDisconnected || _state == MongoServerState.Connecting || _state == MongoServerState.Connected)
{
return;
}
_connectException = null;
// set the state manually here because SetState raises an event that shouldn't be raised
// while holding a lock.
_state = MongoServerState.Connecting;
}
// We know for certain that the state just changed
OnStateChanged();
try
{
var connection = _connectionPool.AcquireConnection(null, null);
try
{
Ping(connection);
LookupServerInformation(connection);
}
finally
{
_connectionPool.ReleaseConnection(connection);
}
SetState(MongoServerState.Connected);
}
catch (Exception ex)
{
lock (_serverInstanceLock)
{
_connectException = ex;
}
_connectionPool.Clear();
Interlocked.Exchange(ref _connectException, ex);
SetState(MongoServerState.Disconnected);
throw;
}
finally
{
lock (_serverInstanceLock)
{
if (_stateVerificationTimer == null)
{
_stateVerificationTimer = new Timer(o => StateVerificationTimerCallback(), null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
}
}
}
}
示例13: ProcessInstanceStateChange
private void ProcessInstanceStateChange(MongoServerInstance instance)
{
lock (_lock)
{
if (_instances.Contains(instance))
{
if (instance.State == MongoServerState.Connected)
{
if (!IsValidInstance(instance))
{
RemoveInstance(instance);
return;
}
if (instance.Address != instance.IsMasterResult.MyAddress)
{
if (!_instances.Any(x => x.Address == instance.IsMasterResult.MyAddress))
{
instance.Address = instance.IsMasterResult.MyAddress;
}
else
{
// we need to get rid of the duplicate.
RemoveInstance(instance);
return;
}
}
if (_state != MongoServerState.Disconnecting && _state != MongoServerState.Disconnected)
{
_connectedInstances.EnsureContains(instance);
ProcessConnectedInstanceStateChange(instance);
}
}
else
{
_connectedInstances.Remove(instance);
}
}
_state = DetermineServerState(_state, _instances);
}
}
示例14: DetermineServerState
/// <summary>
/// Determines the state of the server.
/// </summary>
/// <param name="currentState">State of the current.</param>
/// <param name="instances">The instances.</param>
/// <returns>The state of the server.</returns>
protected abstract MongoServerState DetermineServerState(MongoServerState currentState, IEnumerable<MongoServerInstance> instances);
示例15: Connect
/// <summary>
/// Connects to the instances respecting the timeout and readPreference.
/// </summary>
/// <param name="timeout">The timeout.</param>
/// <param name="readPreference">The read preference.</param>
public void Connect(TimeSpan timeout, ReadPreference readPreference)
{
var timeoutAt = DateTime.UtcNow + timeout;
while (DateTime.UtcNow < timeoutAt)
{
if (ChooseServerInstance(_connectedInstances, readPreference) != null)
{
return;
}
if (Interlocked.CompareExchange(ref _outstandingInstanceConnections, 0, 0) > 0)
{
Thread.Sleep(TimeSpan.FromMilliseconds(20));
continue;
}
lock (_lock)
{
// test this again (kinda like the double lock check pattern). This value may
// be different and we don't want to issue another round of connects needlessly.
if (Interlocked.CompareExchange(ref _outstandingInstanceConnections, 0, 0) > 0)
{
Thread.Sleep(TimeSpan.FromMilliseconds(20));
continue;
}
// if we are already fully connected and an instance still isn't chosen,
// then one simply doesn't exist, so we'll break immediately and throw a
// connection exception.
if (_state == MongoServerState.Connected)
{
break;
}
_state = MongoServerState.Connecting;
_connectionAttempt++;
foreach (var instance in _instances)
{
ConnectInstance(instance);
}
}
}
ThrowConnectionException(readPreference);
}