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

Commit 8d715e2

Browse files
committed
don't attempt to reselect db which is already set after connect
1 parent 67a3055 commit 8d715e2

5 files changed

Lines changed: 97 additions & 8 deletions

File tree

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,9 @@ internal bool AssertConnectedSocket()
265265
}
266266
}
267267

268-
private void TryConnectIfNeeded()
268+
private bool TryConnectIfNeeded()
269269
{
270+
bool didConnect = false;
270271
if (LastConnectedAtTimestamp > 0)
271272
{
272273
var now = Stopwatch.GetTimestamp();
@@ -275,24 +276,24 @@ private void TryConnectIfNeeded()
275276
if (socket == null || (elapsedSecs > IdleTimeOutSecs && !socket.IsConnected()))
276277
{
277278
Reconnect();
279+
didConnect = true;
278280
}
279281
LastConnectedAtTimestamp = now;
280282
}
281283

282284
if (socket == null)
283285
{
284286
Connect();
287+
didConnect = true;
285288
}
289+
290+
return didConnect;
286291
}
287292

288293
private bool Reconnect()
289294
{
290-
var previousDb = db;
291-
292295
SafeConnectionClose();
293-
Connect(); //sets db to 0
294-
295-
if (previousDb != RedisConfig.DefaultDb) this.Db = previousDb;
296+
Connect(); //sets db
296297

297298
return socket != null;
298299
}
@@ -360,7 +361,7 @@ private static byte[] GetCmdBytes(char cmdPrefix, int noOfLines)
360361
}
361362

362363
/// <summary>
363-
/// Command to set multuple binary safe arguments
364+
/// Command to set multiple binary safe arguments
364365
/// </summary>
365366
/// <param name="cmdWithBinaryArgs"></param>
366367
/// <returns></returns>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using ServiceStack.Logging;
5+
using ServiceStack.Redis;
6+
using ServiceStack.Text;
7+
8+
namespace ConsoleTests
9+
{
10+
public class BrPopAfterReconnection
11+
{
12+
protected internal static BasicRedisClientManager BasicRedisClientManager;
13+
14+
public void Execute()
15+
{
16+
// RedisConfig.AssumeServerVersion = 4000;
17+
// RedisConfig.DisableVerboseLogging = false;
18+
// LogManager.LogFactory = new ConsoleLogFactory();
19+
20+
var host = "localhost";
21+
var port = "6379";
22+
var db = "9";
23+
24+
var redisUri = $"{host}:{port}?db={db}";
25+
26+
BasicRedisClientManager = new BasicRedisClientManager(redisUri);
27+
var queue = "FormSaved";
28+
29+
while (true)
30+
{
31+
Task.Run(() => BlockingReceive(queue));
32+
Thread.Sleep(1000);
33+
34+
Console.WriteLine("Restart Redis and press Enter...");
35+
Console.ReadLine();
36+
37+
Console.WriteLine("Enter something:");
38+
var item = Console.ReadLine();
39+
40+
if (!string.IsNullOrWhiteSpace(item))
41+
{
42+
using (var client = BasicRedisClientManager.GetClient())
43+
{
44+
client.AddItemToList(queue, item);
45+
}
46+
47+
Console.WriteLine("Item added");
48+
}
49+
50+
Thread.Sleep(1000);
51+
}
52+
}
53+
54+
public static void BlockingReceive(string queue)
55+
{
56+
using (var client = BasicRedisClientManager.GetReadOnlyClient())
57+
{
58+
Console.WriteLine($"Listening to {queue}");
59+
60+
var fromList = client.BlockingPopItemFromList(queue, TimeSpan.FromSeconds(60));
61+
62+
Console.WriteLine($"Received:{fromList.Dump()}");
63+
}
64+
}
65+
}
66+
}

tests/Console.Tests/Console.Tests.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@
5555
<HintPath>..\..\lib\net45\ServiceStack.Text.dll</HintPath>
5656
</Reference>
5757
<Reference Include="System" />
58+
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
59+
<HintPath>..\..\src\packages\System.Buffers.4.4.0\lib\netstandard1.1\System.Buffers.dll</HintPath>
60+
<Private>True</Private>
61+
</Reference>
5862
<Reference Include="System.Core" />
63+
<Reference Include="System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
64+
<HintPath>..\..\src\packages\System.Memory.4.5.1\lib\netstandard1.1\System.Memory.dll</HintPath>
65+
<Private>True</Private>
66+
</Reference>
67+
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
68+
<HintPath>..\..\src\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
69+
<Private>True</Private>
70+
</Reference>
5971
<Reference Include="System.Xml.Linq" />
6072
<Reference Include="System.Data.DataSetExtensions" />
6173
<Reference Include="Microsoft.CSharp" />
@@ -64,6 +76,7 @@
6476
</ItemGroup>
6577
<ItemGroup>
6678
<Compile Include="BlockingPop.cs" />
79+
<Compile Include="BrPopAfterReconnection.cs" />
6780
<Compile Include="ForceFailover.cs" />
6881
<Compile Include="MasterFailoverWithPassword.cs" />
6982
<Compile Include="NetworkRedisSentinelFailoverTests.cs" />
@@ -84,6 +97,7 @@
8497
</ItemGroup>
8598
<ItemGroup>
8699
<None Include="App.config" />
100+
<None Include="packages.config" />
87101
</ItemGroup>
88102
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
89103
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

tests/Console.Tests/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ static void Main(string[] args)
3636

3737
//new BlockingPop().Execute();
3838

39-
new MasterFailoverWithPassword().Execute();
39+
//new MasterFailoverWithPassword().Execute();
40+
41+
new BrPopAfterReconnection().Execute();
4042
}
4143
}
4244
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="System.Buffers" version="4.4.0" targetFramework="net45" />
4+
<package id="System.Memory" version="4.5.1" targetFramework="net45" />
5+
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net45" />
6+
</packages>

0 commit comments

Comments
 (0)