Skip to content

Commit 206136a

Browse files
committed
Iteration
1 parent 018dba9 commit 206136a

File tree

2 files changed

+26
-48
lines changed

2 files changed

+26
-48
lines changed

lib/executor/src/plugins/response_cache.rs

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use dashmap::DashMap;
22
use ntex::web::HttpResponse;
33
use redis::Commands;
4-
use sonic_rs::json;
54

65
use crate::{
76
plugins::traits::{
8-
ControlFlow, OnExecuteEnd, OnExecuteEndPayload, OnExecuteStart, OnExecuteStartPayload,
9-
OnSchemaReload, OnSchemaReloadPayload,
7+
ControlFlow, OnExecutePayload, OnSchemaReloadPayload, RouterPlugin
108
},
119
utils::consts::TYPENAME_FIELD_NAME,
1210
};
@@ -26,20 +24,15 @@ impl ResponseCachePlugin {
2624
}
2725
}
2826

29-
pub struct ResponseCacheContext {
30-
key: String,
31-
}
32-
33-
impl OnExecuteStart for ResponseCachePlugin {
34-
fn on_execute_start(&self, payload: OnExecuteStartPayload) -> ControlFlow {
27+
impl RouterPlugin for ResponseCachePlugin {
28+
fn on_execute<'exec>(
29+
&self,
30+
payload: OnExecutePayload<'exec>,
31+
) -> ControlFlow<'exec, OnExecutePayload<'exec>> {
3532
let key = format!(
3633
"response_cache:{}:{:?}",
3734
payload.query_plan, payload.variable_values
3835
);
39-
payload
40-
.router_http_request
41-
.extensions_mut()
42-
.insert(ResponseCacheContext { key: key.clone() });
4336
if let Ok(mut conn) = self.redis_client.get_connection() {
4437
let cached_response: Option<Vec<u8>> = conn.get(&key).ok();
4538
if let Some(cached_response) = cached_response {
@@ -49,24 +42,12 @@ impl OnExecuteStart for ResponseCachePlugin {
4942
.body(cached_response),
5043
);
5144
}
52-
}
53-
ControlFlow::Continue
54-
}
55-
}
45+
ControlFlow::OnEnd(Box::new(move |payload: OnExecutePayload| {
46+
// Do not cache if there are errors
47+
if !payload.errors.is_empty() {
48+
return ControlFlow::Continue;
49+
}
5650

57-
impl OnExecuteEnd for ResponseCachePlugin {
58-
fn on_execute_end(&self, payload: OnExecuteEndPayload) -> ControlFlow {
59-
// Do not cache if there are errors
60-
if !payload.errors.is_empty() {
61-
return ControlFlow::Continue;
62-
}
63-
if let Some(key) = payload
64-
.router_http_request
65-
.extensions()
66-
.get::<ResponseCacheContext>()
67-
.map(|ctx| &ctx.key)
68-
{
69-
if let Ok(mut conn) = self.redis_client.get_connection() {
7051
if let Ok(serialized) = sonic_rs::to_vec(&payload.data) {
7152
// Decide on the ttl somehow
7253
// Get the type names
@@ -93,18 +74,16 @@ impl OnExecuteEnd for ResponseCachePlugin {
9374
// Insert the ttl into extensions for client awareness
9475
payload
9576
.extensions
96-
.insert("response_cache_ttl".to_string(), json!(max_ttl));
77+
.insert("response_cache_ttl".to_string(), sonic_rs::json!(max_ttl));
9778

9879
// Set the cache with the decided ttl
9980
let _: () = conn.set_ex(key, serialized, max_ttl).unwrap_or(());
10081
}
101-
}
82+
ControlFlow::Continue
83+
}));
10284
}
10385
ControlFlow::Continue
10486
}
105-
}
106-
107-
impl OnSchemaReload for ResponseCachePlugin {
10887
fn on_schema_reload(&self, payload: OnSchemaReloadPayload) {
10988
// Visit the schema and update ttl_per_type based on some directive
11089
payload

lib/executor/src/plugins/traits.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ use ntex::web::HttpResponse;
88
use crate::response::graphql_error::GraphQLError;
99
use crate::response::value::Value;
1010

11-
pub enum ControlFlow {
11+
pub enum ControlFlow<'a, TPayload> {
1212
Continue,
1313
Break(HttpResponse),
14+
OnEnd(Box<dyn FnOnce(TPayload) -> ControlFlow<'a, ()> + Send + 'a>),
1415
}
1516

1617
pub struct ExecutionResult<'exec> {
@@ -19,21 +20,27 @@ pub struct ExecutionResult<'exec> {
1920
pub extensions: &'exec mut Option<HashMap<String, Value<'exec>>>,
2021
}
2122

22-
pub struct OnExecuteStartPayload<'exec> {
23+
pub struct OnExecutePayload<'exec> {
2324
pub router_http_request: &'exec HttpRequest,
2425
pub query_plan: Arc<QueryPlan>,
2526

2627
pub data: &'exec mut Value<'exec>,
2728
pub errors: &'exec mut Vec<GraphQLError>,
28-
pub extensions: Option<&'exec mut sonic_rs::Value>,
29+
pub extensions: &'exec mut HashMap<String, sonic_rs::Value>,
2930

3031
pub skip_execution: bool,
3132

3233
pub variable_values: &'exec Option<HashMap<String, sonic_rs::Value>>,
3334
}
3435

35-
pub trait OnExecuteStart {
36-
fn on_execute_start(&self, payload: OnExecuteStartPayload) -> ControlFlow;
36+
pub trait RouterPlugin {
37+
fn on_execute<'exec>(
38+
&self,
39+
_payload: OnExecutePayload<'exec>,
40+
) -> ControlFlow<'exec, OnExecutePayload<'exec>> {
41+
ControlFlow::Continue
42+
}
43+
fn on_schema_reload(&self, _payload: OnSchemaReloadPayload) {}
3744
}
3845

3946
pub struct OnExecuteEndPayload<'exec> {
@@ -47,15 +54,7 @@ pub struct OnExecuteEndPayload<'exec> {
4754
pub variable_values: &'exec Option<HashMap<String, sonic_rs::Value>>,
4855
}
4956

50-
pub trait OnExecuteEnd {
51-
fn on_execute_end(&self, payload: OnExecuteEndPayload) -> ControlFlow;
52-
}
53-
5457
pub struct OnSchemaReloadPayload {
5558
pub old_schema: &'static ConsumerSchema,
5659
pub new_schema: &'static mut ConsumerSchema,
5760
}
58-
59-
pub trait OnSchemaReload {
60-
fn on_schema_reload(&self, payload: OnSchemaReloadPayload);
61-
}

0 commit comments

Comments
 (0)