diff --git a/etcd/Client.hpp b/etcd/Client.hpp index 5bf73be..6f72144 100644 --- a/etcd/Client.hpp +++ b/etcd/Client.hpp @@ -269,12 +269,20 @@ namespace etcd pplx::task leasetimetolive(int64_t lease_id); /** - * Gains a lock at a key, using a default created lease, using the default lease (60 seconds), with + * Gains a lock at a key, using a default created lease, using the default lease (10 seconds), with * keeping alive has already been taken care of by the library. * @param key is the key to be used to request the lock. */ pplx::task lock(std::string const &key); + /** + * Gains a lock at a key, using a default created lease, using the specified lease TTL (in seconds), with + * keeping alive has already been taken care of by the library. + * @param key is the key to be used to request the lock. + * @param lease_ttl is the TTL used to create a lease for the key. + */ + pplx::task lock(std::string const &key, int lease_ttl); + /** * Gains a lock at a key, using a user-provided lease, the lifetime of the lease won't be taken care * of by the library. diff --git a/src/Client.cpp b/src/Client.cpp index 6fb131d..c62218b 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -598,20 +598,23 @@ pplx::task etcd::Client::leasetimetolive(int64_t lease_id) } pplx::task etcd::Client::lock(std::string const &key) { - etcdv3::ActionParameters params; - params.auth_token.assign(this->auth_token); - - static const int DEFAULT_LEASE_TTL_FOR_LOCK = 10; - // routines in lock usually will be fast, less than 10 seconds. // // (base on our experiences in vineyard and GraphScope). - auto resp = this->leasegrant(DEFAULT_LEASE_TTL_FOR_LOCK).get(); + static const int DEFAULT_LEASE_TTL_FOR_LOCK = 10; + return this->lock(key, DEFAULT_LEASE_TTL_FOR_LOCK); +} + +pplx::task etcd::Client::lock(std::string const &key, int lease_ttl) { + etcdv3::ActionParameters params; + params.auth_token.assign(this->auth_token); + + auto resp = this->leasegrant(lease_ttl).get(); int64_t lease_id = resp.value().lease(); { std::lock_guard lexical_scope_lock(mutex_for_keepalives); this->keep_alive_for_locks[lease_id].reset( - new KeepAlive(*this, DEFAULT_LEASE_TTL_FOR_LOCK, lease_id)); + new KeepAlive(*this, lease_ttl, lease_id)); } params.key = key; params.lease_id = lease_id;