python-etcd3 icon indicating copy to clipboard operation
python-etcd3 copied to clipboard

How to "keep-alive" by lease_id?

Open petrasl1976 opened this issue 1 year ago • 1 comments

Hi,

I am trying to find a way how can i extend TTL for lease by having its id only (not a whole object)? Lets say i have a script which will create lease:

import etcd3
etcd = etcd3.client()
etcd.lease(50, lease_id=123)

And i have another script which i like to refresh the leases only, without [re]creating it:

import etcd3
etcd = etcd3.client()
etcd.refresh_lease(lease_id=123) # or smth

Similar functionality like etcdctl has, so i can run keep-alive independently from another script:

# etcdctl lease grant 60
lease 42d78d9df80a26ab granted with TTL(60s)
# etcdctl lease keep-alive 42d78d9df80a26ab --once
lease 42d78d9df80a26ab keepalived with TTL(60)

Is there a way with etcd3 module extend lease when it is already created by another process or script ?

Thanks

petrasl1976 avatar Feb 13 '24 07:02 petrasl1976

with the same problem as you, I looked at the implementation and found "refresh_lease" in etcd3/client.py :

@_handle_generator_errors
    def refresh_lease(self, lease_id):
        keep_alive_request = etcdrpc.LeaseKeepAliveRequest(ID=lease_id)
        request_stream = [keep_alive_request]
        for response in self.leasestub.LeaseKeepAlive(
                iter(request_stream),
                self.timeout,
                credentials=self.call_credentials,
                metadata=self.metadata):
            yield response

Because the last line uses yield, so when use refresh_lease, it will not be executed immediately after the function is called, and needs use next() to make it work

it work for me like this:

next(client.refresh_lease(lease_id))

pcleon avatar Jun 22 '24 17:06 pcleon