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

Commit 052cd51

Browse files
committed
Add support of IpAddressMap in RedisSentinel to map internal ips to external ips
1 parent 64741a2 commit 052cd51

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/ServiceStack.Redis/RedisSentinel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public string MasterName
3939
public Action<Exception> OnWorkerError { get; set; }
4040
public Action<string, string> OnSentinelMessageReceived { get; set; }
4141

42+
public Dictionary<string, string> IpAddressMap { get; set; }
43+
4244
public RedisSentinel(string sentinelHost = null, string masterName = null)
4345
: this(new[] { sentinelHost ?? DefaultAddress }, masterName ?? DefaultMasterName) { }
4446

@@ -50,6 +52,7 @@ public RedisSentinel(IEnumerable<string> sentinelHosts, string masterName = null
5052

5153
this.masterName = masterName ?? DefaultMasterName;
5254
this.RedisManagerFactory = new RedisManagerFactory();
55+
IpAddressMap = new Dictionary<string, string>();
5356
}
5457

5558
/// <summary>

src/ServiceStack.Redis/RedisSentinelWorker.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ private List<string> ConvertSlaveArrayToList(object[] slaves)
172172
data.TryGetValue("ip", out ip);
173173
data.TryGetValue("port", out port);
174174

175-
if (ip == "127.0.0.1")
175+
string externalIp;
176+
if (redisSentinel.IpAddressMap.TryGetValue(ip, out externalIp))
177+
{
178+
ip = externalIp;
179+
}
180+
else if (ip == "127.0.0.1")
176181
{
177182
ip = this.sentinelClient.Host;
178183
}
@@ -202,6 +207,12 @@ private List<string> ConvertMasterArrayToList(object[] items)
202207
data.TryGetValue("ip", out ip);
203208
data.TryGetValue("port", out port);
204209

210+
string externalIp;
211+
if (redisSentinel.IpAddressMap.TryGetValue(ip, out externalIp))
212+
{
213+
ip = externalIp;
214+
}
215+
205216
if (ip != null && port != null)
206217
{
207218
servers.Add("{0}:{1}".Fmt(ip, port));

0 commit comments

Comments
 (0)