Skip to content

Commit de19425

Browse files
author
jizhuozhi.george
committed
Add Promise support for http callout
move promise and dispatch http request to callout folder Signed-off-by: jizhuozhi.george <[email protected]>
1 parent b4150bc commit de19425

File tree

6 files changed

+83
-61
lines changed

6 files changed

+83
-61
lines changed

examples/http_parallel_call/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
publish = false
33
name = "proxy-wasm-example-http-parallel-call"
44
version = "0.0.1"
5-
authors = ["Zhuozhi Ji <jizhuozhi.george@google.com>"]
5+
authors = ["Zhuozhi Ji <jizhuozhi.george@bytedance.com>"]
66
description = "Proxy-Wasm plugin example: HTTP parallel call"
77
license = "Apache-2.0"
88
edition = "2018"

examples/http_parallel_call/src/lib.rs

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,83 +12,64 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use proxy_wasm::callout::http::HttpClient;
1516
use proxy_wasm::hostcalls;
16-
use proxy_wasm::promise::Promise;
17+
use proxy_wasm::callout::promise::Promise;
1718
use proxy_wasm::traits::*;
1819
use proxy_wasm::types::*;
19-
use std::collections::HashMap;
20-
use std::rc::Rc;
2120
use std::time::Duration;
2221

2322
proxy_wasm::main! {{
2423
proxy_wasm::set_log_level(LogLevel::Trace);
2524
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpParallelCall::default()) });
2625
}}
2726

28-
type OnHttpResponseArgs = (u32, usize, usize, usize);
29-
3027
#[derive(Default)]
3128
struct HttpParallelCall {
32-
m: HashMap<u32, Rc<Promise<OnHttpResponseArgs>>>,
29+
client: HttpClient,
3330
}
3431

3532
impl HttpContext for HttpParallelCall {
3633
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
3734
// "Hello, "
38-
let token1 = self
39-
.dispatch_http_call(
40-
"httpbin",
41-
vec![
42-
(":method", "GET"),
43-
(":path", "/base64/SGVsbG8sIAo="),
44-
(":authority", "httpbin.org"),
45-
],
46-
None,
47-
vec![],
48-
Duration::from_secs(1),
49-
)
50-
.unwrap();
35+
let promise1 = self.client.dispatch(
36+
"httpbin",
37+
vec![
38+
(":method", "GET"),
39+
(":path", "/base64/SGVsbG8sIA=="),
40+
(":authority", "httpbin.org"),
41+
],
42+
None,
43+
vec![],
44+
Duration::from_secs(1),
45+
);
5146

5247
// "World!"
53-
let token2 = self
54-
.dispatch_http_call(
55-
"httpbin",
56-
vec![
57-
(":method", "GET"),
58-
(":path", "/base64/V29ybGQhCg=="),
59-
(":authority", "httpbin.org"),
60-
],
61-
None,
62-
vec![],
63-
Duration::from_secs(1),
64-
)
65-
.unwrap();
66-
67-
let promise1 = Promise::new();
68-
let promise2 = Promise::new();
69-
self.m.insert(token1, promise1.clone());
70-
self.m.insert(token2, promise2.clone());
48+
let promise2 = self.client.dispatch(
49+
"httpbin",
50+
vec![
51+
(":method", "GET"),
52+
(":path", "/base64/V29ybGQh"),
53+
(":authority", "httpbin.org"),
54+
],
55+
None,
56+
vec![],
57+
Duration::from_secs(1),
58+
);
7159

7260
Promise::all_of(vec![
7361
promise1
74-
.then(|(_, _, _body_size, _)| get_http_call_response_body_string(0, _body_size))
62+
.then(|(_, _, body_size, _)| get_http_call_response_body_string(0, body_size))
7563
.then(|body| body.unwrap_or_default()),
7664
promise2
77-
.then(|(_, _, _body_size, _)| get_http_call_response_body_string(0, _body_size))
65+
.then(|(_, _, body_size, _)| get_http_call_response_body_string(0, body_size))
7866
.then(|body| body.unwrap_or_default()),
7967
])
8068
.then(|results| {
8169
send_http_response(
8270
200,
8371
vec![],
84-
Some(
85-
format!(
86-
"{}{}\n",
87-
results[0].strip_suffix("\n").unwrap(),
88-
results[1].strip_suffix("\n").unwrap()
89-
)
90-
.as_bytes(),
91-
),
72+
Some(format!("{}{}\n", results[0], results[1]).as_bytes()),
9273
);
9374
});
9475

@@ -104,15 +85,13 @@ impl HttpContext for HttpParallelCall {
10485
impl Context for HttpParallelCall {
10586
fn on_http_call_response(
10687
&mut self,
107-
_token_id: u32,
108-
_num_headers: usize,
109-
_body_size: usize,
110-
_num_trailers: usize,
88+
token_id: u32,
89+
num_headers: usize,
90+
body_size: usize,
91+
num_trailers: usize,
11192
) {
112-
let promise = self.m.remove(&_token_id);
113-
promise
114-
.unwrap()
115-
.fulfill((_token_id, _num_headers, _body_size, _num_trailers));
93+
self.client
94+
.callback(token_id, num_headers, body_size, num_trailers)
11695
}
11796
}
11897

src/callout/http.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::callout::promise::Promise;
2+
use crate::hostcalls;
3+
use std::collections::HashMap;
4+
use std::rc::Rc;
5+
use std::time::Duration;
6+
7+
type OnHttpResponseArgs = (u32, usize, usize, usize);
8+
9+
#[derive(Default)]
10+
pub struct HttpClient {
11+
m: HashMap<u32, Rc<Promise<OnHttpResponseArgs>>>,
12+
}
13+
14+
impl HttpClient {
15+
pub fn dispatch(
16+
&mut self,
17+
upstream: &str,
18+
headers: Vec<(&str, &str)>,
19+
body: Option<&[u8]>,
20+
trailers: Vec<(&str, &str)>,
21+
timeout: Duration,
22+
) -> Rc<Promise<(u32, usize, usize, usize)>> {
23+
let token =
24+
hostcalls::dispatch_http_call(upstream, headers, body, trailers, timeout).unwrap();
25+
let promise = Promise::new();
26+
self.m.insert(token, promise.clone());
27+
promise
28+
}
29+
30+
pub fn callback(
31+
&mut self,
32+
token_id: u32,
33+
num_headers: usize,
34+
body_size: usize,
35+
num_trailers: usize,
36+
) {
37+
let promise = self.m.remove(&token_id);
38+
promise
39+
.unwrap()
40+
.fulfill((token_id, num_headers, body_size, num_trailers))
41+
}
42+
}

src/callout/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod http;
2+
pub mod promise;
File renamed without changes.

src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
pub mod hostcalls;
16-
pub mod promise;
17-
pub mod traits;
18-
pub mod types;
19-
2015
mod allocator;
16+
pub mod callout;
2117
mod dispatcher;
18+
pub mod hostcalls;
2219
mod logger;
20+
pub mod traits;
21+
pub mod types;
2322

2423
// For crate-type="cdylib".
2524
#[cfg(not(wasi_exec_model_reactor))]

0 commit comments

Comments
 (0)