From cf572fbada2fde95b8257e32c708f9b5a70adc68 Mon Sep 17 00:00:00 2001 From: Henrik Gedionsen Date: Thu, 21 Aug 2025 09:30:07 +0200 Subject: [PATCH] Flush & write concurrently in LoggingTunnel and avoid double lookups in dictionaries --- src/StackExchange.Redis/ClusterConfiguration.cs | 4 ++-- src/StackExchange.Redis/Configuration/LoggingTunnel.cs | 9 ++++++--- .../ConnectionMultiplexer.Sentinel.cs | 8 +++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/StackExchange.Redis/ClusterConfiguration.cs b/src/StackExchange.Redis/ClusterConfiguration.cs index 0ce256c95..99488ddff 100644 --- a/src/StackExchange.Redis/ClusterConfiguration.cs +++ b/src/StackExchange.Redis/ClusterConfiguration.cs @@ -180,7 +180,7 @@ internal ClusterConfiguration(ServerSelectionStrategy serverSelectionStrategy, s if (node.IsMyself) Origin = node.EndPoint; - if (nodeLookup.ContainsKey(node.EndPoint)) + if (nodeLookup.TryGetValue(node.EndPoint, out var lookedUpNode)) { // Deal with conflicting node entries for the same endpoint // 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 // The node we're trying to add is probably about to become stale. Ignore it. continue; } - else if (!nodeLookup[node.EndPoint].IsConnected) + else if (!lookedUpNode.IsConnected) { // The node we registered previously is probably stale. Replace it with a known good node. nodeLookup[node.EndPoint] = node; diff --git a/src/StackExchange.Redis/Configuration/LoggingTunnel.cs b/src/StackExchange.Redis/Configuration/LoggingTunnel.cs index 0eca972b8..ccfa4ee63 100644 --- a/src/StackExchange.Redis/Configuration/LoggingTunnel.cs +++ b/src/StackExchange.Redis/Configuration/LoggingTunnel.cs @@ -504,8 +504,9 @@ public override void Flush() public override async Task FlushAsync(CancellationToken cancellationToken) { - await _writes.FlushAsync().ForAwait(); + var writesTask = _writes.FlushAsync().ForAwait(); await _inner.FlushAsync().ForAwait(); + await writesTask; } protected override void Dispose(bool disposing) @@ -608,8 +609,9 @@ public override void Write(byte[] buffer, int offset, int count) } public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { - await _writes.WriteAsync(buffer, offset, count, cancellationToken).ForAwait(); + var writesTask = _writes.WriteAsync(buffer, offset, count, cancellationToken).ForAwait(); await _inner.WriteAsync(buffer, offset, count, cancellationToken).ForAwait(); + await writesTask; } #if NETCOREAPP3_0_OR_GREATER public override void Write(ReadOnlySpan buffer) @@ -619,8 +621,9 @@ public override void Write(ReadOnlySpan buffer) } public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken) { - await _writes.WriteAsync(buffer, cancellationToken).ForAwait(); + var writesTask = _writes.WriteAsync(buffer, cancellationToken).ForAwait(); await _inner.WriteAsync(buffer, cancellationToken).ForAwait(); + await writesTask; } #endif } diff --git a/src/StackExchange.Redis/ConnectionMultiplexer.Sentinel.cs b/src/StackExchange.Redis/ConnectionMultiplexer.Sentinel.cs index b1bf7371c..61b36b014 100644 --- a/src/StackExchange.Redis/ConnectionMultiplexer.Sentinel.cs +++ b/src/StackExchange.Redis/ConnectionMultiplexer.Sentinel.cs @@ -14,7 +14,7 @@ public partial class ConnectionMultiplexer { internal EndPoint? currentSentinelPrimaryEndPoint; internal Timer? sentinelPrimaryReconnectTimer; - internal Dictionary sentinelConnectionChildren = new Dictionary(); + internal readonly Dictionary sentinelConnectionChildren = new(); internal ConnectionMultiplexer? sentinelConnection; /// @@ -44,10 +44,8 @@ internal void InitializeSentinel(ILogger? log) lock (sentinelConnectionChildren) { // Switch the primary if we have connections for that service - if (sentinelConnectionChildren.ContainsKey(messageParts[0])) + if (sentinelConnectionChildren.TryGetValue(messageParts[0], out ConnectionMultiplexer? child)) { - ConnectionMultiplexer child = sentinelConnectionChildren[messageParts[0]]; - // Is the connection still valid? if (child.IsDisposed) { @@ -57,7 +55,7 @@ internal void InitializeSentinel(ILogger? log) } else { - SwitchPrimary(switchBlame, sentinelConnectionChildren[messageParts[0]]); + SwitchPrimary(switchBlame, child); } } }