forked from tailcallhq/tailcall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_context.rs
More file actions
205 lines (181 loc) · 7.05 KB
/
Copy pathrequest_context.rs
File metadata and controls
205 lines (181 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::num::NonZeroU64;
use std::sync::{Arc, Mutex};
use async_graphql_value::ConstValue;
use cache_control::{Cachability, CacheControl};
use derive_setters::Setters;
use hyper::HeaderMap;
use crate::blueprint::{Server, Upstream};
use crate::data_loader::DataLoader;
use crate::graphql::GraphqlDataLoader;
use crate::grpc::data_loader::GrpcDataLoader;
use crate::http::{AppContext, DataLoaderRequest, HttpDataLoader};
use crate::{grpc, EntityCache, EnvIO, HttpIO};
#[derive(Setters)]
pub struct RequestContext {
// TODO: consider storing http clients where they are used i.e. expression and dataloaders
pub h_client: Arc<dyn HttpIO>,
// http2 only client is required for grpc in cases the server supports only http2
// and the request will fail on protocol negotiation
// having separate client for now looks like the only way to do with reqwest
pub h2_client: Arc<dyn HttpIO>,
pub server: Server,
pub upstream: Upstream,
pub req_headers: HeaderMap,
pub http_data_loaders: Arc<Vec<DataLoader<DataLoaderRequest, HttpDataLoader>>>,
pub gql_data_loaders: Arc<Vec<DataLoader<DataLoaderRequest, GraphqlDataLoader>>>,
pub grpc_data_loaders: Arc<Vec<DataLoader<grpc::DataLoaderRequest, GrpcDataLoader>>>,
pub min_max_age: Arc<Mutex<Option<i32>>>,
pub cache_public: Arc<Mutex<Option<bool>>>,
pub env_vars: Arc<dyn EnvIO>,
pub cache: Arc<EntityCache>,
}
impl RequestContext {
fn set_min_max_age_conc(&self, min_max_age: i32) {
*self.min_max_age.lock().unwrap() = Some(min_max_age);
}
pub fn get_min_max_age(&self) -> Option<i32> {
*self.min_max_age.lock().unwrap()
}
pub fn set_cache_public_false(&self) {
*self.cache_public.lock().unwrap() = Some(false);
}
pub fn is_cache_public(&self) -> Option<bool> {
*self.cache_public.lock().unwrap()
}
pub fn set_min_max_age(&self, max_age: i32) {
let min_max_age_lock = self.get_min_max_age();
match min_max_age_lock {
Some(min_max_age) if max_age < min_max_age => {
self.set_min_max_age_conc(max_age);
}
None => {
self.set_min_max_age_conc(max_age);
}
_ => {}
}
}
pub fn set_cache_visibility(&self, cachability: &Option<Cachability>) {
if let Some(Cachability::Private) = cachability {
self.set_cache_public_false()
}
}
pub fn set_cache_control(&self, cache_policy: CacheControl) {
if let Some(max_age) = cache_policy.max_age {
self.set_min_max_age(max_age.as_secs() as i32);
}
self.set_cache_visibility(&cache_policy.cachability);
if Some(Cachability::NoCache) == cache_policy.cachability {
self.set_min_max_age(-1);
}
}
pub async fn cache_get(&self, key: &u64) -> anyhow::Result<Option<ConstValue>> {
self.cache.get(key).await
}
#[allow(clippy::too_many_arguments)]
pub async fn cache_insert(
&self,
key: u64,
value: ConstValue,
ttl: NonZeroU64,
) -> anyhow::Result<()> {
self.cache.set(key, value, ttl).await
}
pub fn is_batching_enabled(&self) -> bool {
self.upstream.is_batching_enabled()
}
}
impl From<&AppContext> for RequestContext {
fn from(app_ctx: &AppContext) -> Self {
Self {
h_client: app_ctx.runtime.http.clone(),
h2_client: app_ctx.runtime.http2_only.clone(),
server: app_ctx.blueprint.server.clone(),
upstream: app_ctx.blueprint.upstream.clone(),
req_headers: HeaderMap::new(),
http_data_loaders: app_ctx.http_data_loaders.clone(),
gql_data_loaders: app_ctx.gql_data_loaders.clone(),
cache: app_ctx.runtime.cache.clone(),
grpc_data_loaders: app_ctx.grpc_data_loaders.clone(),
min_max_age: Arc::new(Mutex::new(None)),
cache_public: Arc::new(Mutex::new(None)),
env_vars: app_ctx.runtime.env.clone(),
}
}
}
#[cfg(test)]
mod test {
use std::sync::{Arc, Mutex};
use cache_control::Cachability;
use hyper::HeaderMap;
use crate::blueprint::{Server, Upstream};
use crate::cache::InMemoryCache;
use crate::cli::{init_env, init_http, init_http2_only};
use crate::config::{self, Batch};
use crate::http::RequestContext;
impl Default for RequestContext {
fn default() -> Self {
let crate::config::Config { server, upstream, .. } = crate::config::Config::default();
//TODO: default is used only in tests. Drop default and move it to test.
let server = Server::try_from(server).unwrap();
let upstream = Upstream::try_from(upstream).unwrap();
let h_client = init_http(&upstream, None);
let h2_client = init_http2_only(&upstream.clone(), None);
RequestContext {
req_headers: HeaderMap::new(),
h_client,
h2_client,
server,
upstream,
http_data_loaders: Arc::new(vec![]),
gql_data_loaders: Arc::new(vec![]),
cache: Arc::new(InMemoryCache::default()),
grpc_data_loaders: Arc::new(vec![]),
min_max_age: Arc::new(Mutex::new(None)),
cache_public: Arc::new(Mutex::new(None)),
env_vars: init_env(),
}
}
}
#[test]
fn test_update_max_age_less_than_existing() {
let req_ctx = RequestContext::default();
req_ctx.set_min_max_age(120);
req_ctx.set_min_max_age(60);
assert_eq!(req_ctx.get_min_max_age(), Some(60));
}
#[test]
fn test_update_max_age_greater_than_existing() {
let req_ctx = RequestContext::default();
req_ctx.set_min_max_age(60);
req_ctx.set_min_max_age(120);
assert_eq!(req_ctx.get_min_max_age(), Some(60));
}
#[test]
fn test_update_max_age_no_existing_value() {
let req_ctx = RequestContext::default();
req_ctx.set_min_max_age(120);
assert_eq!(req_ctx.get_min_max_age(), Some(120));
}
#[test]
fn test_update_cache_visibility_private() {
let req_ctx = RequestContext::default();
req_ctx.set_cache_visibility(&Some(Cachability::Private));
assert_eq!(req_ctx.is_cache_public(), Some(false));
}
#[test]
fn test_update_cache_visibility_public() {
let req_ctx: RequestContext = RequestContext::default();
req_ctx.set_cache_visibility(&Some(Cachability::Public));
assert_eq!(req_ctx.is_cache_public(), None);
}
#[test]
fn test_is_batching_enabled_default() {
// create ctx with default batch
let config = config::Config::default();
let server = Server::try_from(config.server.clone()).unwrap();
let mut upstream = Upstream::try_from(config.upstream.clone()).unwrap();
upstream.batch = Some(Batch::default());
let req_ctx: RequestContext = RequestContext::default().upstream(upstream).server(server);
assert!(req_ctx.is_batching_enabled());
}
}