Skip to content

Instantly share code, notes, and snippets.

@chakrit
Created November 9, 2011 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chakrit/1350627 to your computer and use it in GitHub Desktop.
Save chakrit/1350627 to your computer and use it in GitHub Desktop.
Manual clustering sample for sider.
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