Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Promise support for http callout #265

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ jobs:
- 'http_body'
- 'http_config'
- 'http_headers'
- 'http_parallel_call'
- 'grpc_auth_random'

defaults:
Expand Down Expand Up @@ -444,6 +445,7 @@ jobs:
- 'http_body'
- 'http_config'
- 'http_headers'
- 'http_parallel_call'
- 'grpc_auth_random'

defaults:
Expand Down
5 changes: 4 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ cargo_build_script(

rust_library(
name = "proxy_wasm",
srcs = glob(["src/*.rs"]),
srcs = glob([
"src/*.rs",
"src/callout/*.rs",
]),
edition = "2018",
visibility = ["//visibility:public"],
deps = [
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [HTTP Headers](./examples/http_headers/)
- [HTTP Response body](./examples/http_body/)
- [HTTP Configuration](./examples/http_config/)
- [HTTP Parallel Call](./examples/http_parallel_call/)
- [gRPC Auth (random)](./examples/grpc_auth_random/)

## Articles & blog posts from the community
Expand Down
22 changes: 22 additions & 0 deletions examples/http_parallel_call/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
publish = false
name = "proxy-wasm-example-http-parallel-call"
version = "0.0.1"
authors = ["Zhuozhi Ji <[email protected]>"]
description = "Proxy-Wasm plugin example: HTTP parallel call"
license = "Apache-2.0"
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
log = "0.4"
proxy-wasm = { path = "../../" }

[profile.release]
lto = true
opt-level = 3
codegen-units = 1
panic = "abort"
strip = "debuginfo"
27 changes: 27 additions & 0 deletions examples/http_parallel_call/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Proxy-Wasm plugin example: HTTP parallel call

Proxy-Wasm plugin that makes multiply HTTP callout and combine responses as final response .

### Building

```sh
$ cargo build --target wasm32-wasi --release
```

### Using in Envoy

This example can be run with [`docker compose`](https://docs.docker.com/compose/install/)
and has a matching Envoy configuration.

```sh
$ docker compose up
```

#### Access granted.

Send HTTP request to `localhost:10000/`:

```sh
$ curl localhost:10000/
Hello, World!\n
```
36 changes: 36 additions & 0 deletions examples/http_parallel_call/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

services:
envoy:
image: envoyproxy/envoy:v1.31-latest
hostname: envoy
ports:
- "10000:10000"
volumes:
- ./envoy.yaml:/etc/envoy/envoy.yaml
- ./target/wasm32-wasi/release:/etc/envoy/proxy-wasm-plugins
networks:
- envoymesh
depends_on:
- httpbin
httpbin:
image: mccutchen/go-httpbin
hostname: httpbin
ports:
- "8080:8080"
networks:
- envoymesh
networks:
envoymesh: {}
68 changes: 68 additions & 0 deletions examples/http_parallel_call/envoy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

static_resources:
listeners:
address:
socket_address:
address: 0.0.0.0
port_value: 10000
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: ingress_http
codec_type: AUTO
route_config:
name: local_routes
virtual_hosts:
- name: local_service
domains:
- "*"
routes:
- match:
prefix: "/"
route:
cluster: httpbin
http_filters:
- name: envoy.filters.http.wasm
typed_config:
"@type": type.googleapis.com/udpa.type.v1.TypedStruct
type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
value:
config:
name: "http_parallel_call"
vm_config:
runtime: "envoy.wasm.runtime.v8"
code:
local:
filename: "/etc/envoy/proxy-wasm-plugins/proxy_wasm_example_http_parallel_call.wasm"
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
- name: httpbin
connect_timeout: 5s
type: STRICT_DNS
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: httpbin
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: httpbin
port_value: 8080
112 changes: 112 additions & 0 deletions examples/http_parallel_call/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use proxy_wasm::callout::http::HttpClient;
use proxy_wasm::callout::promise::Promise;
use proxy_wasm::traits::*;
use proxy_wasm::types::*;
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;

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

#[derive(Default, Clone)]
struct HttpParallelCall {
client: Rc<RefCell<HttpClient>>,
}

impl HttpContext for HttpParallelCall {
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
let self_clone_for_promise1 = self.clone();
let self_clone_for_promise2 = self.clone();
let self_clone_for_join = self.clone();

// "Hello, "
let promise1 = self
.client
.borrow_mut()
.dispatch(
"httpbin",
vec![
(":method", "GET"),
(":path", "/base64/SGVsbG8sIA=="),
(":authority", "httpbin.org"),
],
None,
vec![],
Duration::from_secs(1),
)
.then(move |(_, _, body_size, _)| {
match self_clone_for_promise1.get_http_call_response_body(0, body_size) {
None => "".to_owned(),
Some(bytes) => String::from_utf8(bytes.to_vec()).unwrap(),
}
});

// "World!"
let promise2 = self
.client
.borrow_mut()
.dispatch(
"httpbin",
vec![
(":method", "GET"),
(":path", "/base64/V29ybGQh"),
(":authority", "httpbin.org"),
],
None,
vec![],
Duration::from_secs(1),
)
.then(move |(_, _, body_size, _)| {
match self_clone_for_promise2.get_http_call_response_body(0, body_size) {
None => "".to_owned(),
Some(bytes) => String::from_utf8(bytes.to_vec()).unwrap(),
}
});

Promise::all_of(vec![promise1, promise2]).then(move |results| {
self_clone_for_join.send_http_response(
200,
vec![],
Some(format!("{}{}\n", results[0], results[1]).as_bytes()),
);
});

Action::Pause
}

fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action {
self.set_http_response_header("Powered-By", Some("proxy-wasm"));
Action::Continue
}
}

impl Context for HttpParallelCall {
fn on_http_call_response(
&mut self,
token_id: u32,
num_headers: usize,
body_size: usize,
num_trailers: usize,
) {
self.client
.borrow_mut()
.callback(token_id, num_headers, body_size, num_trailers)
}
}
56 changes: 56 additions & 0 deletions src/callout/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::callout::promise::Promise;
use crate::hostcalls;
use std::collections::HashMap;
use std::rc::Rc;
use std::time::Duration;

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

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

impl HttpClient {
pub fn dispatch(
&mut self,
upstream: &str,
headers: Vec<(&str, &str)>,
body: Option<&[u8]>,
trailers: Vec<(&str, &str)>,
timeout: Duration,
) -> Rc<Promise<(u32, usize, usize, usize)>> {
let token =
hostcalls::dispatch_http_call(upstream, headers, body, trailers, timeout).unwrap();
let promise = Promise::new();
self.m.insert(token, promise.clone());
promise
}

pub fn callback(
&mut self,
token_id: u32,
num_headers: usize,
body_size: usize,
num_trailers: usize,
) {
let promise = self.m.remove(&token_id);
promise
.unwrap()
.fulfill((token_id, num_headers, body_size, num_trailers))
}
}
16 changes: 16 additions & 0 deletions src/callout/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod http;
pub mod promise;
Loading
Loading