Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IRedisLock.ThrownExceptions #27

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions RedLock.Tests/RedisLockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,5 +466,19 @@ public void TimeLock()
}
}
}

[Test]
public void TestThrownExceptions()
{
using (var redisLockFactory = new RedisLockFactory(AllInactiveEndPoints.First()))
{
var resource = $"testredislock-{Guid.NewGuid()}";

using (var redisLock = redisLockFactory.Create(resource, TimeSpan.MaxValue))
{
Assert.IsTrue(redisLock.HasError());
}
}
}
}
}
3 changes: 2 additions & 1 deletion RedLock/IRedisLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public interface IRedisLock : IDisposable
string LockId { get; }
bool IsAcquired { get; }
int ExtendCount { get; }
}
System.Collections.Generic.IEnumerable<Exception> ThrownExceptions { get; }
}
}
16 changes: 16 additions & 0 deletions RedLock/IRedisLockExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedLock
{
public static class IRedisLockExtensions
{
public static bool HasError(this IRedisLock redisLock)
{
return redisLock.ThrownExceptions.Any();
}
}
}
1 change: 1 addition & 0 deletions RedLock/RedLock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<ItemGroup>
<Compile Include="App_Packages\LibLog.4.2\LibLog.cs" />
<Compile Include="IRedisLock.cs" />
<Compile Include="IRedisLockExtensions.cs" />
<Compile Include="IRedisLockFactory.cs" />
<Compile Include="RedisConnection.cs" />
<Compile Include="RedisLockEndPoint.cs" />
Expand Down
27 changes: 23 additions & 4 deletions RedLock/RedisLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class RedisLock : IRedisLock
private readonly TimeSpan minimumExpiryTime = TimeSpan.FromMilliseconds(10);
private readonly TimeSpan minimumRetryTime = TimeSpan.FromMilliseconds(10);

private List<Exception> thrownExceptions;
public IEnumerable<Exception> ThrownExceptions { get { return thrownExceptions ?? Enumerable.Empty<Exception>(); } }
private readonly object thrownExceptionsLock = new object();

private RedisLock(
ICollection<RedisConnection> redisCaches,
string resource,
Expand Down Expand Up @@ -408,6 +412,7 @@ private bool LockInstance(RedisConnection cache)
catch (Exception ex)
{
Logger.Debug($"Error locking lock instance {host}: {ex.Message}");
RecordException(ex);
}

Logger.Trace(() => $"LockInstance exit {host}: {redisKey}, {LockId}, {result}");
Expand All @@ -433,7 +438,8 @@ private async Task<bool> LockInstanceAsync(RedisConnection cache)
catch (Exception ex)
{
Logger.Debug($"Error locking lock instance {host}: {ex.Message}");
}
RecordException(ex);
}

Logger.Trace(() => $"LockInstanceAsync exit {host}: {redisKey}, {LockId}, {result}");

Expand All @@ -459,7 +465,8 @@ private bool ExtendInstance(RedisConnection cache)
catch (Exception ex)
{
Logger.Debug($"Error extending lock instance {host}: {ex.Message}");
}
RecordException(ex);
}

Logger.Trace(() => $"ExtendInstance exit {host}: {redisKey}, {LockId}, {result}");

Expand All @@ -483,7 +490,8 @@ private bool UnlockInstance(RedisConnection cache)
catch (Exception ex)
{
Logger.Debug($"Error unlocking lock instance {host}: {ex.Message}");
}
RecordException(ex);
}

Logger.Trace(() => $"UnlockInstance exit {host}: {redisKey}, {LockId}, {result}");

Expand All @@ -508,13 +516,24 @@ private async Task<bool> UnlockInstanceAsync(RedisConnection cache)
catch (Exception ex)
{
Logger.Debug($"Error unlocking lock instance {host}: {ex.Message}");
}
RecordException(ex);
}

Logger.Trace(() => $"UnlockInstanceAsync exit {host}: {redisKey}, {LockId}, {result}");

return result;
}

private void RecordException(Exception ex)
{
lock(thrownExceptionsLock)
{
if (thrownExceptions == null)
thrownExceptions = new List<Exception>();
thrownExceptions.Add(ex);
}
}

private static string GetRedisKey(string redisKeyFormat, string resource)
{
return string.Format(redisKeyFormat, resource);
Expand Down