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

Commit f494dbe

Browse files
committed
Add fallback for missing INFO command for Twemproxy
1 parent 6f7ffab commit f494dbe

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public partial class RedisNativeClient
2626
{
2727
private static Timer UsageTimer;
2828
private static int __requestsPerHour = 0;
29+
private const int Unknown = -1;
2930
public int ServerVersionNumber { get; set; }
3031

3132
public static void DisposeTimers()
@@ -91,7 +92,7 @@ private void Connect()
9192
if (ServerVersionNumber == 0)
9293
{
9394
var parts = ServerVersion.Split('.');
94-
var version = int.Parse(parts[0]) * 1000;
95+
var version = int.Parse(parts[0])*1000;
9596
if (parts.Length > 1)
9697
version += int.Parse(parts[1])*100;
9798
if (parts.Length > 2)
@@ -100,7 +101,14 @@ private void Connect()
100101
ServerVersionNumber = version;
101102
}
102103
}
103-
catch {}
104+
catch (Exception)
105+
{
106+
//Twemproxy doesn't support the INFO command so automatically closes the socket
107+
//Fallback to ServerVersionNumber=Unknown then try re-connecting
108+
ServerVersionNumber = Unknown;
109+
Connect();
110+
return;
111+
}
104112

105113
var ipEndpoint = socket.LocalEndPoint as IPEndPoint;
106114
clientPort = ipEndpoint != null ? ipEndpoint.Port : -1;

tests/ServiceStack.Redis.Tests/ServiceStack.Redis.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274
<Compile Include="RedisPersistenceProviderTests.cs" />
275275
<Compile Include="ShippersExample.cs" />
276276
<Compile Include="TestData\PopulateTestData.cs" />
277+
<Compile Include="TwemproxyTests.cs" />
277278
<Compile Include="UserSessionRedisClientTests.cs" />
278279
<Compile Include="ValueTypeExamples.cs" />
279280
<Compile Include="QueueTests.cs" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using NUnit.Framework;
2+
using ServiceStack.Text;
3+
4+
namespace ServiceStack.Redis.Tests
5+
{
6+
[TestFixture, Explicit]
7+
public class TwemproxyTests
8+
{
9+
[Test]
10+
public void Can_connect_to_twemproxy()
11+
{
12+
var redis = new RedisClient("10.0.0.14", 22121) {
13+
//ServerVersionNumber = 2611
14+
};
15+
//var redis = new RedisClient("10.0.0.14");
16+
redis.SetEntry("foo", "bar");
17+
var foo = redis.GetEntry("foo");
18+
19+
Assert.That(foo, Is.EqualTo("bar"));
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)