Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Add GetServerRole()
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jul 11, 2015
1 parent ad2865e commit b5d3b70
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/ServiceStack.Redis/RedisClient.cs
Expand Up @@ -1050,6 +1050,37 @@ public void MergeHyperLogs(string toKey, params string[] fromKeys)
base.PfMerge(toKey, fromKeys);
}

public RedisServerRole GetServerRole()
{
if (AssertServerVersionNumber() >= 2812)
{
var text = base.Role();
var roleName = text.Children[0].Text;
return ToServerRole(roleName);
}

string role;
this.Info.TryGetValue("role", out role);
return ToServerRole(role);
}

private static RedisServerRole ToServerRole(string roleName)
{
if (string.IsNullOrEmpty(roleName))
return RedisServerRole.Unknown;

switch (roleName)
{
case "master":
return RedisServerRole.Master;
case "slave":
return RedisServerRole.Slave;
case "sentinel":
return RedisServerRole.Sentinel;
default:
return RedisServerRole.Unknown;
}
}
}

}
2 changes: 2 additions & 0 deletions tests/ServiceStack.Redis.Tests/RedisClientConfigTests.cs
Expand Up @@ -83,6 +83,8 @@ public void Can_get_Role_Info()
{
var result = Redis.Role();
result.PrintDump();
Assert.That(result.Children[0].Text, Is.EqualTo("master"));
Assert.That(Redis.GetServerRole(), Is.EqualTo(RedisServerRole.Master));

//needs redis-server v3.0
//var slave = new RedisClient("10.0.0.9:6380");
Expand Down

0 comments on commit b5d3b70

Please sign in to comment.