From d718870b654e9187c73b9ef19d0896b2e6ac89d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BA=AA=E5=8D=93=E5=BF=97?= Date: Tue, 4 Jul 2023 19:59:43 +0800 Subject: [PATCH] feat: `get_match_config` returns with rule_id for stateful plugins (#406) --- .../wasm-rust/extensions/say-hello/src/lib.rs | 8 +++---- plugins/wasm-rust/src/rule_matcher.rs | 21 +++++++++++-------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/plugins/wasm-rust/extensions/say-hello/src/lib.rs b/plugins/wasm-rust/extensions/say-hello/src/lib.rs index 458a7a466..fb78a7ea6 100644 --- a/plugins/wasm-rust/extensions/say-hello/src/lib.rs +++ b/plugins/wasm-rust/extensions/say-hello/src/lib.rs @@ -13,7 +13,7 @@ // limitations under the License. use higress_wasm_rust::log::Log; -use higress_wasm_rust::rule_matcher::{on_configure, RuleMatcher}; +use higress_wasm_rust::rule_matcher::{on_configure, RuleMatcher, SharedRuleMatcher}; use proxy_wasm::traits::{Context, HttpContext, RootContext}; use proxy_wasm::types::{Action, ContextType, LogLevel}; use serde::Deserialize; @@ -28,11 +28,11 @@ proxy_wasm::main! {{ struct SayHelloRoot { log: Log, - rule_matcher: Rc>>, + rule_matcher: SharedRuleMatcher, } struct SayHello { - rule_matcher: Rc>>, + rule_matcher: SharedRuleMatcher, } #[derive(Default, Debug, Deserialize)] @@ -82,7 +82,7 @@ impl HttpContext for SayHello { self.send_http_response(200, vec![], Some("Hello, World!".as_bytes())); return Action::Continue; } - Some(config) => config, + Some(config) => config.1, }; self.send_http_response( diff --git a/plugins/wasm-rust/src/rule_matcher.rs b/plugins/wasm-rust/src/rule_matcher.rs index eefad117c..647834433 100644 --- a/plugins/wasm-rust/src/rule_matcher.rs +++ b/plugins/wasm-rust/src/rule_matcher.rs @@ -20,7 +20,9 @@ use proxy_wasm::traits::RootContext; use proxy_wasm::types::LogLevel; use serde::de::DeserializeOwned; use serde_json::{from_slice, Map, Value}; +use std::cell::RefCell; use std::collections::HashSet; +use std::rc::Rc; enum Category { Route, @@ -37,6 +39,8 @@ const RULES_KEY: &str = "_rules_"; const MATCH_ROUTE_KEY: &str = "_match_route_"; const MATCH_DOMAIN_KEY: &str = "_match_domain_"; +pub type SharedRuleMatcher = Rc>>; + struct HostMatcher { match_type: MatchType, host: String, @@ -137,15 +141,15 @@ where Ok(()) } - pub fn get_match_config(&self) -> Option<&PluginConfig> { + pub fn get_match_config(&self) -> Option<(i64, &PluginConfig)> { let host = get_http_request_header(":authority").unwrap_or_default(); let route_name = get_property(vec!["route_name"]).unwrap_or_default(); - for rule in &self.rule_config { + for (i, rule) in self.rule_config.iter().enumerate() { match rule.category { Category::Host => { if self.host_match(rule, host.as_str()) { - return Some(&rule.config); + return Some((i as i64, &rule.config)); } } Category::Route => { @@ -154,20 +158,19 @@ where .unwrap_or_else(|_| "".to_string()) .as_str(), ) { - return Some(&rule.config); + return Some((i as i64, &rule.config)); } } } } - return self.global_config.as_ref(); + self.global_config + .as_ref() + .map(|config| (usize::MAX as i64, config)) } pub fn rewrite_config(&mut self, rewrite: fn(config: &PluginConfig) -> PluginConfig) { - self.global_config = match self.global_config.as_ref() { - None => None, - Some(config) => Some(rewrite(config)) - }; + self.global_config = self.global_config.as_ref().map(rewrite); for rule_config in &mut self.rule_config { rule_config.config = rewrite(&rule_config.config);