.. that way you can get rid of Sniff(), Ping() -> cleaner code
i.e
public class ConnectionPool : IConnection
{
// pseudo..
public ConnectionPool( IEnumerable<IConnection> connections )
{
_selector = new RandomSelector( connections );
// ......
}
protected async Task<ElasticResponse> Execute( HttpMethod method, string path, string data )
{
// delegate the call to the connection
var connection = await FindConnectionAsync();
return await connection.Execute( method, path, data ); // Get, Post, ....
}
protected virtual async Task<IConnection> FindConnectionAsync()
{
for( var i = 0; i < _settings.RetryCount; i++ ) {
var connection = await _selector.MoveNext(); // could be implemented as an IEnumerator<Task<IConnection>>
if( connection != null ) {
return connection;
}
await Task.Delay( _settings.RetryWait );
}
throw new SomeExceptionAboutNotAbleToFindAcceptableConnection();
}
}
... or.. you know, I could just do it for you ;-)