-
Notifications
You must be signed in to change notification settings - Fork 107
Add Promise support for http callout #265
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
Open
jizhuozhi
wants to merge
12
commits into
proxy-wasm:main
Choose a base branch
from
jizhuozhi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0ad1305
Add Promise support for http callout
cfefbcf
Add Promise support for http callout
a1e4f9e
Add Promise support for http callout
b4150bc
Add Promise support for http callout
de19425
Add Promise support for http callout
5c95bb4
Add Promise support for http callout
95f4d06
Add Promise support for http callout
a5ff216
Add Promise support for http callout
dc6e39f
Add Promise support for http callout
d797999
Add Promise support for http callout
fdc72a6
Merge branch 'main' into main
PiotrSikora 9833aa6
Add Promise support for http callout
jizhuozhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/headers`: | ||
|
||
```sh | ||
$ curl localhost:10000/headers | ||
Hello, World!\n | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// 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::hostcalls; | ||
use proxy_wasm::promise::Promise; | ||
use proxy_wasm::traits::*; | ||
use proxy_wasm::types::*; | ||
use std::collections::HashMap; | ||
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)] | ||
struct HttpParallelCall { | ||
m: HashMap<u32, Rc<Promise<(u32, usize, usize, usize)>>>, | ||
} | ||
|
||
impl HttpContext for HttpParallelCall { | ||
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { | ||
// "Hello, " | ||
let token1 = self | ||
.dispatch_http_call( | ||
"httpbin", | ||
vec![ | ||
(":method", "GET"), | ||
(":path", "/base64/SGVsbG8sIAo="), | ||
(":authority", "httpbin.org"), | ||
], | ||
None, | ||
vec![], | ||
Duration::from_secs(1), | ||
) | ||
.unwrap(); | ||
|
||
// "World!" | ||
let token2 = self | ||
.dispatch_http_call( | ||
"httpbin", | ||
vec![ | ||
(":method", "GET"), | ||
(":path", "/base64/V29ybGQhCg=="), | ||
(":authority", "httpbin.org"), | ||
], | ||
None, | ||
vec![], | ||
Duration::from_secs(1), | ||
) | ||
.unwrap(); | ||
|
||
let promise1 = Promise::new(); | ||
let promise2 = Promise::new(); | ||
self.m.insert(token1, promise1.clone()); | ||
self.m.insert(token2, promise2.clone()); | ||
jizhuozhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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())), | ||
promise2 | ||
.then(|(_, _, _body_size, _)| get_http_call_response_body_string(0, _body_size)) | ||
.then(|body| body.unwrap_or_else(|| "".to_string())), | ||
jizhuozhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]) | ||
.then(|results| { | ||
send_http_response( | ||
200, | ||
vec![], | ||
Some( | ||
format!( | ||
"{}{}\n", | ||
results[0].strip_suffix("\n").unwrap(), | ||
results[1].strip_suffix("\n").unwrap() | ||
jizhuozhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
.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, | ||
) { | ||
let promise = self.m.remove(&_token_id); | ||
promise | ||
.unwrap() | ||
.fulfill((_token_id, _num_headers, _body_size, _num_trailers)); | ||
jizhuozhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
fn get_http_call_response_body_string(start: usize, max_size: usize) -> Option<String> { | ||
match hostcalls::get_buffer(BufferType::HttpCallResponseBody, start, max_size).unwrap() { | ||
None => None, | ||
Some(bytes) => { | ||
let body_string = String::from_utf8(bytes.to_vec()).unwrap(); | ||
Some(body_string) | ||
} | ||
} | ||
} | ||
|
||
fn send_http_response(status_code: u32, headers: Vec<(&str, &str)>, body: Option<&[u8]>) { | ||
hostcalls::send_http_response(status_code, headers, body).unwrap() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.