Пример #1
0
        static int Main(string[] args)
        {
            var         type   = args[0];
            var         name   = args[1];
            IDisposable handle = null;

            switch (type)
            {
            case "sql":
                handle = new SqlDistributedLock(name, ConnectionString).Acquire();
                break;

            case "sqlreaderwriterlock":
                handle = new SqlDistributedReaderWriterLock(name, ConnectionString).AcquireWriteLock();
                break;

            case "system":
                handle = new SystemDistributedLock(name).Acquire();
                break;

            default:
                return(123);
            }

            if (Console.ReadLine() != "abandon")
            {
                handle.Dispose();
            }

            return(0);
        }
    public async Task <ILock> LockAsync(string ck, int ttl = 20000, int retry = 20, int retryDelay = 1000)
    {
        var l   = new SystemDistributedLock(ck, TimeSpan.FromMilliseconds(retryDelay));
        var lck = new Lock(ck, l, ttl, TimeSpan.FromMilliseconds(retry * retryDelay));
        await lck.Acquire();

        return(lck);
    }
 internal Lock(string ck, SystemDistributedLock lck, int ttl, TimeSpan ts)
 {
     this.CK  = ck;
     this.Ttl = ttl;
     ID       = Guid.NewGuid().ToString("n");
     this.lck = lck;
     this.ts  = ts;
 }
 public void TestGetSafeLockNameCompat()
 {
     SystemDistributedLock.GetSafeLockName("").ShouldEqual("EMPTYz4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==");
     SystemDistributedLock.GetSafeLockName("abc").ShouldEqual("abc");
     SystemDistributedLock.GetSafeLockName("\\").ShouldEqual("_CgzRFsLFf7El/ZraEx9sqWRYeplYohSBSmI9sYIe1c4y2u7ECFoU4x2QCjV7HiVJMZsuDMLIz7r8akpKr+viAw==");
     SystemDistributedLock.GetSafeLockName(new string('a', SystemDistributedLock.MaxLockNameLength)).ShouldEqual(new string('a', SystemDistributedLock.MaxLockNameLength));
     SystemDistributedLock.GetSafeLockName(new string('\\', SystemDistributedLock.MaxLockNameLength)).ShouldEqual("_____________________________________________________________________________________________________________________________________________________________________Y7DJXlpJeJjeX5XAOWV+ka/3ONBj5dHhKWcSH4pd5AC9YHFm+l1gBArGpBSBn3WcX00ArcDtKw7g24kJaHLifQ==");
     SystemDistributedLock.GetSafeLockName(new string('x', SystemDistributedLock.MaxLockNameLength + 1)).ShouldEqual("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsrCnXZ1XHiT//dOSBfAU0iC4Gtnlr0dQACBUK8Ev2OdRYJ9jcvbiqVCv/rjyPemTW9AvOonkdr0B2bG04gmeYA==");
 }
Пример #5
0
        public void Build <T>(Guid id, Func <Guid, T> initSnap, params Action <T>[] relateBuildsAction) where T : Aggregate
        {
            var myLock = new SystemDistributedLock(SystemDistributedLock.GetSafeLockName($"Game{id}"));

            using (myLock.Acquire())
            {
                PrivateBuild(id, initSnap, relateBuildsAction);
            }
        }
Пример #6
0
        public static int Main(string[] args)
        {
            var         type = args[0];
            var         name = args[1];
            IDisposable?handle;

            switch (type)
            {
            case "SqlDistributedLock":
                handle = new SqlDistributedLock(name, ConnectionString).Acquire();
                break;

            case "SqlReaderWriterLockDistributedLock":
                handle = new SqlDistributedReaderWriterLock(name, ConnectionString).AcquireWriteLock();
                break;

            case "SqlSemaphoreDistributedLock":
                handle = new SqlDistributedSemaphore(name, maxCount: 1, connectionString: ConnectionString).Acquire();
                break;

            case "SqlSemaphoreDistributedLock5":
                handle = new SqlDistributedSemaphore(name, maxCount: 5, connectionString: ConnectionString).Acquire();
                break;

            case "SystemDistributedLock":
                handle = new SystemDistributedLock(name).Acquire();
                break;

            default:
                return(123);
            }

            Console.WriteLine("Acquired");
            Console.Out.Flush();

            if (Console.ReadLine() != "abandon")
            {
                handle.Dispose();
            }

            return(0);
        }
 internal override string GetSafeLockName(string name) => SystemDistributedLock.GetSafeLockName(name);
 internal override string GetSafeLockName(string name)
 {
     return(SystemDistributedLock.GetSafeLockName(name));
 }