コード例 #1
0
        public string AttemptLock(TimeSpan?timeout, byte[] lockNodeBytes)
        {
            var startTime = DateTime.UtcNow;

            var retryCount = 0;

            string ourPath    = null;
            var    hasTheLock = false;
            var    isDone     = false;

            while (!isDone)
            {
                isDone = true;

                try
                {
                    ZKPaths.Mkdirs(Client, _path, false);
                    ourPath    = Client.Create(_path, lockNodeBytes, Driver.Acl ?? Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential);
                    hasTheLock = InternalLockLoop(startTime, timeout, ourPath);
                }
                catch (KeeperException.NoNodeException)
                {
                    // gets thrown by StandardLockInternalsDriver when it can't find the lock node
                    // this can happen when the session expires, etc. So, if the retry allows, just try it all again
                    if (RetryPolicy.AllowRetry(retryCount++, DateTime.UtcNow - startTime))
                    {
                        isDone = false;
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(hasTheLock ? ourPath : null);
        }