1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ use proxy_wasm:: callout:: http:: HttpClient ;
1516use proxy_wasm:: hostcalls;
16- use proxy_wasm:: promise:: Promise ;
17+ use proxy_wasm:: callout :: promise:: Promise ;
1718use proxy_wasm:: traits:: * ;
1819use proxy_wasm:: types:: * ;
19- use std:: collections:: HashMap ;
20- use std:: rc:: Rc ;
2120use std:: time:: Duration ;
2221
2322proxy_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 ) ]
3128struct HttpParallelCall {
32- m : HashMap < u32 , Rc < Promise < OnHttpResponseArgs > > > ,
29+ client : HttpClient ,
3330}
3431
3532impl 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 {
10485impl 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
0 commit comments