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

Commit a05b5f9

Browse files
committed
fix whitespace + C#7-ify locking impls
1 parent 2a7e1cd commit a05b5f9

File tree

9 files changed

+159
-175
lines changed

9 files changed

+159
-175
lines changed

src/ServiceStack.Redis/RedisLock.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public RedisLock(RedisClient redisClient, string key, TimeSpan? timeOut)
2121
//This pattern is taken from the redis command for SETNX http://redis.io/commands/setnx
2222

2323
//Calculate a unix time for when the lock should expire
24-
TimeSpan realSpan = timeOut ?? new TimeSpan(365, 0, 0, 0); //if nothing is passed in the timeout hold for a year
25-
DateTime expireTime = DateTime.UtcNow.Add(realSpan);
26-
string lockString = (expireTime.ToUnixTimeMs() + 1).ToString();
24+
var realSpan = timeOut ?? new TimeSpan(365, 0, 0, 0); //if nothing is passed in the timeout hold for a year
25+
var expireTime = DateTime.UtcNow.Add(realSpan);
26+
var lockString = (expireTime.ToUnixTimeMs() + 1).ToString();
2727

2828
//Try to set the lock, if it does not exist this will succeed and the lock is obtained
2929
var nx = redisClient.SetValueIfNotExists(key, lockString);
@@ -35,9 +35,8 @@ public RedisLock(RedisClient redisClient, string key, TimeSpan? timeOut)
3535
//Therefore we need to get the value of the lock to see when it should expire
3636

