123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System.Collections.Concurrent;
- using WingServerCommon.Log;
- namespace WingServerCommon.Utilities
- {
- public class LockManager
- {
- private bool _isDistributed = false;
- private static object _acquireLocker = new object();
- private ConcurrentDictionary<string, ILocker> _lockers = new ConcurrentDictionary<string, ILocker>();
- public LockManager()
- {
- _isDistributed = false;
- Logger.WriteLineInfo($"LockManager init success, mode: Alone");
- }
- public LockManager(Func<string, bool> OnGetLock, Func<string, bool> OnReleaseLock)
- {
- _isDistributed = true;
- DistributedLock.OnGetLock = OnGetLock;
- DistributedLock.OnReleaseLock = OnReleaseLock;
- Logger.WriteLineInfo($"LockManager init success, mode: Distributed");
- }
- public async Task<ILocker> Acquire(string lockKey)
- {
- Logger.WriteLineInfo($"LockManager Acquire locker by key:{lockKey}");
- ILocker locker;
- if (!_lockers.TryGetValue(lockKey, out locker))
- {
- lock (_acquireLocker)
- {
- locker = _lockers.GetOrAdd(lockKey, (k) =>
- {
- if (_isDistributed)
- {
- return new DistributedLock(lockKey);
- }
- else
- {
- return new StandAloneLock(lockKey);
- }
- });
- }
- }
- return await locker.LockAsync();
- }
- public void Release(string lockKey)
- {
- Logger.WriteLineInfo($"LockManager Release locker by key:{lockKey}");
- if (_lockers.TryGetValue(lockKey, out ILocker locker))
- {
- locker.Dispose();
- }
- }
- }
- public interface ILocker : IDisposable
- {
- Task<ILocker> LockAsync();
- }
- public class StandAloneLock : ILocker
- {
- SemaphoreSlim slimlock = new SemaphoreSlim(1, 1);
- private string _lockKey;
- private string _serialNumber;
- public StandAloneLock(string guid)
- {
- _lockKey = guid;
- }
- public async Task<ILocker> LockAsync()
- {
- await slimlock.WaitAsync(3 * 1000);
- _serialNumber = Guid.NewGuid().ToString("N");
- Logger.WriteLineInfo($"LockManager StandAloneLock waitasync end, lockkey:{_lockKey}, serialNumber:{_serialNumber}");
- return this;
- }
- public void Dispose()
- {
- slimlock.Release();
- Logger.WriteLineInfo($"LockManager StandAloneLock release end, lockkey:{_lockKey}, serialNumber:{_serialNumber}");
- }
- }
- public class DistributedLock : ILocker
- {
- public static Func<string, bool> OnGetLock;
- public static Func<string, bool> OnReleaseLock;
- private string _lockKey;
- private string _serialNumber;
- public DistributedLock(string guid)
- {
- _lockKey = guid;
- }
- public async Task<ILocker> LockAsync()
- {
- OnGetLock.Invoke(_lockKey);
- _serialNumber = Guid.NewGuid().ToString("N");
- Logger.WriteLineInfo($"LockManager DistributedLock waitasync end, lockkey:{_lockKey}, serialNumber:{_serialNumber}");
- return this;
- }
- public void Dispose()
- {
- OnReleaseLock.Invoke(_lockKey);
- Logger.WriteLineInfo($"LockManager DistributedLock release end, lockkey:{_lockKey}, serialNumber:{_serialNumber}");
- }
- }
- }
|