Allows specify a lease TTL for lock.

Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
This commit is contained in:
Tao He 2021-02-08 11:07:23 +08:00
parent 2aec773d72
commit c9f82b46d4
2 changed files with 19 additions and 8 deletions

View File

@ -269,12 +269,20 @@ namespace etcd
pplx::task<Response> leasetimetolive(int64_t lease_id); pplx::task<Response> 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. * keeping alive has already been taken care of by the library.
* @param key is the key to be used to request the lock. * @param key is the key to be used to request the lock.
*/ */
pplx::task<Response> lock(std::string const &key); pplx::task<Response> 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<Response> 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 * 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. * of by the library.

View File

@ -598,20 +598,23 @@ pplx::task<etcd::Response> etcd::Client::leasetimetolive(int64_t lease_id)
} }
pplx::task<etcd::Response> etcd::Client::lock(std::string const &key) { pplx::task<etcd::Response> 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. // routines in lock usually will be fast, less than 10 seconds.
// //
// (base on our experiences in vineyard and GraphScope). // (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::Response> 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(); int64_t lease_id = resp.value().lease();
{ {
std::lock_guard<std::mutex> lexical_scope_lock(mutex_for_keepalives); std::lock_guard<std::mutex> lexical_scope_lock(mutex_for_keepalives);
this->keep_alive_for_locks[lease_id].reset( 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.key = key;
params.lease_id = lease_id; params.lease_id = lease_id;