Skip to content

Commit 229e9c5

Browse files
authored
Flush & write concurrently in LoggingTunnel and avoid double lookups in dictionaries (#2943)
1 parent 91bbd3e commit 229e9c5

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

src/StackExchange.Redis/ClusterConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ internal ClusterConfiguration(ServerSelectionStrategy serverSelectionStrategy, s
180180
if (node.IsMyself)
181181
Origin = node.EndPoint;
182182

183-
if (nodeLookup.ContainsKey(node.EndPoint))
183+
if (nodeLookup.TryGetValue(node.EndPoint, out var lookedUpNode))
184184
{
185185
// Deal with conflicting node entries for the same endpoint
186186
// This can happen in dynamic environments when a node goes down and a new one is created
@@ -190,7 +190,7 @@ internal ClusterConfiguration(ServerSelectionStrategy serverSelectionStrategy, s
190190
// The node we're trying to add is probably about to become stale. Ignore it.
191191
continue;
192192
}
193-
else if (!nodeLookup[node.EndPoint].IsConnected)
193+
else if (!lookedUpNode.IsConnected)
194194
{
195195
// The node we registered previously is probably stale. Replace it with a known good node.
196196
nodeLookup[node.EndPoint] = node;

src/StackExchange.Redis/Configuration/LoggingTunnel.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,9 @@ public override void Flush()
504504

505505
public override async Task FlushAsync(CancellationToken cancellationToken)
506506
{
507-
await _writes.FlushAsync().ForAwait();
507+
var writesTask = _writes.FlushAsync().ForAwait();
508508
await _inner.FlushAsync().ForAwait();
509+
await writesTask;
509510
}
510511

511512
protected override void Dispose(bool disposing)
@@ -608,8 +609,9 @@ public override void Write(byte[] buffer, int offset, int count)
608609
}
609610
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
610611
{
611-
await _writes.WriteAsync(buffer, offset, count, cancellationToken).ForAwait();
612+
var writesTask = _writes.WriteAsync(buffer, offset, count, cancellationToken).ForAwait();
612613
await _inner.WriteAsync(buffer, offset, count, cancellationToken).ForAwait();
614+
await writesTask;
613615
}
614616
#if NETCOREAPP3_0_OR_GREATER
615617
public override void Write(ReadOnlySpan<byte> buffer)
@@ -619,8 +621,9 @@ public override void Write(ReadOnlySpan<byte> buffer)
619621
}
620622
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
621623
{
622-
await _writes.WriteAsync(buffer, cancellationToken).ForAwait();
624+
var writesTask = _writes.WriteAsync(buffer, cancellationToken).ForAwait();
623625
await _inner.WriteAsync(buffer, cancellationToken).ForAwait();
626+
await writesTask;
624627
}
625628
#endif
626629
}

src/StackExchange.Redis/ConnectionMultiplexer.Sentinel.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public partial class ConnectionMultiplexer
1414
{
1515
internal EndPoint? currentSentinelPrimaryEndPoint;
1616
internal Timer? sentinelPrimaryReconnectTimer;
17-
internal Dictionary<string, ConnectionMultiplexer> sentinelConnectionChildren = new Dictionary<string, ConnectionMultiplexer>();
17+
internal readonly Dictionary<string, ConnectionMultiplexer> sentinelConnectionChildren = new();
1818
internal ConnectionMultiplexer? sentinelConnection;
1919

2020
/// <summary>
@@ -44,10 +44,8 @@ internal void InitializeSentinel(ILogger? log)
4444
lock (sentinelConnectionChildren)
4545
{
4646
// Switch the primary if we have connections for that service
47-
if (sentinelConnectionChildren.ContainsKey(messageParts[0]))
47+
if (sentinelConnectionChildren.TryGetValue(messageParts[0], out ConnectionMultiplexer? child))
4848
{
49-
ConnectionMultiplexer child = sentinelConnectionChildren[messageParts[0]];
50-
5149
// Is the connection still valid?
5250
if (child.IsDisposed)
5351
{
@@ -57,7 +55,7 @@ internal void InitializeSentinel(ILogger? log)
5755
}
5856
else
5957
{
60-
SwitchPrimary(switchBlame, sentinelConnectionChildren[messageParts[0]]);
58+
SwitchPrimary(switchBlame, child);
6159
}
6260
}
6361
}

0 commit comments

Comments
 (0)