Change http_content to Rc in HttpWrapper (#1391)

This commit is contained in:
007gzs
2024-10-21 09:44:01 +08:00
committed by GitHub
parent 32e5a59ae0
commit d96994767c
11 changed files with 446 additions and 304 deletions

View File

@@ -43,6 +43,12 @@ dependencies = [
"serde",
]
[[package]]
name = "downcast-rs"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2"
[[package]]
name = "fnv"
version = "1.0.7"
@@ -74,6 +80,7 @@ dependencies = [
name = "higress-wasm-rust"
version = "0.1.0"
dependencies = [
"downcast-rs",
"http",
"lazy_static",
"multimap",
@@ -108,9 +115,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.157"
version = "0.2.159"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "374af5f94e54fa97cf75e945cce8a6b201e88a1a07e688b47dfd2a59c66dbd86"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
[[package]]
name = "log"
@@ -135,15 +142,15 @@ dependencies = [
[[package]]
name = "once_cell"
version = "1.19.0"
version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]]
name = "proc-macro2"
version = "1.0.86"
version = "1.0.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9"
dependencies = [
"unicode-ident",
]
@@ -151,17 +158,18 @@ dependencies = [
[[package]]
name = "proxy-wasm"
version = "0.2.2"
source = "git+https://github.com/higress-group/proxy-wasm-rust-sdk?branch=main#73833051f57d483570cf5aaa9d62bd7402fae63b"
source = "git+https://github.com/higress-group/proxy-wasm-rust-sdk?branch=main#6735737fad486c8a7cc324241f58df4a160e7887"
dependencies = [
"downcast-rs",
"hashbrown",
"log",
]
[[package]]
name = "quote"
version = "1.0.36"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
@@ -174,18 +182,18 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "serde"
version = "1.0.208"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2"
checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.208"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf"
checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
dependencies = [
"proc-macro2",
"quote",
@@ -194,9 +202,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.125"
version = "1.0.128"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed"
checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8"
dependencies = [
"itoa",
"memchr",
@@ -206,9 +214,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.75"
version = "2.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9"
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590"
dependencies = [
"proc-macro2",
"quote",
@@ -217,15 +225,15 @@ dependencies = [
[[package]]
name = "unicode-ident"
version = "1.0.12"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "uuid"
version = "1.10.0"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314"
checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a"
dependencies = [
"getrandom",
]

View File

@@ -1,8 +1,6 @@
use higress_wasm_rust::cluster_wrapper::DnsCluster;
use higress_wasm_rust::log::Log;
use higress_wasm_rust::plugin_wrapper::{
HttpCallArgStorage, HttpCallbackFn, HttpContextWrapper, RootContextWrapper,
};
use higress_wasm_rust::plugin_wrapper::{HttpContextWrapper, RootContextWrapper};
use higress_wasm_rust::rule_matcher::{on_configure, RuleMatcher, SharedRuleMatcher};
use http::Method;
use multimap::MultiMap;
@@ -12,7 +10,7 @@ use proxy_wasm::types::{Bytes, ContextType, DataAction, HeaderAction, LogLevel};
use serde::Deserialize;
use std::cell::RefCell;
use std::ops::DerefMut;
use std::rc::Rc;
use std::rc::{Rc, Weak};
use std::time::Duration;
proxy_wasm::main! {{
@@ -37,40 +35,27 @@ fn format_body(body: Option<Vec<u8>>) -> String {
format!("{:?}", body)
}
fn test_callback(
this: &mut DemoWasm,
status_code: u16,
headers: &MultiMap<String, String>,
body: Option<Vec<u8>>,
) {
this.log.info(&format!(
"test_callback status_code:{}, headers: {:?}, body: {}",
status_code,
headers,
format_body(body)
));
this.reset_http_request();
}
struct DemoWasm {
// 每个请求对应的插件实例
log: Log,
config: Option<DemoWasmConfig>,
arg_storage: HttpCallArgStorage<Box<HttpCallbackFn<DemoWasm>>>,
config: Option<Rc<DemoWasmConfig>>,
weak: Weak<RefCell<Box<dyn HttpContextWrapper<DemoWasmConfig>>>>,
}
impl Context for DemoWasm {}
impl HttpContext for DemoWasm {}
impl HttpContextWrapper<DemoWasmConfig, Box<HttpCallbackFn<DemoWasm>>> for DemoWasm {
impl HttpContextWrapper<DemoWasmConfig> for DemoWasm {
fn init_self_weak(
&mut self,
self_weak: Weak<RefCell<Box<dyn HttpContextWrapper<DemoWasmConfig>>>>,
) {
self.weak = self_weak;
self.log.info("init_self_rc");
}
fn log(&self) -> &Log {
&self.log
}
fn get_http_call_storage(
&mut self,
) -> Option<&mut HttpCallArgStorage<Box<HttpCallbackFn<DemoWasm>>>> {
Some(&mut self.arg_storage)
}
fn on_config(&mut self, config: &DemoWasmConfig) {
fn on_config(&mut self, config: Rc<DemoWasmConfig>) {
// 获取config
self.log.info(&format!("on_config {}", config.test));
self.config = Some(config.clone())
@@ -101,16 +86,6 @@ impl HttpContextWrapper<DemoWasmConfig, Box<HttpCallbackFn<DemoWasm>>> for DemoW
// 是否缓存返回body
true
}
fn on_http_call_response_detail(
&mut self,
_token_id: u32,
arg: Box<HttpCallbackFn<DemoWasm>>,
status_code: u16,
headers: &MultiMap<String, String>,
body: Option<Vec<u8>>,
) {
arg(self, status_code, headers, body)
}
fn on_http_request_complete_body(&mut self, req_body: &Bytes) -> DataAction {
// 请求body获取完成回调
self.log.info(&format!(
@@ -118,23 +93,41 @@ impl HttpContextWrapper<DemoWasmConfig, Box<HttpCallbackFn<DemoWasm>>> for DemoW
String::from_utf8(req_body.clone()).unwrap_or("".to_string())
));
let cluster = DnsCluster::new("httpbin", "httpbin.org", 80);
if self
.http_call(
&cluster,
&Method::POST,
"http://httpbin.org/post",
MultiMap::new(),
Some("test_body".as_bytes()),
// Box::new(move |this, _status_code, _headers, _body| this.resume_http_request()),
Box::new(test_callback),
Duration::from_secs(5),
)
.is_ok()
{
DataAction::StopIterationAndBuffer
} else {
self.log.info("http_call fail");
DataAction::Continue
let self_rc = match self.weak.upgrade() {
Some(rc) => rc.clone(),
None => {
self.log.error("self_weak upgrade error");
return DataAction::Continue;
}
};
let http_call_res = self.http_call(
&cluster,
&Method::POST,
"http://httpbin.org/post",
MultiMap::new(),
Some("test_body".as_bytes()),
Box::new(move |status_code, headers, body| {
if let Some(this) = self_rc.borrow().downcast_ref::<DemoWasm>() {
this.log.info(&format!(
"test_callback status_code:{}, headers: {:?}, body: {}",
status_code,
headers,
format_body(body)
));
this.resume_http_request();
} else {
self_rc.borrow().resume_http_request();
}
}),
Duration::from_secs(5),
);
match http_call_res {
Ok(_) => DataAction::StopIterationAndBuffer,
Err(e) => {
self.log.info(&format!("http_call fail {:?}", e));
DataAction::Continue
}
}
}
fn on_http_response_complete_body(&mut self, res_body: &Bytes) -> DataAction {
@@ -185,7 +178,7 @@ impl RootContext for DemoWasmRoot {
}
}
impl RootContextWrapper<DemoWasmConfig, Box<HttpCallbackFn<DemoWasm>>> for DemoWasmRoot {
impl RootContextWrapper<DemoWasmConfig> for DemoWasmRoot {
fn rule_matcher(&self) -> &SharedRuleMatcher<DemoWasmConfig> {
&self.rule_matcher
}
@@ -193,11 +186,11 @@ impl RootContextWrapper<DemoWasmConfig, Box<HttpCallbackFn<DemoWasm>>> for DemoW
fn create_http_context_wrapper(
&self,
_context_id: u32,
) -> Option<Box<dyn HttpContextWrapper<DemoWasmConfig, Box<HttpCallbackFn<DemoWasm>>>>> {
) -> Option<Box<dyn HttpContextWrapper<DemoWasmConfig>>> {
Some(Box::new(DemoWasm {
config: None,
log: Log::new(PLUGIN_NAME.to_string()),
arg_storage: HttpCallArgStorage::new(),
weak: Weak::default(),
}))
}
}