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

Commit d855694

Browse files
committed
Add new ForceMasterFailover() API and sanitize outputs
1 parent 865a710 commit d855694

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/ServiceStack.Redis/RedisSentinelWorker.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public RedisSentinelWorker(RedisSentinel sentinel, RedisEndpoint sentinelEndpoin
2020
this.sentinel = sentinel;
2121
this.sentinelClient = new RedisClient(sentinelEndpoint) {
2222
Db = 0, //Sentinel Servers doesn't support DB, reset to 0
23-
ConnectTimeout = sentinel.SentinelWorkerTimeoutMs,
23+
ConnectTimeout = sentinel.SentinelWorkerConnectTimeoutMs,
24+
ReceiveTimeout = sentinel.SentinelWorkerReceiveTimeoutMs,
25+
SendTimeout = sentinel.SentinelWorkerSendTimeoutMs,
2426
};
2527

2628
if (Log.IsDebugEnabled)
@@ -72,18 +74,21 @@ internal SentinelInfo GetSentinelInfo()
7274
internal string GetMasterHost(string masterName)
7375
{
7476
var masterInfo = sentinelClient.SentinelGetMasterAddrByName(masterName);
75-
if (masterInfo.Count > 0)
76-
{
77-
var ip = masterInfo[0];
78-
var port = masterInfo[1];
77+
return masterInfo.Count > 0
78+
? SanitizeMasterConfig(masterInfo)
79+
: null;
80+
}
7981

80-
string aliasIp;
81-
if (sentinel.IpAddressMap.TryGetValue(ip, out aliasIp))
82-
ip = aliasIp;
82+
private string SanitizeMasterConfig(List<string> masterInfo)
83+
{
84+
var ip = masterInfo[0];
85+
var port = masterInfo[1];
8386

84-
return "{0}:{1}".Fmt(ip, port);
85-
}
86-
return null;
87+
string aliasIp;
88+
if (sentinel.IpAddressMap.TryGetValue(ip, out aliasIp))
89+
ip = aliasIp;
90+
91+
return "{0}:{1}".Fmt(ip, port);
8792
}
8893

8994
internal List<string> GetSentinelHosts(string masterName)
@@ -152,6 +157,14 @@ public void BeginListeningForConfigurationChanges()
152157
}
153158
}
154159

160+
public string ForceMasterFailover(string masterName)
161+
{
162+
var masterInfo = this.sentinelClient.SentinelFailover(masterName);
163+
return masterInfo.Count > 0
164+
? SanitizeMasterConfig(masterInfo)
165+
: null;
166+
}
167+
155168
public void Dispose()
156169
{
157170
new IDisposable[] { this.sentinelClient, sentinePubSub }.Dispose(Log);

0 commit comments

Comments
 (0)