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

Pass flush flag through sub chains #937

Merged
merged 7 commits into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 41 additions & 26 deletions shotover-proxy/src/transforms/chain.rs
Expand Up @@ -17,6 +17,7 @@ type InnerChain = Vec<Transforms>;
pub struct BufferedChainMessages {
pub local_addr: SocketAddr,
pub messages: Messages,
pub flush: bool,
pub return_chan: Option<oneshot::Sender<crate::error::ChainResponse>>,
}

Expand All @@ -25,18 +26,21 @@ impl BufferedChainMessages {
BufferedChainMessages {
local_addr,
messages: m,
flush: false,
return_chan: None,
}
}

pub fn new(
m: Messages,
local_addr: SocketAddr,
flush: bool,
return_chan: oneshot::Sender<ChainResponse>,
) -> Self {
BufferedChainMessages {
local_addr,
messages: m,
flush,
return_chan: Some(return_chan),
}
}
Expand Down Expand Up @@ -98,6 +102,7 @@ impl BufferedChain {
.send(BufferedChainMessages::new(
wrapper.messages,
wrapper.local_addr,
wrapper.flush,
one_tx,
))
.map_err(|e| anyhow!("Couldn't send message to wrapped chain {:?}", e))
Expand All @@ -106,7 +111,12 @@ impl BufferedChain {
Some(timeout) => {
self.send_handle
.send_timeout(
BufferedChainMessages::new(wrapper.messages, wrapper.local_addr, one_tx),
BufferedChainMessages::new(
wrapper.messages,
wrapper.local_addr,
wrapper.flush,
one_tx,
),
Duration::from_micros(timeout),
)
.map_err(|e| anyhow!("Couldn't send message to wrapped chain {:?}", e))
Expand All @@ -122,27 +132,34 @@ impl BufferedChain {
wrapper: Wrapper<'_>,
buffer_timeout_micros: Option<u64>,
) -> Result<()> {
match buffer_timeout_micros {
None => {
self.send_handle
.send(BufferedChainMessages::new_with_no_return(
wrapper.messages,
wrapper.local_addr,
))
.map_err(|e| anyhow!("Couldn't send message to wrapped chain {:?}", e))
.await?
}
Some(timeout) => {
self.send_handle
.send_timeout(
BufferedChainMessages::new_with_no_return(
if wrapper.flush {
// To obey flush request we need to ensure messages have completed sending before returning.
// In order to achieve that we need to use the regular process_request method.
self.process_request(wrapper, buffer_timeout_micros).await?;
} else {
// When there is no flush we can return much earlier by not waiting for a response.
match buffer_timeout_micros {
None => {
self.send_handle
.send(BufferedChainMessages::new_with_no_return(
wrapper.messages,
wrapper.local_addr,
),
Duration::from_micros(timeout),
)
.map_err(|e| anyhow!("Couldn't send message to wrapped chain {:?}", e))
.await?
))
.map_err(|e| anyhow!("Couldn't send message to wrapped chain {:?}", e))
.await?
}
Some(timeout) => {
self.send_handle
.send_timeout(
BufferedChainMessages::new_with_no_return(
wrapper.messages,
wrapper.local_addr,
),
Duration::from_micros(timeout),
)
.map_err(|e| anyhow!("Couldn't send message to wrapped chain {:?}", e))
.await?
}
}
}
Ok(())
Expand All @@ -167,19 +184,17 @@ impl TransformChain {
local_addr,
return_chan,
messages,
flush,
}) = rx.recv().await
{
#[cfg(test)]
{
count_clone.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}

let chain_response = chain
.process_request(
Wrapper::new_with_chain_name(messages, chain.name.clone(), local_addr),
chain.name.clone(),
)
.await;
let mut wrapper = Wrapper::new_with_chain_name(messages, chain.name.clone(), local_addr);
wrapper.flush = flush;
let chain_response = chain.process_request(wrapper, chain.name.clone()).await;

if let Err(e) = &chain_response {
error!("Internal error in buffered chain: {e:?}");
Expand Down
2 changes: 1 addition & 1 deletion shotover-proxy/src/transforms/mod.rs
Expand Up @@ -399,7 +399,7 @@ impl<'a> Clone for Wrapper<'a> {
client_details: self.client_details.clone(),
chain_name: self.chain_name.clone(),
local_addr: self.local_addr,
flush: false,
flush: self.flush,
}
}
}
Expand Down