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 delay middleware #117

Merged
merged 2 commits into from
Oct 6, 2023
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
7 changes: 7 additions & 0 deletions benches/bench/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,36 +229,42 @@ fn config() -> Config {
params: vec![],
response: None,
cache: None,
delay_ms: None,
},
RpcMethod {
method: helpers::ASYNC_FAST_CALL.to_string(),
params: vec![],
response: None,
cache: None,
delay_ms: None,
},
RpcMethod {
method: helpers::SYNC_MEM_CALL.to_string(),
params: vec![],
response: None,
cache: None,
delay_ms: None,
},
RpcMethod {
method: helpers::ASYNC_MEM_CALL.to_string(),
params: vec![],
response: None,
cache: None,
delay_ms: None,
},
RpcMethod {
method: helpers::SYNC_SLOW_CALL.to_string(),
params: vec![],
response: None,
cache: None,
delay_ms: None,
},
RpcMethod {
method: helpers::ASYNC_SLOW_CALL.to_string(),
params: vec![],
response: None,
cache: None,
delay_ms: None,
},
RpcMethod {
method: helpers::ASYNC_INJECT_CALL.to_string(),
Expand All @@ -278,6 +284,7 @@ fn config() -> Config {
],
response: None,
cache: None,
delay_ms: None,
},
],
subscriptions: vec![RpcSubscription {
Expand Down
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extensions:

middlewares:
methods:
- delay
- response
- inject_params
- cache
Expand Down
3 changes: 3 additions & 0 deletions src/config/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub struct RpcMethod {

#[serde(default)]
pub response: Option<JsonValue>,

#[serde(default)]
pub delay_ms: Option<u64>,
}

#[derive(Copy, Clone, Deserialize, Debug)]
Expand Down
4 changes: 4 additions & 0 deletions src/middlewares/methods/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ mod tests {
}),
params: vec![],
response: None,
delay_ms: None,
},
&ext,
)
Expand All @@ -366,6 +367,7 @@ mod tests {
}),
params: vec![],
response: None,
delay_ms: None,
},
&ext,
)
Expand All @@ -382,6 +384,7 @@ mod tests {
}),
params: vec![],
response: None,
delay_ms: None,
},
&ext,
)
Expand All @@ -395,6 +398,7 @@ mod tests {
cache: None,
params: vec![],
response: None,
delay_ms: None,
},
&ext,
)
Expand Down
46 changes: 46 additions & 0 deletions src/middlewares/methods/delay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::time::Duration;

use async_trait::async_trait;

use crate::{
middleware::{Middleware, MiddlewareBuilder, NextFn, RpcMethod},
middlewares::{CallRequest, CallResult},
utils::{TypeRegistry, TypeRegistryRef},
};

pub struct DelayMiddleware {
delay: Duration,
}

impl DelayMiddleware {
pub fn new(delay: u64) -> Self {
Self {
delay: Duration::from_millis(delay),
}
}
}

#[async_trait]
impl MiddlewareBuilder<RpcMethod, CallRequest, CallResult> for DelayMiddleware {
async fn build(
method: &RpcMethod,
_extensions: &TypeRegistryRef,
) -> Option<Box<dyn Middleware<CallRequest, CallResult>>> {
method
.delay_ms
.map(|delay| Box::new(DelayMiddleware::new(delay)) as Box<dyn Middleware<CallRequest, CallResult>>)
}
}

#[async_trait]
impl Middleware<CallRequest, CallResult> for DelayMiddleware {
async fn call(
&self,
request: CallRequest,
context: TypeRegistry,
next: NextFn<CallRequest, CallResult>,
) -> CallResult {
tokio::time::sleep(self.delay).await;
next(request, context).await
}
}
1 change: 1 addition & 0 deletions src/middlewares/methods/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod block_tag;
pub mod cache;
pub mod delay;
pub mod inject_params;
pub mod response;
pub mod upstream;
1 change: 1 addition & 0 deletions src/middlewares/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub async fn create_method_middleware(
"cache" => cache::CacheMiddleware::build(method, extensions).await,
"block_tag" => block_tag::BlockTagMiddleware::build(method, extensions).await,
"inject_params" => inject_params::InjectParamsMiddleware::build(method, extensions).await,
"delay" => delay::DelayMiddleware::build(method, extensions).await,
_ => panic!("Unknown method middleware: {}", name),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ mod tests {
params: vec![],
cache: None,
response: None,
delay_ms: None,
}],
subscriptions: vec![],
aliases: vec![],
Expand Down