Skip to content

Commit 3a9d8c9

Browse files
committed
- add explicit connection failure type
- burn the connection on failure - add initial metrics
1 parent 75ae891 commit 3a9d8c9

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

src/StackExchange.Redis/Enums/ConnectionFailureType.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public enum ConnectionFailureType
4444
/// <summary>
4545
/// It has not been possible to create an initial connection to the redis server(s).
4646
/// </summary>
47-
UnableToConnect
47+
UnableToConnect,
48+
/// <summary>
49+
/// High-integrity mode was enabled, and a failure was detected
50+
/// </summary>
51+
ResponseIntegrityFailure,
4852
}
4953
}

src/StackExchange.Redis/PhysicalConnection.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,10 @@ private void MatchResult(in RawResult result)
17051705
if (_awaitingToken is not null && (msg = Interlocked.Exchange(ref _awaitingToken, null)) is not null)
17061706
{
17071707
_readStatus = ReadStatus.ResponseSequenceCheck;
1708-
ProcessHighIntegrityResponseToken(msg, in result, BridgeCouldBeNull);
1708+
if (!ProcessHighIntegrityResponseToken(msg, in result, BridgeCouldBeNull))
1709+
{
1710+
RecordConnectionFailed(ConnectionFailureType.ResponseIntegrityFailure, origin: nameof(ReadStatus.ResponseSequenceCheck));
1711+
}
17091712
return;
17101713
}
17111714

@@ -1745,7 +1748,7 @@ private void MatchResult(in RawResult result)
17451748
_readStatus = ReadStatus.MatchResultComplete;
17461749
_activeMessage = null;
17471750

1748-
static void ProcessHighIntegrityResponseToken(Message message, in RawResult result, PhysicalBridge? bridge)
1751+
static bool ProcessHighIntegrityResponseToken(Message message, in RawResult result, PhysicalBridge? bridge)
17491752
{
17501753
bool isValid = false;
17511754
if (result.Resp2TypeBulkString == ResultType.BulkString)
@@ -1762,20 +1765,21 @@ static void ProcessHighIntegrityResponseToken(Message message, in RawResult resu
17621765
{
17631766
Span<byte> span = stackalloc byte[4];
17641767
payload.CopyTo(span);
1765-
interpreted = BinaryPrimitives.ReadUInt32LittleEndian(payload.First.Span);
1768+
interpreted = BinaryPrimitives.ReadUInt32LittleEndian(span);
17661769
}
17671770
isValid = interpreted == message.HighIntegrityToken;
17681771
}
17691772
}
17701773
if (isValid)
17711774
{
17721775
message.Complete();
1776+
return true;
17731777
}
17741778
else
17751779
{
17761780
message.SetExceptionAndComplete(new InvalidOperationException("High-integrity mode detected possible protocol de-sync"), bridge);
1781+
return false;
17771782
}
1778-
17791783
}
17801784

17811785
static bool TryGetPubSubPayload(in RawResult value, out RedisValue parsed, bool allowArraySingleton = true)

src/StackExchange.Redis/PublicAPI/PublicAPI.Shipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ StackExchange.Redis.ConnectionFailureType.ProtocolFailure = 4 -> StackExchange.R
321321
StackExchange.Redis.ConnectionFailureType.SocketClosed = 6 -> StackExchange.Redis.ConnectionFailureType
322322
StackExchange.Redis.ConnectionFailureType.SocketFailure = 2 -> StackExchange.Redis.ConnectionFailureType
323323
StackExchange.Redis.ConnectionFailureType.UnableToConnect = 9 -> StackExchange.Redis.ConnectionFailureType
324+
StackExchange.Redis.ConnectionFailureType.ResponseIntegrityFailure = 10 -> StackExchange.Redis.ConnectionFailureType
324325
StackExchange.Redis.ConnectionFailureType.UnableToResolvePhysicalConnection = 1 -> StackExchange.Redis.ConnectionFailureType
325326
StackExchange.Redis.ConnectionMultiplexer
326327
StackExchange.Redis.ConnectionMultiplexer.ClientName.get -> string!
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-

1+
#nullable enable

tests/ConsoleTest/Program.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@
66
stopwatch.Start();
77

88
var options = ConfigurationOptions.Parse("localhost");
9+
#if !SEREDIS_BASELINE
10+
options.HighIntegrity = false; // as needed
11+
Console.WriteLine($"{nameof(options.HighIntegrity)}: {options.HighIntegrity}");
12+
#endif
13+
914
//options.SocketManager = SocketManager.ThreadPool;
1015
var connection = ConnectionMultiplexer.Connect(options);
16+
connection.ConnectionFailed += Connection_ConnectionFailed;
17+
18+
void Connection_ConnectionFailed(object? sender, ConnectionFailedEventArgs e)
19+
{
20+
Console.Error.WriteLine($"CONNECTION FAILED: {e.ConnectionType}, {e.FailureType}, {e.Exception}");
21+
}
1122

1223
var startTime = DateTime.UtcNow;
1324
var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime;
1425

15-
var scenario = args?.Length > 0 ? args[0] : "parallel";
26+
var scenario = args?.Length > 0 ? args[0] : "mass-insert";
1627

1728
switch (scenario)
1829
{

tests/ConsoleTestBaseline/ConsoleTestBaseline.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<OutputType>Exe</OutputType>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<DefineConstants>$(DefineConstants);SEREDIS_BASELINE</DefineConstants>
89
</PropertyGroup>
910

1011
<ItemGroup>

0 commit comments

Comments
 (0)