|
| 1 | +using Medallion.Threading.Internal; |
| 2 | +using Medallion.Threading.Redis.RedLock; |
| 3 | +using StackExchange.Redis; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Medallion.Threading.Redis.Primitives |
| 11 | +{ |
| 12 | + internal class RedisReadLockPrimitive : IRedLockAcquirableSynchronizationPrimitive, IRedLockExtensibleSynchronizationPrimitive |
| 13 | + { |
| 14 | + private readonly RedisValue _lockId = RedLockHelper.CreateLockId(); |
| 15 | + private readonly RedisKey _readerKey, _writerKey; |
| 16 | + private readonly RedLockTimeouts _timeouts; |
| 17 | + |
| 18 | + public RedisReadLockPrimitive(RedisKey readerKey, RedisKey writerKey, RedLockTimeouts timeouts) |
| 19 | + { |
| 20 | + this._readerKey = readerKey; |
| 21 | + this._writerKey = writerKey; |
| 22 | + this._timeouts = timeouts; |
| 23 | + } |
| 24 | + |
| 25 | + public TimeoutValue AcquireTimeout => this._timeouts.AcquireTimeout; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// RELEASE READ |
| 29 | + /// |
| 30 | + /// Just remove our ID from the reader set (noop if it wasn't there or the set DNE) |
| 31 | + /// </summary> |
| 32 | + private static readonly RedisScript<RedisReadLockPrimitive> ReleaseReadScript = new RedisScript<RedisReadLockPrimitive>( |
| 33 | + @"redis.call('srem', @readerKey, @lockId)", |
| 34 | + p => new { readerKey = p._readerKey, lockId = p._lockId } |
| 35 | + ); |
| 36 | + |
| 37 | + public void Release(IDatabase database, bool fireAndForget) => ReleaseReadScript.Execute(database, this, fireAndForget); |
| 38 | + public Task ReleaseAsync(IDatabaseAsync database, bool fireAndForget) => ReleaseReadScript.ExecuteAsync(database, this, fireAndForget); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// TRY EXTEND READ |
| 42 | + /// |
| 43 | + /// First, check if the reader set exists and our ID is still a member. If not, we fail. |
| 44 | + /// |
| 45 | + /// Then, extend the reader set TTL to be at least our expiry (at least because other readers might be operating with a longer expiry) |
| 46 | + /// </summary> |
| 47 | + private static readonly RedisScript<RedisReadLockPrimitive> TryExtendReadScript = new RedisScript<RedisReadLockPrimitive>(@" |
| 48 | + if redis.call('sismember', @readerKey, @lockId) == 0 then |
| 49 | + return 0 |
| 50 | + end |
| 51 | + if readerTtl < @expiryMillis then |
| 52 | + redis.call('pexpire', @readerKey, @expiryMillis) |
| 53 | + end |
| 54 | + return 1", |
| 55 | + p => new { readerKey = p._readerKey, lockId = p._lockId, expiryMillis = p._timeouts.Expiry.InMilliseconds } |
| 56 | + ); |
| 57 | + |
| 58 | + public Task<bool> TryExtendAsync(IDatabaseAsync database) => TryExtendReadScript.ExecuteAsync(database, this).AsBooleanTask(); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// TRY ACQUIRE READ |
| 62 | + /// |
| 63 | + /// First, check the writer lock value: if it exists then we fail. |
| 64 | + /// |
| 65 | + /// Then, add our ID to the reader set, creating it if it does not exist. Then, extend the TTL |
| 66 | + /// of the reader set to be at least our expiry. Return success. |
| 67 | + /// </summary> |
| 68 | + private static readonly RedisScript<RedisReadLockPrimitive> TryAcquireReadScript = new RedisScript<RedisReadLockPrimitive>($@" |
| 69 | + if redis.call('exists', @writerKey) == 1 then |
| 70 | + return 0 |
| 71 | + end |
| 72 | + redis.call('sadd', @readerKey, @lockId) |
| 73 | + local readerTtl = redis.call('pttl', @readerKey) |
| 74 | + if readerTtl < tonumber(@expiryMillis) then |
| 75 | + redis.call('pexpire', @readerKey, @expiryMillis) |
| 76 | + end |
| 77 | + return 1", |
| 78 | + p => new { writerKey = p._writerKey, readerKey = p._readerKey, lockId = p._lockId, expiryMillis = p._timeouts.Expiry.InMilliseconds } |
| 79 | + ); |
| 80 | + |
| 81 | + public Task<bool> TryAcquireAsync(IDatabaseAsync database) => TryAcquireReadScript.ExecuteAsync(database, this).AsBooleanTask(); |
| 82 | + public bool TryAcquire(IDatabase database) => (bool)TryAcquireReadScript.Execute(database, this); |
| 83 | + } |
| 84 | + |
| 85 | + internal class RedisWriterWaitingPrimitive : RedisMutexPrimitive |
| 86 | + { |
| 87 | + public const string LockIdSuffix = "_WRITERWAITING"; |
| 88 | + |
| 89 | + public RedisWriterWaitingPrimitive(RedisKey writerKey, RedisValue baseLockId, RedLockTimeouts timeouts) |
| 90 | + : base(writerKey, baseLockId + LockIdSuffix, timeouts) |
| 91 | + { |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + internal class RedisWriteLockPrimitive : IRedLockAcquirableSynchronizationPrimitive, IRedLockExtensibleSynchronizationPrimitive |
| 96 | + { |
| 97 | + private readonly RedisKey _readerKey, _writerKey; |
| 98 | + private readonly RedisValue _lockId; |
| 99 | + private readonly RedLockTimeouts _timeouts; |
| 100 | + private readonly RedisMutexPrimitive _mutexPrimitive; |
| 101 | + |
| 102 | + public RedisWriteLockPrimitive( |
| 103 | + RedisKey readerKey, |
| 104 | + RedisKey writerKey, |
| 105 | + RedisValue lockId, |
| 106 | + RedLockTimeouts timeouts) |
| 107 | + { |
| 108 | + this._readerKey = readerKey; |
| 109 | + this._writerKey = writerKey; |
| 110 | + this._lockId = lockId; |
| 111 | + this._timeouts = timeouts; |
| 112 | + this._mutexPrimitive = new RedisMutexPrimitive(this._writerKey, this._lockId, this._timeouts); |
| 113 | + } |
| 114 | + |
| 115 | + public TimeoutValue AcquireTimeout => this._timeouts.AcquireTimeout; |
| 116 | + |
| 117 | + public void Release(IDatabase database, bool fireAndForget) => this._mutexPrimitive.Release(database, fireAndForget); |
| 118 | + public Task ReleaseAsync(IDatabaseAsync database, bool fireAndForget) => this._mutexPrimitive.ReleaseAsync(database, fireAndForget); |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// TRY ACQUIRE WRITE |
| 122 | + /// |
| 123 | + /// First, check if writerValue exists. If so, fail unless it's our waiting ID. |
| 124 | + /// |
| 125 | + /// Then, check if there are no readers. If so, then set writerValue to our ID and return success. If not, then if the lock |
| 126 | + /// has our waiting ID re-up the expiry (avoids the need to extend the writer waiting lock). |
| 127 | + /// |
| 128 | + /// Finally, return failure. |
| 129 | + /// </summary> |
| 130 | + private static readonly RedisScript<RedisWriteLockPrimitive> TryAcquireWriteScript = new RedisScript<RedisWriteLockPrimitive>($@" |
| 131 | + local writerValue = redis.call('get', @writerKey) |
| 132 | + if writerValue == false or writerValue == @lockId .. '{RedisWriterWaitingPrimitive.LockIdSuffix}' then |
| 133 | + if redis.call('scard', @readerKey) == 0 then |
| 134 | + redis.call('set', @writerKey, @lockId, 'px', @expiryMillis) |
| 135 | + return 1 |
| 136 | + end |
| 137 | + if writerValue ~= false then |
| 138 | + redis.call('pexpire', @writerKey, @expiryMillis) |
| 139 | + end |
| 140 | + end |
| 141 | + return 0", |
| 142 | + p => new { writerKey = p._writerKey, readerKey = p._readerKey, lockId = p._lockId, expiryMillis = p._timeouts.Expiry.InMilliseconds } |
| 143 | + ); |
| 144 | + |
| 145 | + public bool TryAcquire(IDatabase database) => (bool)TryAcquireWriteScript.Execute(database, this); |
| 146 | + public Task<bool> TryAcquireAsync(IDatabaseAsync database) => TryAcquireWriteScript.ExecuteAsync(database, this).AsBooleanTask(); |
| 147 | + |
| 148 | + public Task<bool> TryExtendAsync(IDatabaseAsync database) => this._mutexPrimitive.TryExtendAsync(database); |
| 149 | + } |
| 150 | +} |
0 commit comments