Skip to content

Commit 286b096

Browse files
committed
Write lock only when applying changes on physical member (dispose, shutdown, =null ...)
=> so in normal run, only read locks are done => no slowdown out of connections problems because of read lock are reentrant
1 parent 44c765a commit 286b096

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/StackExchange.Redis/PhysicalBridge.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public WriteResult TryWriteSync(Message message, bool isReplica)
205205
if (!IsConnected) return QueueOrFailMessage(message);
206206

207207
WriteResult result = WriteResult.WriteFailure;
208-
if (threadSafePhysicalConnectionAccessor.WorkOnPhysicalWithLock(physical =>
208+
if (threadSafePhysicalConnectionAccessor.UsePhysicalWithLock(physical =>
209209
{
210210
result = WriteMessageTakingWriteLockSync(physical, message);
211211
LogNonPreferred(message.Flags, isReplica);
@@ -237,7 +237,7 @@ public ValueTask<WriteResult> TryWriteAsync(Message message, bool isReplica, boo
237237
}
238238

239239
ValueTask<WriteResult> result = new ValueTask<WriteResult>(WriteResult.Success);
240-
if (threadSafePhysicalConnectionAccessor.WorkOnPhysicalWithLock(physical =>
240+
if (threadSafePhysicalConnectionAccessor.UsePhysicalWithLock(physical =>
241241
{
242242
result = WriteMessageTakingWriteLockAsync(physical, message, bypassBacklog: bypassBacklog);
243243
LogNonPreferred(message.Flags, isReplica);
@@ -414,7 +414,7 @@ internal void KeepAlive(bool forceRun = false)
414414
{
415415
msg.SetInternalCall();
416416
Multiplexer.Trace("Enqueue: " + msg);
417-
threadSafePhysicalConnectionAccessor.WorkOnPhysicalWithLock(physical =>
417+
threadSafePhysicalConnectionAccessor.UsePhysicalWithLock(physical =>
418418
{
419419
Multiplexer.OnInfoMessage($"heartbeat ({physical?.LastWriteSecondsAgo}s >= {ServerEndPoint.WriteEverySeconds}s, {physical?.GetSentAwaitingResponseCount()} waiting) '{msg.CommandAndKey}' on '{PhysicalName}' (v{features.Version})");
420420
physical?.UpdateLastWriteTime(); // preemptively
@@ -747,7 +747,7 @@ internal bool TryEnqueue(List<Message> messages, bool isReplica)
747747
return false;
748748
}
749749

750-
return threadSafePhysicalConnectionAccessor.WorkOnPhysicalWithLock(physical =>
750+
return threadSafePhysicalConnectionAccessor.UsePhysicalWithLock(physical =>
751751
{
752752
foreach (var message in messages)
753753
{
@@ -1180,7 +1180,7 @@ private void ProcessBridgeBacklog()
11801180
try
11811181
{
11821182
_backlogStatus = BacklogStatus.WritingMessage;
1183-
threadSafePhysicalConnectionAccessor.WorkOnPhysicalWithLock(physical =>
1183+
threadSafePhysicalConnectionAccessor.UsePhysicalWithLock(physical =>
11841184
{
11851185
var result = WriteMessageInsideLock(physical, message);
11861186
if (result == WriteResult.Success)

src/StackExchange.Redis/ThreadSafePhysicalConnectionAccessor.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ internal void Shutdown()
132132
}
133133
}
134134

135+
internal bool UsePhysicalWithLock(Action<PhysicalConnection> callBack)
136+
{
137+
try
138+
{
139+
rwLock.EnterReadLock();
140+
if (physical != null)
141+
{
142+
callBack(physical);
143+
return true;
144+
}
145+
return false;
146+
}
147+
finally
148+
{
149+
rwLock.ExitReadLock();
150+
}
151+
}
152+
135153
internal bool WorkOnPhysicalWithLock(Action<PhysicalConnection> callBack)
136154
{
137155
try
@@ -154,25 +172,25 @@ internal void SimulateConnectionFailure(SimulatedFailureType failureType)
154172
{
155173
try
156174
{
157-
rwLock.EnterWriteLock();
175+
rwLock.EnterReadLock();
158176
physical?.SimulateConnectionFailure(failureType);
159177
}
160178
finally
161179
{
162-
rwLock.ExitWriteLock();
180+
rwLock.ExitReadLock();
163181
}
164182
}
165183

166184
internal void SetIdle()
167185
{
168186
try
169187
{
170-
rwLock.EnterWriteLock();
188+
rwLock.EnterReadLock();
171189
physical?.SetIdle();
172190
}
173191
finally
174192
{
175-
rwLock.ExitWriteLock();
193+
rwLock.ExitReadLock();
176194
}
177195
}
178196

0 commit comments

Comments
 (0)