Skip to content

Commit 2ba76c2

Browse files
committed
fixes
1 parent 7399677 commit 2ba76c2

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/extensions/policy_module.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
1010
impl crate::application::CoLink {
1111
pub async fn policy_module_start(&self) -> Result<(), Error> {
1212
let lock = self.lock("_policy_module:settings").await?;
13-
let mut settings: Settings = match self.read_entry("_policy_module:settings").await {
14-
Ok(res) => prost::Message::decode(&*res)?,
15-
Err(_) => Default::default(),
13+
let (mut settings, timestamp): (Settings, i64) = match self
14+
.read_entries(&[StorageEntry {
15+
key_name: "_policy_module:settings".to_string(),
16+
..Default::default()
17+
}])
18+
.await
19+
{
20+
Ok(res) => (
21+
prost::Message::decode(&*res[0].payload)?,
22+
get_timestamp(&res[0].key_path),
23+
),
24+
Err(_) => (Default::default(), 0),
1625
};
1726
if settings.enable {
1827
self.unlock(lock).await?;
19-
return Err("The policy module has already been started.")?;
28+
return self.wait_for_applying(timestamp).await; // Wait for the current timestamp to be applied.
2029
}
2130
settings.enable = true;
2231
let mut payload = vec![];
@@ -44,7 +53,7 @@ impl crate::application::CoLink {
4453
};
4554
if !settings.enable {
4655
self.unlock(lock).await?;
47-
return Err("The policy module is not running.")?;
56+
return Ok(()); // Return directly here because we only release the lock after the policy module truly stopped.
4857
}
4958
settings.enable = false;
5059
let mut payload = vec![];
@@ -54,8 +63,9 @@ impl crate::application::CoLink {
5463
.update_entry("_policy_module:settings", &payload)
5564
.await?,
5665
);
57-
self.unlock(lock).await?;
58-
self.wait_for_applying(timestamp).await
66+
let res = self.wait_for_applying(timestamp).await;
67+
self.unlock(lock).await?; // Unlock after the policy module truly stopped.
68+
res
5969
}
6070

6171
pub async fn policy_module_get_rules(&self) -> Result<Vec<Rule>, Error> {
@@ -84,7 +94,9 @@ impl crate::application::CoLink {
8494
.await?,
8595
);
8696
self.unlock(lock).await?;
87-
self.wait_for_applying(timestamp).await?;
97+
if settings.enable {
98+
self.wait_for_applying(timestamp).await?;
99+
}
88100
Ok(rule_id)
89101
}
90102

@@ -103,7 +115,10 @@ impl crate::application::CoLink {
103115
.await?,
104116
);
105117
self.unlock(lock).await?;
106-
self.wait_for_applying(timestamp).await
118+
if settings.enable {
119+
self.wait_for_applying(timestamp).await?;
120+
}
121+
Ok(())
107122
}
108123

109124
async fn wait_for_applying(&self, timestamp: i64) -> Result<(), Error> {

0 commit comments

Comments
 (0)