microsoft / etcd3

:bookmark: Node.js client for etcd3
https://microsoft.github.io/etcd3/classes/etcd3.html
Other
518 stars 73 forks source link

How can a let a lease to expire automatically? #69

Closed willgm closed 6 years ago

willgm commented 6 years ago

When I use the client.lease(ttl) api, it automatically keeps it alive before the TTL ends using an setTimeout. But what if I do want the leasing to end and automatically revoke all the keys attached to it? I can't find an options to make it happens.

connor4312 commented 6 years ago

Thanks for the request, this will be fixed in v0.2.10

gtamas commented 4 years ago

@connor4312

is this fixed now? I'm having a similar problem.

   const builder = ttl ?  this.client.lease(ttl) : this.client;
            return this.client
            .if(key, 'Create', '==', 0)
            .then(builder.put(key).value(value))
            .else(this.client.get(key))
            .commit();

So this code creates the lease. But unless I restart my app (or reload), the key won't be deleted. I mean, with TTlL=2 it just keeps the key alive and I can read it using etcdctl even after several seconds. However, as soon as I quit the app or reload the "browser" window (this is an electron app), the key gets removed.

Am I doing something wrong? I can use a setTimeout to explicitly revoke() the lease after X seconds, but this should be automatic, right?.

connor4312 commented 4 years ago

You should call builder.if rather than this.client.if to start the chain

manishiitg commented 3 years ago

@gtamas i got the same issue to fix create lease like this

const lease = client.lease(2, {
                        autoKeepAlive: false
                    });

by default lease is kept alive using setInterval internally https://github.com/microsoft/etcd3/blob/4b92eed/src/lease.ts#L73

jaytist commented 2 years ago

If I put lease ttl to 1 sec and autoKeepAlive :false and sleep my test for 3 sec only then the lease expires it should expires in 1 sec or is it expected behaviour?