3737
redisClient.Watch(key);
38-
string lockExpireString = redisClient.Get<string>(key);
39-
long lockExpireTime;
40-
if (!long.TryParse(lockExpireString, out lockExpireTime))
38+
var lockExpireString = redisClient.Get<string>(key);
39+
if (!long.TryParse(lockExpireString, out var lockExpireTime))
4140
{
4241
redisClient.UnWatch(); // since the client is scoped externally
4342
return false;

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace ServiceStack.Redis.Support.Locking
88
public class DisposableDistributedLock : IDisposable
99
{
1010
private readonly IDistributedLock myLock;
11-
private readonly long lockState;
1211
private readonly long lockExpire;
1312
private readonly IRedisClient myClient;
1413
private readonly string globalLockKey;
@@ -25,20 +24,12 @@ public DisposableDistributedLock(IRedisClient client, string globalLockKey, int
2524
myLock = new DistributedLock();
2625
myClient = client;
2726
this.globalLockKey = globalLockKey;
28-
lockState = myLock.Lock(globalLockKey, acquisitionTimeout, lockTimeout, out lockExpire, myClient);
27+
LockState = myLock.Lock(globalLockKey, acquisitionTimeout, lockTimeout, out lockExpire, myClient);
2928
}
3029

30+
public long LockState { get; }
3131

32-
public long LockState
33-
{
34-
get { return lockState; }
35-
}
36-
37-
public long LockExpire
38-
{
39-
get { return lockExpire; }
40-
}
41-
32+
public long LockExpire => lockExpire;
4233

4334
/// <summary>
4435
/// unlock

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

Lines changed: 75 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,131 +4,127 @@
44
namespace ServiceStack.Redis.Support.Locking
55
{
66
public class DistributedLock : IDistributedLock
7-
{
7+
{
88
public const int LOCK_NOT_ACQUIRED = 0;
99
public const int LOCK_ACQUIRED = 1;
1010
public const int LOCK_RECOVERED = 2;
1111

12-
/// <summary>
13-
/// acquire distributed, non-reentrant lock on key
14-
/// </summary>
15-
/// <param name="key">global key for this lock</param>
16-
/// <param name="acquisitionTimeout">timeout for acquiring lock</param>
17-
/// <param name="lockTimeout">timeout for lock, in seconds (stored as value against lock key) </param>
12+
/// <summary>
13+
/// acquire distributed, non-reentrant lock on key
14+
/// </summary>
15+
/// <param name="key">global key for this lock</param>
16+
/// <param name="acquisitionTimeout">timeout for acquiring lock</param>
17+
/// <param name="lockTimeout">timeout for lock, in seconds (stored as value against lock key) </param>
1818
/// <param name="client"></param>
1919
/// <param name="lockExpire"></param>
2020
public virtual long Lock(string key, int acquisitionTimeout, int lockTimeout, out long lockExpire, IRedisClient client)
21-
{
22-
lockExpire = 0;
21+
{
22+
lockExpire = 0;
2323

2424
// cannot lock on a null key
2525
if (key == null)
2626
return LOCK_NOT_ACQUIRED;
2727

28-
const int sleepIfLockSet = 200;
29-
acquisitionTimeout *= 1000; //convert to ms
30-
int tryCount = (acquisitionTimeout / sleepIfLockSet) + 1;
28+
const int sleepIfLockSet = 200;
29+
acquisitionTimeout *= 1000; //convert to ms
30+
int tryCount = (acquisitionTimeout / sleepIfLockSet) + 1;
3131

32-
var ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
33-
var newLockExpire = CalculateLockExpire(ts, lockTimeout);
32+
var ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
33+
var newLockExpire = CalculateLockExpire(ts, lockTimeout);
3434

3535
var localClient = (RedisClient)client;
3636
long wasSet = localClient.SetNX(key, BitConverter.GetBytes(newLockExpire));
37-
int totalTime = 0;
37+
int totalTime = 0;
3838
while (wasSet == LOCK_NOT_ACQUIRED && totalTime < acquisitionTimeout)
39-
{
40-
int count = 0;
41-
while (wasSet == 0 && count < tryCount && totalTime < acquisitionTimeout)
42-
{
43-
TaskUtils.Sleep(sleepIfLockSet);
44-
totalTime += sleepIfLockSet;
45-
ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
46-
newLockExpire = CalculateLockExpire(ts, lockTimeout);
39+
{
40+
int count = 0;
41+
while (wasSet == 0 && count < tryCount && totalTime < acquisitionTimeout)
42+
{
43+
TaskUtils.Sleep(sleepIfLockSet);
44+
totalTime += sleepIfLockSet;
45+
ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
46+
newLockExpire = CalculateLockExpire(ts, lockTimeout);
4747
wasSet = localClient.SetNX(key, BitConverter.GetBytes(newLockExpire));
48-
count++;
49-
}
50-
// acquired lock!
48+
count++;
49+
}
50+
// acquired lock!
5151
if (wasSet != LOCK_NOT_ACQUIRED) break;
5252

53-
// handle possibliity of crashed client still holding the lock
53+
// handle possibliity of crashed client still holding the lock
5454
using (var pipe = localClient.CreatePipeline())
55-
{
56-
long lockValue=0;
57-
pipe.QueueCommand(r => ((RedisNativeClient)r).Watch(key));
58-
pipe.QueueCommand(r => ((RedisNativeClient)r).Get(key), x => lockValue = (x != null) ? BitConverter.ToInt64(x,0) : 0);
59-
pipe.Flush();
55+
{
56+
long lockValue = 0;
57+
pipe.QueueCommand(r => ((RedisNativeClient)r).Watch(key));
58+
pipe.QueueCommand(r => ((RedisNativeClient)r).Get(key), x => lockValue = (x != null) ? BitConverter.ToInt64(x, 0) : 0);
59+
pipe.Flush();
6060

61-
// if lock value is 0 (key is empty), or expired, then we can try to acquire it
61+
// if lock value is 0 (key is empty), or expired, then we can try to acquire it
6262
ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
63-
if (lockValue < ts.TotalSeconds)
64-
{
65-
ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
66-
newLockExpire = CalculateLockExpire(ts, lockTimeout);
67-
using (var trans = localClient.CreateTransaction())
68-
{
69-
var expire = newLockExpire;
70-
trans.QueueCommand(r => ((RedisNativeClient)r).Set(key, BitConverter.GetBytes(expire)));
71-
if (trans.Commit())
72-
wasSet = LOCK_RECOVERED; //recovered lock!
73-
}
74-
}
75-
else
76-
{
63+
if (lockValue < ts.TotalSeconds)
64+
{
65+
ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
66+
newLockExpire = CalculateLockExpire(ts, lockTimeout);
67+
using (var trans = localClient.CreateTransaction())
68+
{
69+
var expire = newLockExpire;
70+
trans.QueueCommand(r => ((RedisNativeClient)r).Set(key, BitConverter.GetBytes(expire)));
71+
if (trans.Commit())
72+
wasSet = LOCK_RECOVERED; //recovered lock!
73+
}
74+
}
75+
else
76+
{
7777
localClient.UnWatch();
78-
}
79-
}
78+
}
79+
}
8080
if (wasSet != LOCK_NOT_ACQUIRED) break;
81-
TaskUtils.Sleep(sleepIfLockSet);
82-
totalTime += sleepIfLockSet;
83-
}
81+
TaskUtils.Sleep(sleepIfLockSet);
82+
totalTime += sleepIfLockSet;
83+
}
8484
if (wasSet != LOCK_NOT_ACQUIRED)
8585
{
8686
lockExpire = newLockExpire;
8787
}
88-
return wasSet;
89-
90-
}
88+
return wasSet;
89+
}
9190

92-
93-
/// <summary>
94-
/// unlock key
95-
/// </summary>
96-
public virtual bool Unlock(string key, long lockExpire, IRedisClient client)
97-
{
98-
if (lockExpire <= 0)
99-
return false;
100-
long lockVal = 0;
91+
/// <summary>
92+
/// unlock key
93+
/// </summary>
94+
public virtual bool Unlock(string key, long lockExpire, IRedisClient client)
95+
{
96+
if (lockExpire <= 0)
97+
return false;
98+
long lockVal = 0;
10199
var localClient = (RedisClient)client;
102100
using (var pipe = localClient.CreatePipeline())
103101
{
104-
105-
pipe.QueueCommand(r => ((RedisNativeClient) r).Watch(key));
106-
pipe.QueueCommand(r => ((RedisNativeClient) r).Get(key),
102+
103+
pipe.QueueCommand(r => ((RedisNativeClient)r).Watch(key));
104+
pipe.QueueCommand(r => ((RedisNativeClient)r).Get(key),
107105
x => lockVal = (x != null) ? BitConverter.ToInt64(x, 0) : 0);
108106
pipe.Flush();
109107
}
110108

111-
if (lockVal != lockExpire)
112-
{
109+
if (lockVal != lockExpire)
110+
{
113111
if (lockVal != 0)
114-
Debug.WriteLine(String.Format("Unlock(): Failed to unlock key {0}; lock has been acquired by another client ", key));
112+
Debug.WriteLine($"Unlock(): Failed to unlock key {key}; lock has been acquired by another client ");
115113
else
116-
Debug.WriteLine(String.Format("Unlock(): Failed to unlock key {0}; lock has been identifed as a zombie and harvested ", key));
114+
Debug.WriteLine($"Unlock(): Failed to unlock key {key}; lock has been identifed as a zombie and harvested ");
117115
localClient.UnWatch();
118-
return false;
119-
}
116+
return false;
117+
}
120118

121119
using (var trans = localClient.CreateTransaction())
122120
{
123121
trans.QueueCommand(r => ((RedisNativeClient)r).Del(key));
124-
var rc = trans.Commit();
122+
var rc = trans.Commit();
125123
if (!rc)
126-
Debug.WriteLine(String.Format("Unlock(): Failed to delete key {0}; lock has been acquired by another client ", key));
124+
Debug.WriteLine($"Unlock(): Failed to delete key {key}; lock has been acquired by another client ");
127125
return rc;
128126
}
129-
130-
}
131-
127+
}
132128

133129
/// <summary>
134130
///
@@ -140,6 +136,5 @@ private static long CalculateLockExpire(TimeSpan ts, int timeout)
140136
{
141137
return (long)(ts.TotalSeconds + timeout + 1.5);
142138
}
143-
144-
}
139+
}
145140
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
/// Distributed lock interface
55
/// </summary>
66
public interface IDistributedLock
7-
{
8-
long Lock(string key, int acquisitionTimeout, int lockTimeout, out long lockExpire, IRedisClient client);
9-
bool Unlock(string key, long lockExpire, IRedisClient client);
10-
}
7+
{
8+
long Lock(string key, int acquisitionTimeout, int lockTimeout, out long lockExpire, IRedisClient client);
9+
bool Unlock(string key, long lockExpire, IRedisClient client);
10+
}
1111
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace ServiceStack.Redis.Support.Locking
66
/// Locking strategy interface
77
/// </summary>
88
public interface ILockingStrategy
9-
{
10-
IDisposable ReadLock();
9+
{
10+
IDisposable ReadLock();
1111

12-
IDisposable WriteLock();
13-
}
12+
IDisposable WriteLock();
13+
}
1414
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
namespace ServiceStack.Redis.Support.Locking
44
{
5-
public class NoLockingStrategy : ILockingStrategy
6-
{
7-
public IDisposable ReadLock()
8-
{
9-
return null;
10-
}
5+
public class NoLockingStrategy : ILockingStrategy
6+
{
7+
public IDisposable ReadLock()
8+
{
9+
return null;
10+
}
1111

12-
public IDisposable WriteLock()
13-
{
14-
return null;
15-
}
16-
}
12+
public IDisposable WriteLock()
13+
{
14+
return null;
15+
}
16+
}
1717
}

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33

44
namespace ServiceStack.Redis.Support.Locking
55
{
6-
/// <summary>
7-
/// This class manages a read lock for a local readers/writer lock,
8-
/// using the Resource Acquisition Is Initialization pattern
9-
/// </summary>
10-
public class ReadLock : IDisposable
11-
{
12-
private readonly ReaderWriterLockSlim lockObject;
6+
/// <summary>
7+
/// This class manages a read lock for a local readers/writer lock,
8+
/// using the Resource Acquisition Is Initialization pattern
9+
/// </summary>
10+
public class ReadLock : IDisposable
11+
{
12+
private readonly ReaderWriterLockSlim lockObject;
1313

14-
/// <summary>
15-
/// RAII initialization
16-
/// </summary>
17-
/// <param name="lockObject"></param>
18-
public ReadLock(ReaderWriterLockSlim lockObject)
19-
{
20-
this.lockObject = lockObject;
21-
lockObject.EnterReadLock();
22-
}
14+
/// <summary>
15+
/// RAII initialization
16+
/// </summary>
17+
/// <param name="lockObject"></param>
18+
public ReadLock(ReaderWriterLockSlim lockObject)
19+
{
20+
this.lockObject = lockObject;
21+
lockObject.EnterReadLock();
22+
}
2323

24-
/// <summary>
25-
/// RAII disposal
26-
/// </summary>
27-
public void Dispose()
28-
{
29-
lockObject.ExitReadLock();
30-
}
31-
}
24+
/// <summary>
25+
/// RAII disposal
26+
/// </summary>
27+
public void Dispose()
28+
{
29+
lockObject.ExitReadLock();
30+
}
31+
}
3232
}

0 commit comments

Comments
 (0)