in src/lock.ts [73:95]
public acquire(): Promise<this> {
const lease = (this.lease = new Lease(this.pool, this.namespace, this.leaseTTL));
const kv = new RPC.KVClient(this.pool);
return lease.grant().then(leaseID => {
return new ComparatorBuilder(kv, this.namespace)
.and(this.key, 'Create', '==', 0)
.then(new PutBuilder(kv, this.namespace, this.key).value('').lease(leaseID))
.options(this.callOptions)
.commit()
.then<this>(res => {
if (res.succeeded) {
return this;
}
return this.release()
.catch(() => undefined)
.then(() => {
throw new EtcdLockFailedError(`Failed to acquire a lock on ${this.key}`);
});
});
});
}