Created
November 9, 2011 06:39
-
-
Save chakrit/1350627 to your computer and use it in GitHub Desktop.
Manual clustering sample for sider.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Sider.Samples | |
{ | |
public class ClusterSample : Sample | |
{ | |
private IClientsPool<string>[] _pool = new[] { | |
new ThreadwisePool<string>(host: "10.10.10.1"), | |
new ThreadwisePool<string>(host: "10.10.10.2"), | |
new ThreadwisePool<string>(host: "10.10.10.3"), | |
}; | |
public override string Name { get { return "Manual clustering sample."; } } | |
public override void Run() | |
{ | |
WriteLine("Expecting 3 clients at port 6379 6380 and 6381."); | |
WriteLine("Any of it may go down, and the client will retry using other hosts."); | |
// send 1M pings | |
for (int i = 0; i < 1000000; i++) | |
ClientAction(client => client.Ping()); | |
} | |
public void ClientAction(Action<IRedisClient<string>> clientAction, | |
int hostIdx = 0) | |
{ | |
// round-robin, keeps retrying the next host | |
if (hostIdx >= _pool.Length) | |
ClientAction(clientAction, 0); | |
try { | |
// run the action on the specified host | |
var client = _pool[hostIdx].GetClient(); | |
clientAction.Invoke(client); | |
} | |
catch (TimeoutException) { | |
// retry using the next host on Timeouts | |
ClientAction(clientAction, hostIdx + 1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment