Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 55e4a09

Browse files
Aaron BoxerAaron Boxer
authored andcommitted
after last refactor, lock key must be passed into unlock method in distributed lock
1 parent 5c2a3e5 commit 55e4a09

File tree

5 files changed

+14
-21
lines changed

5 files changed

+14
-21
lines changed

src/ServiceStack.Redis/Support/Locking/DisposableDistributedLock.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class DisposableDistributedLock : IDisposable
1111
private readonly long lockState;
1212
private readonly long lockExpire;
1313
private readonly IRedisClient myClient;
14+
private readonly string globalLockKey;
1415

1516
/// <summary>
1617
/// Lock
@@ -23,6 +24,7 @@ public DisposableDistributedLock(IRedisClient client, string globalLockKey, int
2324
{
2425
myLock = new DistributedLock();
2526
myClient = client;
27+
this.globalLockKey = globalLockKey;
2628
lockState = myLock.Lock(globalLockKey, acquisitionTimeout, lockTimeout, out lockExpire, myClient);
2729
}
2830

@@ -43,7 +45,7 @@ public long LockExpire
4345
/// </summary>
4446
public void Dispose()
4547
{
46-
myLock.Unlock(lockExpire, myClient);
48+
myLock.Unlock(globalLockKey, lockExpire, myClient);
4749
}
4850
}
4951
}

src/ServiceStack.Redis/Support/Locking/DistributedLock.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ public class DistributedLock : IDistributedLock
99
public const int LOCK_ACQUIRED = 1;
1010
public const int LOCK_RECOVERED = 2;
1111

12-
protected string lockKey;
13-
14-
1512
/// <summary>
1613
/// acquire distributed, non-reentrant lock on key
1714
/// </summary>
@@ -26,10 +23,6 @@ public virtual long Lock(string key, int acquisitionTimeout, int lockTimeout, ou
2623

2724
// cannot lock on a null key
2825
if (key == null)
29-
return LOCK_NOT_ACQUIRED;
30-
31-
// cannot re-enter a lock
32-
if (lockKey != null)
3326
return LOCK_NOT_ACQUIRED;
3427

3528
const int sleepIfLockSet = 200;
@@ -91,8 +84,6 @@ public virtual long Lock(string key, int acquisitionTimeout, int lockTimeout, ou
9184
if (wasSet != LOCK_NOT_ACQUIRED)
9285
{
9386
lockExpire = newLockExpire;
94-
lockKey = key;
95-
9687
}
9788
return wasSet;
9889

@@ -102,7 +93,7 @@ public virtual long Lock(string key, int acquisitionTimeout, int lockTimeout, ou
10293
/// <summary>
10394
/// unlock key
10495
/// </summary>
105-
public virtual bool Unlock(long lockExpire, IRedisClient client)
96+
public virtual bool Unlock(string key, long lockExpire, IRedisClient client)
10697
{
10798
if (lockExpire <= 0)
10899
return false;
@@ -111,28 +102,28 @@ public virtual bool Unlock(long lockExpire, IRedisClient client)
111102
using (var pipe = localClient.CreatePipeline())
112103
{
113104

114-
pipe.QueueCommand(r => ((RedisNativeClient) r).Watch(lockKey));
115-
pipe.QueueCommand(r => ((RedisNativeClient) r).Get(lockKey),
105+
pipe.QueueCommand(r => ((RedisNativeClient) r).Watch(key));
106+
pipe.QueueCommand(r => ((RedisNativeClient) r).Get(key),
116107
x => lockVal = (x != null) ? BitConverter.ToInt64(x, 0) : 0);
117108
pipe.Flush();
118109
}
119110

120111
if (lockVal != lockExpire)
121112
{
122113
if (lockVal != 0)
123-
Debug.WriteLine(String.Format("Unlock(): Failed to unlock key {0}; lock has been acquired by another client ", lockKey));
114+
Debug.WriteLine(String.Format("Unlock(): Failed to unlock key {0}; lock has been acquired by another client ", key));
124115
else
125-
Debug.WriteLine(String.Format("Unlock(): Failed to unlock key {0}; lock has been identifed as a zombie and harvested ", lockKey));
116+
Debug.WriteLine(String.Format("Unlock(): Failed to unlock key {0}; lock has been identifed as a zombie and harvested ", key));
126117
localClient.UnWatch();
127118
return false;
128119
}
129120

130121
using (var trans = localClient.CreateTransaction())
131122
{
132-
trans.QueueCommand(r => ((RedisNativeClient)r).Del(lockKey));
123+
trans.QueueCommand(r => ((RedisNativeClient)r).Del(key));
133124
var rc = trans.Commit();
134125
if (!rc)
135-
Debug.WriteLine(String.Format("Unlock(): Failed to delete key {0}; lock has been acquired by another client ", lockKey));
126+
Debug.WriteLine(String.Format("Unlock(): Failed to delete key {0}; lock has been acquired by another client ", key));
136127
return rc;
137128
}
138129

src/ServiceStack.Redis/Support/Locking/IDistributedLock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
public interface IDistributedLock
77
{
88
long Lock(string key, int acquisitionTimeout, int lockTimeout, out long lockExpire, IRedisClient client);
9-
bool Unlock(long lockExpire, IRedisClient client);
9+
bool Unlock(string key, long lockExpire, IRedisClient client);
1010
}
1111
}

src/ServiceStack.Redis/Support/Queue/Implementation/RedisSequentialWorkQueue.Locks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public bool PopAndUnlock(int numProcessed, IRedisClient client)
8282

8383
// unlock work queue id
8484
workQueue.Unlock(workItemId);
85-
return myLock.Unlock(lockExpire, client);
85+
return myLock.Unlock(dequeueLockKey, lockExpire, client);
8686
}
8787

8888
/// <summary>

tests/ServiceStack.Redis.Tests/RedisClientTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,15 @@ public void Can_create_distributed_lock()
375375
Assert.AreEqual(distributedLock.Lock(key, lockTimeout, lockTimeout, out lockExpire, Redis), DistributedLock.LOCK_RECOVERED);
376376

377377

378-
Assert.IsTrue(distributedLock.Unlock(lockExpire, Redis));
378+
Assert.IsTrue(distributedLock.Unlock(key, lockExpire, Redis));
379379

380380
//can now lock
381381
distributedLock = new DistributedLock();
382382
Assert.AreEqual(distributedLock.Lock(key, lockTimeout, lockTimeout, out lockExpire, Redis), DistributedLock.LOCK_ACQUIRED);
383383

384384

385385
//cleanup
386-
Assert.IsTrue(distributedLock.Unlock(lockExpire, Redis));
386+
Assert.IsTrue(distributedLock.Unlock(key, lockExpire, Redis));
387387

388388

389389
}

0 commit comments

Comments
 (0)