public void Send()
{
while (continueSending)
{
IList<PersistentMessage> messages = null;
//normal conditions will be at 5, when there are several unreliable endpoints
//it will grow up to 31 connections all attempting to connect, timeouts can take up to 30 seconds
if ((currentlySendingCount - currentlyConnecting > 5) || currentlyConnecting > 30)
{
lock (@lock)
Monitor.Wait(@lock, TimeSpan.FromSeconds(1));
continue;
}
Endpoint point = null;
queueStorage.Send(actions =>
{
messages = actions.GetMessagesToSendAndMarkThemAsInFlight(100, 1024*1024, out point);
actions.Commit();
});
if (messages.Count == 0)
{
lock (@lock)
Monitor.Wait(@lock, TimeSpan.FromSeconds(1));
continue;
}
Interlocked.Increment(ref currentlySendingCount);
Interlocked.Increment(ref currentlyConnecting);
new Sender
{
Connected = () => Interlocked.Decrement(ref currentlyConnecting),
Destination = point,
Messages = messages.ToArray(),
Success = OnSuccess(messages),
Failure = OnFailure(point, messages),
FailureToConnect = e =>
{
Interlocked.Decrement(ref currentlyConnecting);
OnFailure(point, messages)(e);
},
Revert = OnRevert(point),
Commit = OnCommit(point, messages)
}.Send();
}
}