-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
61 additions
and
170 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,54 +1,54 @@ | ||
use std::{sync::Arc, time::Duration}; | ||
|
||
use anyhow::Result; | ||
use crates_index::{Crate, GitIndex}; | ||
use parking_lot::Mutex; | ||
use tokio::{ | ||
task::spawn_blocking, | ||
time::{self, MissedTickBehavior}, | ||
}; | ||
use std::sync::Arc; | ||
|
||
use crate::models::crates::CrateName; | ||
use crates_index::Crate; | ||
use crates_index::SparseIndex; | ||
|
||
#[derive(Clone)] | ||
pub struct ManagedIndex { | ||
index: Arc<Mutex<GitIndex>>, | ||
index: Arc<SparseIndex>, | ||
client: reqwest::Client, | ||
} | ||
|
||
#[cfg(test)] | ||
static_assertions::assert_impl_all!(ManagedIndex: Send, Sync); | ||
|
||
impl ManagedIndex { | ||
pub fn new() -> Self { | ||
pub fn new(client: reqwest::Client) -> Self { | ||
// the index path is configurable through the `CARGO_HOME` env variable | ||
let index = Arc::new(Mutex::new(GitIndex::new_cargo_default().unwrap())); | ||
|
||
Self { index } | ||
} | ||
let index = Arc::new(SparseIndex::new_cargo_default().unwrap()); | ||
|
||
pub fn crate_(&self, crate_name: CrateName) -> Option<Crate> { | ||
self.index.lock().crate_(crate_name.as_ref()) | ||
Self { index, client } | ||
} | ||
|
||
pub async fn refresh_at_interval(&self, update_interval: Duration) { | ||
let mut update_interval = time::interval(update_interval); | ||
update_interval.set_missed_tick_behavior(MissedTickBehavior::Delay); | ||
|
||
loop { | ||
if let Err(err) = self.refresh().await { | ||
tracing::error!( | ||
"failed refreshing the crates.io-index, the operation will be retried: {}", | ||
error_reporter::Report::new(err), | ||
); | ||
} | ||
update_interval.tick().await; | ||
} | ||
} | ||
|
||
async fn refresh(&self) -> Result<(), crates_index::Error> { | ||
let index = Arc::clone(&self.index); | ||
|
||
spawn_blocking(move || index.lock().update()) | ||
.await | ||
.expect("blocking index update task should never panic")?; | ||
|
||
Ok(()) | ||
pub async fn crate_(&self, crate_name: CrateName) -> Option<Crate> { | ||
let req = self | ||
.index | ||
.make_cache_request(crate_name.as_str()) | ||
.unwrap() | ||
.body(()) | ||
.unwrap() | ||
.into_parts() | ||
.0; | ||
let req = http::Request::from_parts(req, Vec::new()); | ||
let req = reqwest::Request::try_from(req).unwrap(); | ||
|
||
let res = self.client.execute(req).await.unwrap(); | ||
|
||
let mut builder = http::Response::builder() | ||
.status(res.status()) | ||
.version(res.version()); | ||
|
||
builder | ||
.headers_mut() | ||
.unwrap() | ||
.extend(res.headers().iter().map(|(k, v)| (k.clone(), v.clone()))); | ||
|
||
let body = res.bytes().await.unwrap(); | ||
let res = builder.body(body.to_vec()).unwrap(); | ||
|
||
self.index | ||
.parse_cache_response(crate_name.as_str(), res, true) | ||
.unwrap() | ||
} | ||
} |