improve install performance

This commit is contained in:
Aiden McClelland
2024-07-29 18:44:56 -06:00
parent bca75a3ea4
commit 5c153c9e21
4 changed files with 111 additions and 49 deletions

View File

@@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::sync::Arc;
use chrono::{DateTime, Utc};
use helpers::NonDetachingJoinHandle;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use tokio::io::AsyncWrite;
@@ -14,8 +15,9 @@ use crate::registry::signer::commitment::{Commitment, Digestable};
use crate::registry::signer::sign::{AnySignature, AnyVerifyingKey};
use crate::registry::signer::AcceptSigners;
use crate::s9pk::merkle_archive::source::http::HttpSource;
use crate::s9pk::merkle_archive::source::Section;
use crate::s9pk::merkle_archive::source::{ArchiveSource, Section};
use crate::s9pk::S9pk;
use crate::upload::UploadingFile;
#[derive(Debug, Deserialize, Serialize, TS)]
#[serde(rename_all = "camelCase")]
@@ -70,4 +72,42 @@ impl RegistryAsset<MerkleArchiveCommitment> {
)
.await
}
pub async fn deserialize_s9pk_buffered(
&self,
client: Client,
) -> Result<S9pk<Section<Arc<BufferedHttpSource>>>, Error> {
S9pk::deserialize(
&Arc::new(BufferedHttpSource::new(client, self.url.clone()).await?),
Some(&self.commitment),
)
.await
}
}
pub struct BufferedHttpSource {
_download: NonDetachingJoinHandle<()>,
file: UploadingFile,
}
impl BufferedHttpSource {
pub async fn new(client: Client, url: Url) -> Result<Self, Error> {
let (mut handle, file) = UploadingFile::new().await?;
let response = client.get(url).send().await?;
Ok(Self {
_download: tokio::spawn(async move { handle.download(response).await }).into(),
file,
})
}
}
impl ArchiveSource for BufferedHttpSource {
type FetchReader = <UploadingFile as ArchiveSource>::FetchReader;
type FetchAllReader = <UploadingFile as ArchiveSource>::FetchAllReader;
async fn size(&self) -> Option<u64> {
self.file.size().await
}
async fn fetch_all(&self) -> Result<Self::FetchAllReader, Error> {
self.file.fetch_all().await
}
async fn fetch(&self, position: u64, size: u64) -> Result<Self::FetchReader, Error> {
self.file.fetch(position, size).await
}
}