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

Commit fcf546f

Browse files
committed
Allow default single string constructor to contain host:port format
1 parent 4f9d2c5 commit fcf546f

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ internal IRedisPipelineShared Pipeline
111111
}
112112

113113
public RedisNativeClient(string host)
114-
: this(host, DefaultPort) {}
114+
: this(host.SplitOnLast(':')[0], host.Contains(':') ? int.Parse(host.SplitOnLast(':')[1]) : DefaultPort) { }
115115

116116
public RedisNativeClient(string host, int port)
117117
: this(host, port, null) {}

src/TestMqHost/Program2.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ namespace TestMqHost
1313
{
1414
class Program2
1515
{
16+
1617
static void Main(string[] args)
1718
{
1819
var clientManager = new PooledRedisClientManager(new[] { "localhost" })
1920
{
2021
PoolTimeout = 1000,
2122
};
22-
clientManager.GetClient().FlushAll();
23+
using (var client = clientManager.GetClient())
24+
{
25+
client.FlushAll();
26+
}
2327

2428
var mqHost = new RedisMqServer(clientManager);
2529

tests/ServiceStack.Redis.Tests/RedisFailoverTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,78 @@ public void Can_MqServer_recover_from_server_terminated_client_connections()
140140
});
141141

142142
}
143+
144+
[Test]
145+
public void Can_failover_at_runtime()
146+
{
147+
var failoverHost = "ny-devredis01:6380";
148+
var localClient = new RedisClient("localhost");
149+
string key = "test:failover";
150+
151+
localClient.Remove(key);
152+
var failoverClient = new RedisClient(failoverHost);
153+
failoverClient.Remove(key);
154+
155+
var clientManager = new PooledRedisClientManager(new[] { "localhost" });
156+
157+
RunInLoop(clientManager, callback:() =>
158+
{
159+
lock (clientManager)
160+
Monitor.Pulse(clientManager);
161+
});
162+
163+
Thread.Sleep(100);
164+
165+
clientManager.FailoverTo(failoverHost);
166+
167+
lock (clientManager)
168+
Monitor.Wait(clientManager);
169+
170+
var localIncr = localClient.Get<int>(key);
171+
var failoverIncr = failoverClient.Get<int>(key);
172+
Assert.That(localIncr, Is.GreaterThan(0));
173+
Assert.That(failoverIncr, Is.GreaterThan(0));
174+
Assert.That(localIncr + failoverIncr, Is.EqualTo(100));
175+
}
176+
177+
178+
public static bool RunInLoop(PooledRedisClientManager clientManager, int iterations = 100, int sleepMs = 10, Action callback=null)
179+
{
180+
int count = 0;
181+
int errors = 0;
182+
183+
10.Times(i =>
184+
{
185+
ThreadPool.QueueUserWorkItem(_ =>
186+
{
187+
while (iterations-- > 0)
188+
{
189+
using (var client = clientManager.GetClient())
190+
{
191+
try
192+
{
193+
var result = client.Increment("test:failover", 1);
194+
if (++count % (iterations / 10) == 0)
195+
lock (clientManager)
196+
Console.WriteLine("count: {0}, errors: {1}", count, errors);
197+
}
198+
catch (Exception ex)
199+
{
200+
errors++;
201+
}
202+
Thread.Sleep(sleepMs);
203+
}
204+
}
205+
206+
if (callback != null)
207+
{
208+
callback();
209+
callback = null;
210+
}
211+
});
212+
});
213+
214+
return true;
215+
}
143216
}
144217
}

0 commit comments

Comments
 (0)