-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from kozalosev/bugfix/callbacks
Fix callbacks and caching
- Loading branch information
Showing
9 changed files
with
152 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use http_cache::{CacheMode, HitOrMiss, HttpCache, MokaManager, XCACHELOOKUP}; | ||
use http_cache_reqwest::{Cache, CacheOptions}; | ||
use reqwest::header::HeaderValue; | ||
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; | ||
|
||
pub fn caching_client() -> ClientWithMiddleware { | ||
let client = reqwest::Client::builder() | ||
.build().expect("couldn't create an HTTP client"); | ||
let client = ClientBuilder::new(client) | ||
.with(Cache(HttpCache { | ||
mode: CacheMode::Default, | ||
manager: MokaManager::default(), | ||
options: Some(CacheOptions::default()), | ||
})) | ||
.build(); | ||
client | ||
} | ||
|
||
pub trait WithCachedResponseCounters { | ||
fn cached_resp_counter(&self) -> &prometheus::Counter; | ||
fn fetched_resp_counter(&self) -> &prometheus::Counter; | ||
|
||
fn inc_resp_counter(&self, resp: &reqwest::Response) { | ||
let resp_counter = if from_cache(resp) { | ||
self.cached_resp_counter() | ||
} else { | ||
self.fetched_resp_counter() | ||
}; | ||
resp_counter.inc(); | ||
} | ||
} | ||
|
||
fn from_cache(resp: &reqwest::Response) -> bool { | ||
log::debug!("Response headers: {:?}", resp.headers()); | ||
|
||
let hit = HitOrMiss::HIT.to_string(); | ||
let predicate = |x: &&HeaderValue| { | ||
let value = x.to_str().unwrap_or(""); | ||
value == hit | ||
}; | ||
resp.headers() | ||
.get(XCACHELOOKUP) | ||
.filter(predicate) | ||
.is_some() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.