Skip to content

Commit

Permalink
Add Promise support for http callout
Browse files Browse the repository at this point in the history
fix ci

Signed-off-by: jizhuozhi.george <[email protected]>
  • Loading branch information
jizhuozhi committed Oct 14, 2024
1 parent a1e4f9e commit b4150bc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
8 changes: 5 additions & 3 deletions examples/http_parallel_call/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ proxy_wasm::main! {{
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpParallelCall::default()) });
}}

type OnHttpResponseArgs = (u32, usize, usize, usize);

#[derive(Default)]
struct HttpParallelCall {
m: HashMap<u32, Rc<Promise<(u32, usize, usize, usize)>>>,
m: HashMap<u32, Rc<Promise<OnHttpResponseArgs>>>,
}

impl HttpContext for HttpParallelCall {
Expand Down Expand Up @@ -70,10 +72,10 @@ impl HttpContext for HttpParallelCall {
Promise::all_of(vec![
promise1
.then(|(_, _, _body_size, _)| get_http_call_response_body_string(0, _body_size))
.then(|body| body.unwrap_or_else(|| "".to_string())),
.then(|body| body.unwrap_or_default()),
promise2
.then(|(_, _, _body_size, _)| get_http_call_response_body_string(0, _body_size))
.then(|body| body.unwrap_or_else(|| "".to_string())),
.then(|body| body.unwrap_or_default()),
])
.then(|results| {
send_http_response(
Expand Down
21 changes: 12 additions & 9 deletions src/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ enum PromiseState<T> {
Rejected(String),
}

type ThenCallbackRef<T> = RefCell<Option<Box<dyn FnOnce(T)>>>;
type CatchCallbackRef = RefCell<Option<Box<dyn FnOnce(String)>>>;

pub struct Promise<T> {
state: RefCell<PromiseState<T>>,
then_callback: RefCell<Option<Box<dyn FnOnce(T)>>>,
catch_callback: RefCell<Option<Box<dyn FnOnce(String)>>>,
then_callback: ThenCallbackRef<T>,
catch_callback: CatchCallbackRef,
}

impl<T> Promise<T>
Expand Down Expand Up @@ -202,7 +205,7 @@ mod tests {
});

promise.fulfill(42);
assert_eq!(true, touched.take())
assert!(touched.take())
}

#[test]
Expand All @@ -217,7 +220,7 @@ mod tests {
});

promise.reject("Error".to_string());
assert_eq!(true, touched.take())
assert!(touched.take())
}

#[test]
Expand All @@ -237,7 +240,7 @@ mod tests {
});

promise.fulfill(10);
assert_eq!(true, touched.take())
assert!(touched.take())
}

#[test]
Expand All @@ -264,7 +267,7 @@ mod tests {
panic!("Should not reach here");
});

assert_eq!(true, touched.take())
assert!(touched.take())
}

#[test]
Expand All @@ -289,7 +292,7 @@ mod tests {
*touched_clone.borrow_mut() = true;
});

assert_eq!(true, touched.take())
assert!(touched.take())
}

#[test]
Expand All @@ -314,7 +317,7 @@ mod tests {
*touched_clone.borrow_mut() = true;
});

assert_eq!(true, touched.take())
assert!(touched.take())
}

#[test]
Expand All @@ -333,6 +336,6 @@ mod tests {
panic!("Should not reach here");
});

assert_eq!(true, touched.take())
assert!(touched.take())
}
}

0 comments on commit b4150bc

Please sign in to comment.