Skip to content

Commit 5bf73ce

Browse files
committed
Iteration
1 parent 018dba9 commit 5bf73ce

File tree

2 files changed

+28
-48
lines changed

2 files changed

+28
-48
lines changed

lib/executor/src/plugins/response_cache.rs

Lines changed: 13 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, OnExecuteControlFlow, 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+
&'static self,
30+
payload: OnExecutePayload<'exec>,
31+
) -> OnExecuteControlFlow<'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+
return ControlFlow::OnEnd(Box::new(move |payload| {
46+
// Do not cache if there are errors
47+
if !payload.errors.is_empty() {
48+
return ();
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,15 @@ 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+
}));
10283
}
10384
ControlFlow::Continue
10485
}
105-
}
106-
107-
impl OnSchemaReload for ResponseCachePlugin {
10886
fn on_schema_reload(&self, payload: OnSchemaReloadPayload) {
10987
// Visit the schema and update ttl_per_type based on some directive
11088
payload

lib/executor/src/plugins/traits.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::hash::Hash;
12
use std::{collections::HashMap, sync::Arc};
23

34
use hive_router_query_planner::consumer_schema::ConsumerSchema;
@@ -8,8 +9,9 @@ use ntex::web::HttpResponse;
89
use crate::response::graphql_error::GraphQLError;
910
use crate::response::value::Value;
1011

11-
pub enum ControlFlow {
12+
pub enum ControlFlow<TEndPayload> {
1213
Continue,
14+
OnEnd(Box<dyn FnOnce(TEndPayload) -> ()>),
1315
Break(HttpResponse),
1416
}
1517

@@ -19,21 +21,29 @@ pub struct ExecutionResult<'exec> {
1921
pub extensions: &'exec mut Option<HashMap<String, Value<'exec>>>,
2022
}
2123

22-
pub struct OnExecuteStartPayload<'exec> {
24+
pub struct OnExecutePayload<'exec> {
2325
pub router_http_request: &'exec HttpRequest,
2426
pub query_plan: Arc<QueryPlan>,
2527

2628
pub data: &'exec mut Value<'exec>,
2729
pub errors: &'exec mut Vec<GraphQLError>,
28-
pub extensions: Option<&'exec mut sonic_rs::Value>,
30+
pub extensions: &'exec mut HashMap<String, sonic_rs::Value>,
2931

3032
pub skip_execution: bool,
3133

3234
pub variable_values: &'exec Option<HashMap<String, sonic_rs::Value>>,
3335
}
3436

35-
pub trait OnExecuteStart {
36-
fn on_execute_start(&self, payload: OnExecuteStartPayload) -> ControlFlow;
37+
pub type OnExecuteControlFlow<'exec> = ControlFlow<OnExecutePayload<'exec>>;
38+
39+
pub trait RouterPlugin {
40+
fn on_execute<'exec>(
41+
&'static self,
42+
_payload: OnExecutePayload<'exec>,
43+
) -> ControlFlow<OnExecutePayload<'exec>> {
44+
ControlFlow::Continue
45+
}
46+
fn on_schema_reload(&self, _payload: OnSchemaReloadPayload) {}
3747
}
3848

3949
pub struct OnExecuteEndPayload<'exec> {
@@ -47,15 +57,7 @@ pub struct OnExecuteEndPayload<'exec> {
4757
pub variable_values: &'exec Option<HashMap<String, sonic_rs::Value>>,
4858
}
4959

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

0 commit comments

Comments
 (0)