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

X request header related fixes #251

Merged
merged 4 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cache-rpc"
version = "0.2.13"
version = "0.2.14"
authors = ["Alexander Polakov <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
4 changes: 3 additions & 1 deletion src/rpc/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub async fn rpc_handler(
let url = app_state.rpc_url.clone();
let error = Error::Timeout(id).error_response();

let xreqid = xrid.0.clone();
let stream = stream_generator::generate_stream(move |mut stream| async move {
let mut backoff = backoff_settings(30);
let total = metrics().passthrough_total_time.start_timer();
Expand All @@ -182,7 +183,7 @@ pub async fn rpc_handler(
if let Some(header) = request_header.as_ref() {
request = request.append_header(header.clone());
}
request = request.append_header(("X-Request-ID", xrid.0.as_str()));
request = request.append_header(("X-Request-ID", xreqid.as_str()));
let resp = request.send_body(body.clone()).await.map_err(|err| {
error!(error = %err, "error while streaming response");
metrics().streaming_errors.inc();
Expand Down Expand Up @@ -235,6 +236,7 @@ pub async fn rpc_handler(

Ok(HttpResponse::Ok()
.content_type("application/json")
.append_header(("X-Request-ID", xrid.0.as_str()))
.streaming(Box::pin(stream)))
}

Expand Down
10 changes: 4 additions & 6 deletions src/rpc/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,9 @@ pub(super) fn parse_params<'a, T: Default + Deserialize<'a>>(
pub(super) fn generate_request_id() -> String {
const HEX_DIGITS: &[u8; 22] = b"0123456789abcdefABCDEF";
use rand::seq::SliceRandom;
let mut id = vec![0_u8; 32];
let mut rng = rand::thread_rng();
let closure = || HEX_DIGITS.choose(&mut rng).copied();
for (i, b) in std::iter::from_fn(closure).take(32).enumerate() {
id[i] = b;
}
String::from_utf8(id).unwrap()

(0..32)
.map(|_| *HEX_DIGITS.choose(&mut rng).unwrap() as char)
.collect()
}
1 change: 1 addition & 0 deletions src/rpc/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ pub(super) fn account_response<'a, 'b>(
return Ok(HttpResponse::Ok()
.append_header(("x-cache-status", "hit"))
.append_header(("x-cache-type", "lru"))
.append_header(("X-Request-ID", xrid.0.as_str()))
.content_type("application/json")
.body(body));
}
Expand Down