Skip to content

Commit

Permalink
refactor: Wrap lease_keepalive_abort with defer
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed May 13, 2024
1 parent 7f2b1b3 commit 60ee9bb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ crate-type = ["cdylib"]
etcd-client = "0.12.4"
pyo3 = { version = "0.20.2", features = ["extension-module", "multiple-pymethods"] }
pyo3-asyncio = { version = "0.20.0", features = ["tokio-runtime"] }
scopeguard = "1.2.0"
tokio = { version = "1.32.0", features = ["sync"] }
tokio-stream = "0.1.14"
tonic = "0.10.2"
13 changes: 8 additions & 5 deletions src/lock_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use etcd_client::{Client as EtcdClient, LockOptions};

use pyo3::{prelude::*, types::PyBytes};
use scopeguard::defer;
use std::{future::ready, time::Duration};
use tokio::time::{sleep, timeout};

Expand Down Expand Up @@ -112,13 +113,9 @@ impl EtcdLockManager {
None => ready(Ok(self.try_lock(&mut client).await)).await,
};

if let Some(ref lease_keepalive_task) = self.lease_keepalive_task {
lease_keepalive_task.abort();
}

match timeout_result {
Ok(Ok(_)) => {}
Ok(Err(e)) => return Err(e.into()),
Ok(Err(try_lock_err)) => return Err(try_lock_err.into()),
Err(timedout_err) => {
if let Some(lease_id) = self.lease_id {
if let Err(etcd_client::Error::GRpcStatus(status)) =
Expand All @@ -133,6 +130,12 @@ impl EtcdLockManager {
}
}

defer! {
if let Some(ref lease_keepalive_task) = self.lease_keepalive_task {
lease_keepalive_task.abort();
}
}

Ok(PyCommunicator::new(client))
}

Expand Down

0 comments on commit 60ee9bb

Please sign in to comment.