|
| 1 | +use crate::colink_proto::*; |
| 2 | +pub use colink_policy_module_proto::*; |
| 3 | +use prost::Message; |
| 4 | +mod colink_policy_module_proto { |
| 5 | + include!(concat!(env!("OUT_DIR"), "/colink_policy_module.rs")); |
| 6 | +} |
| 7 | + |
| 8 | +type Error = Box<dyn std::error::Error + Send + Sync + 'static>; |
| 9 | + |
| 10 | +impl crate::application::CoLink { |
| 11 | + pub async fn policy_module_start(&self) -> Result<(), Error> { |
| 12 | + let lock = self.lock("_policy_module:settings").await?; |
| 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), |
| 25 | + }; |
| 26 | + if settings.enable { |
| 27 | + self.unlock(lock).await?; |
| 28 | + return self.wait_for_applying(timestamp).await; // Wait for the current timestamp to be applied. |
| 29 | + } |
| 30 | + settings.enable = true; |
| 31 | + let mut payload = vec![]; |
| 32 | + settings.encode(&mut payload).unwrap(); |
| 33 | + let timestamp = get_timestamp( |
| 34 | + &self |
| 35 | + .update_entry("_policy_module:settings", &payload) |
| 36 | + .await?, |
| 37 | + ); |
| 38 | + self.unlock(lock).await?; |
| 39 | + let participants = vec![Participant { |
| 40 | + user_id: self.get_user_id()?, |
| 41 | + role: "local".to_string(), |
| 42 | + }]; |
| 43 | + self.run_task("policy_module", Default::default(), &participants, false) |
| 44 | + .await?; |
| 45 | + self.wait_for_applying(timestamp).await |
| 46 | + } |
| 47 | + |
| 48 | + pub async fn policy_module_stop(&self) -> Result<(), Error> { |
| 49 | + let lock = self.lock("_policy_module:settings").await?; |
| 50 | + let mut settings: Settings = match self.read_entry("_policy_module:settings").await { |
| 51 | + Ok(res) => prost::Message::decode(&*res)?, |
| 52 | + Err(_) => Default::default(), |
| 53 | + }; |
| 54 | + if !settings.enable { |
| 55 | + self.unlock(lock).await?; |
| 56 | + return Ok(()); // Return directly here because we only release the lock after the policy module truly stopped. |
| 57 | + } |
| 58 | + settings.enable = false; |
| 59 | + let mut payload = vec![]; |
| 60 | + settings.encode(&mut payload).unwrap(); |
| 61 | + let timestamp = get_timestamp( |
| 62 | + &self |
| 63 | + .update_entry("_policy_module:settings", &payload) |
| 64 | + .await?, |
| 65 | + ); |
| 66 | + let res = self.wait_for_applying(timestamp).await; |
| 67 | + self.unlock(lock).await?; // Unlock after the policy module truly stopped. |
| 68 | + res |
| 69 | + } |
| 70 | + |
| 71 | + pub async fn policy_module_get_rules(&self) -> Result<Vec<Rule>, Error> { |
| 72 | + let settings: Settings = match self.read_entry("_policy_module:settings").await { |
| 73 | + Ok(res) => prost::Message::decode(&*res)?, |
| 74 | + Err(_) => Default::default(), |
| 75 | + }; |
| 76 | + Ok(settings.rules) |
| 77 | + } |
| 78 | + |
| 79 | + pub async fn policy_module_add_rule(&self, rule: &Rule) -> Result<String, Error> { |
| 80 | + let lock = self.lock("_policy_module:settings").await?; |
| 81 | + let mut settings: Settings = match self.read_entry("_policy_module:settings").await { |
| 82 | + Ok(res) => prost::Message::decode(&*res)?, |
| 83 | + Err(_) => Default::default(), |
| 84 | + }; |
| 85 | + let rule_id = uuid::Uuid::new_v4().to_string(); |
| 86 | + let mut rule = rule.clone(); |
| 87 | + rule.rule_id = rule_id.clone(); |
| 88 | + settings.rules.push(rule); |
| 89 | + let mut payload = vec![]; |
| 90 | + settings.encode(&mut payload).unwrap(); |
| 91 | + let timestamp = get_timestamp( |
| 92 | + &self |
| 93 | + .update_entry("_policy_module:settings", &payload) |
| 94 | + .await?, |
| 95 | + ); |
| 96 | + self.unlock(lock).await?; |
| 97 | + if settings.enable { |
| 98 | + self.wait_for_applying(timestamp).await?; |
| 99 | + } |
| 100 | + Ok(rule_id) |
| 101 | + } |
| 102 | + |
| 103 | + pub async fn policy_module_remove_rule(&self, rule_id: &str) -> Result<(), Error> { |
| 104 | + let lock = self.lock("_policy_module:settings").await?; |
| 105 | + let mut settings: Settings = match self.read_entry("_policy_module:settings").await { |
| 106 | + Ok(res) => prost::Message::decode(&*res)?, |
| 107 | + Err(_) => Default::default(), |
| 108 | + }; |
| 109 | + settings.rules.retain(|x| x.rule_id != rule_id); |
| 110 | + let mut payload = vec![]; |
| 111 | + settings.encode(&mut payload).unwrap(); |
| 112 | + let timestamp = get_timestamp( |
| 113 | + &self |
| 114 | + .update_entry("_policy_module:settings", &payload) |
| 115 | + .await?, |
| 116 | + ); |
| 117 | + self.unlock(lock).await?; |
| 118 | + if settings.enable { |
| 119 | + self.wait_for_applying(timestamp).await?; |
| 120 | + } |
| 121 | + Ok(()) |
| 122 | + } |
| 123 | + |
| 124 | + async fn wait_for_applying(&self, timestamp: i64) -> Result<(), Error> { |
| 125 | + let key = "_policy_module:applied_settings_timestamp"; |
| 126 | + let start_timestamp = match self |
| 127 | + .read_entries(&[StorageEntry { |
| 128 | + key_name: key.to_string(), |
| 129 | + ..Default::default() |
| 130 | + }]) |
| 131 | + .await |
| 132 | + { |
| 133 | + Ok(res) => { |
| 134 | + let applied_settings_timestamp = |
| 135 | + i64::from_le_bytes(<[u8; 8]>::try_from(&*res[0].payload).unwrap()); |
| 136 | + if applied_settings_timestamp >= timestamp { |
| 137 | + return Ok(()); |
| 138 | + } |
| 139 | + get_timestamp(&res[0].key_path) + 1 |
| 140 | + } |
| 141 | + Err(_) => 0, |
| 142 | + }; |
| 143 | + let queue_name = self.subscribe(key, Some(start_timestamp)).await?; |
| 144 | + let mut subscriber = self.new_subscriber(&queue_name).await?; |
| 145 | + loop { |
| 146 | + let data = subscriber.get_next().await?; |
| 147 | + let message: SubscriptionMessage = Message::decode(&*data).unwrap(); |
| 148 | + if message.change_type != "delete" { |
| 149 | + let applied_settings_timestamp = |
| 150 | + i64::from_le_bytes(<[u8; 8]>::try_from(&*message.payload).unwrap()); |
| 151 | + if applied_settings_timestamp >= timestamp { |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + self.unsubscribe(&queue_name).await?; |
| 157 | + Ok(()) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +fn get_timestamp(key_path: &str) -> i64 { |
| 162 | + let pos = key_path.rfind('@').unwrap(); |
| 163 | + key_path[pos + 1..].parse().unwrap() |
| 164 | +} |
0 commit comments