@@ -48,6 +48,7 @@ internal sealed class PhysicalBridge : IDisposable
4848 private int failConnectCount = 0 ;
4949 private volatile bool isDisposed ;
5050 private volatile bool shouldResetConnectionRetryCount ;
51+ private bool _needsReconnect ;
5152 private long nonPreferredEndpointCount ;
5253
5354 // private volatile int missedHeartbeats;
@@ -131,6 +132,16 @@ public enum State : byte
131132 private RedisProtocol _protocol ; // note starts at zero, not RESP2
132133 internal void SetProtocol ( RedisProtocol protocol ) => _protocol = protocol ;
133134
135+ /// <summary>
136+ /// Indicates whether the bridge needs to reconnect.
137+ /// </summary>
138+ internal bool NeedsReconnect => Volatile . Read ( ref _needsReconnect ) ;
139+
140+ /// <summary>
141+ /// Marks that the bridge needs to reconnect.
142+ /// </summary>
143+ internal void MarkNeedsReconnect ( ) => Volatile . Write ( ref _needsReconnect , true ) ;
144+
134145 public void Dispose ( )
135146 {
136147 isDisposed = true ;
@@ -210,7 +221,7 @@ private WriteResult FailDueToNoConnection(Message message)
210221 public WriteResult TryWriteSync ( Message message , bool isReplica )
211222 {
212223 if ( isDisposed ) throw new ObjectDisposedException ( Name ) ;
213- if ( ! IsConnected ) return QueueOrFailMessage ( message ) ;
224+ if ( ! IsConnected || NeedsReconnect ) return QueueOrFailMessage ( message ) ;
214225
215226 var physical = this . physical ;
216227 if ( physical == null )
@@ -234,7 +245,7 @@ public WriteResult TryWriteSync(Message message, bool isReplica)
234245 public ValueTask < WriteResult > TryWriteAsync ( Message message , bool isReplica , bool bypassBacklog = false )
235246 {
236247 if ( isDisposed ) throw new ObjectDisposedException ( Name ) ;
237- if ( ! IsConnected && ! bypassBacklog ) return new ValueTask < WriteResult > ( QueueOrFailMessage ( message ) ) ;
248+ if ( ( ! IsConnected || NeedsReconnect ) && ! bypassBacklog ) return new ValueTask < WriteResult > ( QueueOrFailMessage ( message ) ) ;
238249
239250 var physical = this . physical ;
240251 if ( physical == null )
@@ -1458,6 +1469,8 @@ private bool ChangeState(State oldState, State newState)
14581469 Multiplexer . Trace ( "Connecting..." , Name ) ;
14591470 if ( ChangeState ( State . Disconnected , State . Connecting ) )
14601471 {
1472+ // Clear the reconnect flag as we're starting a new connection
1473+ Volatile . Write ( ref _needsReconnect , false ) ;
14611474 Interlocked . Increment ( ref socketCount ) ;
14621475 Interlocked . Exchange ( ref connectStartTicks , Environment . TickCount ) ;
14631476 // separate creation and connection for case when connection completes synchronously
0 commit comments