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
#[cfg(test)]
mod test;
pub mod error;
use crate::client::HyperClient;
use async_trait::async_trait;
use bytes::Bytes;
use error::{HttpCacheError, HyperClientError};
use error_stack::{IntoReport, Report, ResultExt};
use headers::{CacheControl, HeaderMapExt};
use http::Uri;
use hyper::{self, body::to_bytes};
use serde::de::DeserializeOwned;
use serde_json::from_slice;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::Mutex;
use tokio::sync::RwLock;
#[derive(Clone, Debug)]
struct Cache<ContentT> {
expires_at: SystemTime,
content: ContentT,
}
impl<ContentT> Cache<ContentT> {
pub fn new(max_age: Duration, content: ContentT) -> Self {
Self {
expires_at: SystemTime::now() + max_age,
content,
}
}
pub fn is_expired(&self) -> bool {
self.expires_at <= SystemTime::now()
}
pub fn update(&mut self, max_age: Duration, content: ContentT) {
self.expires_at = SystemTime::now() + max_age;
self.content = content;
}
}
#[derive(Clone, Debug)]
pub struct Resource {
pub data: Bytes,
pub max_age: Duration,
}
#[async_trait]
pub trait CacheClient: Sized + Send + Sync
where
Self::Error: std::error::Error + Send + Sync + 'static,
{
type Error;
async fn fetch(&self, uri: &Uri) -> Result<Resource, Report<Self::Error>>;
}
#[async_trait]
impl CacheClient for HyperClient {
type Error = HyperClientError;
async fn fetch(&self, uri: &Uri) -> Result<Resource, Report<Self::Error>> {
let response = self
.get(uri.clone())
.await
.into_report()
.change_context(HyperClientError::FailedToFetch)?;
let status = response.status();
if !status.is_success() {
return Err(Report::new(HyperClientError::BadHttpResponse(status)));
}
let cache_header: Option<CacheControl> = response.headers().typed_get();
let body = to_bytes(response)
.await
.into_report()
.change_context(HyperClientError::FailedToFetch)?;
if let Some(cache_header) = cache_header {
let ttl = cache_header
.s_max_age()
.unwrap_or_else(|| cache_header.max_age().unwrap_or_default());
return Ok(Resource {
data: body,
max_age: ttl,
});
}
Ok(Resource {
data: body,
max_age: Duration::default(),
})
}
}
pub struct HttpCache<CacheClientT, ContentT> {
client: CacheClientT,
path: Uri,
cache: Arc<RwLock<Cache<ContentT>>>,
refresh: Mutex<()>,
}
impl<CacheClientT, ContentT> HttpCache<CacheClientT, ContentT>
where
CacheClientT: CacheClient,
ContentT: DeserializeOwned + Clone + Send + Sync,
{
pub async fn new(client: CacheClientT, path: Uri) -> Result<Self, Report<HttpCacheError>> {
let resource = client.fetch(&path).await.change_context(HttpCacheError)?;
let initial_cache: Cache<ContentT> = Cache::new(
resource.max_age,
from_slice(&resource.data)
.into_report()
.change_context(HttpCacheError)?,
);
Ok(Self {
client,
path,
cache: Arc::new(RwLock::new(initial_cache)),
refresh: Mutex::new(()),
})
}
pub async fn get(&self) -> Result<ContentT, Report<HttpCacheError>> {
let cache = self.cache.read().await.clone();
if cache.is_expired() {
let _refresh_guard = self.refresh.lock().await;
let cache = self.cache.read().await.clone();
if !cache.is_expired() {
return Ok(cache.content);
}
let resource = self
.client
.fetch(&self.path)
.await
.change_context(HttpCacheError)?;
let content: ContentT = from_slice(&resource.data)
.into_report()
.change_context(HttpCacheError)?;
self.cache
.write()
.await
.update(resource.max_age, content.clone());
return Ok(content);
}
Ok(cache.content)
}
}