Add fastresume session option

This commit is contained in:
Igor Katson 2024-08-20 21:31:52 +01:00
parent 29c46f2579
commit e11744cd63
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
3 changed files with 18 additions and 2 deletions

View file

@ -17,6 +17,7 @@ impl BitVFactory for NonPersistentBitVFactory {
async fn load(&self, _: TorrentIdOrHash) -> anyhow::Result<Option<Box<dyn BitV>>> { async fn load(&self, _: TorrentIdOrHash) -> anyhow::Result<Option<Box<dyn BitV>>> {
Ok(None) Ok(None)
} }
async fn store_initial_check( async fn store_initial_check(
&self, &self,
_id: TorrentIdOrHash, _id: TorrentIdOrHash,

View file

@ -371,6 +371,9 @@ pub struct SessionOptions {
/// librqbit instances at a time. /// librqbit instances at a time.
pub dht_config: Option<PersistentDhtConfig>, pub dht_config: Option<PersistentDhtConfig>,
/// Enable fastresume, to restore state quickly after restart.
pub fastresume: bool,
/// Turn on to dump session contents into a file periodically, so that on next start /// Turn on to dump session contents into a file periodically, so that on next start
/// all remembered torrents will continue where they left off. /// all remembered torrents will continue where they left off.
pub persistence: Option<SessionPersistenceConfig>, pub persistence: Option<SessionPersistenceConfig>,
@ -507,6 +510,17 @@ impl Session {
async fn persistence_factory( async fn persistence_factory(
opts: &SessionOptions, opts: &SessionOptions,
) -> anyhow::Result<(Option<Arc<dyn SessionPersistenceStore>>, Arc<dyn BitVFactory>)> { ) -> anyhow::Result<(Option<Arc<dyn SessionPersistenceStore>>, Arc<dyn BitVFactory>)> {
macro_rules! make_result {
($store:expr) => {
if opts.fastresume {
Ok((Some($store.clone()), $store))
} else {
Ok((Some($store), Arc::new(NonPersistentBitVFactory {})))
}
};
}
match &opts.persistence { match &opts.persistence {
Some(SessionPersistenceConfig::Json { folder }) => { Some(SessionPersistenceConfig::Json { folder }) => {
let folder = match folder.as_ref() { let folder = match folder.as_ref() {
@ -520,13 +534,13 @@ impl Session {
.context("error initializing JsonSessionPersistenceStore")?, .context("error initializing JsonSessionPersistenceStore")?,
); );
Ok((Some(s.clone()), s)) make_result!(s)
}, },
#[cfg(feature = "postgres")] #[cfg(feature = "postgres")]
Some(SessionPersistenceConfig::Postgres { connection_string }) => { Some(SessionPersistenceConfig::Postgres { connection_string }) => {
use crate::session_persistence::postgres::PostgresSessionStorage; use crate::session_persistence::postgres::PostgresSessionStorage;
let p = Arc::new(PostgresSessionStorage::new(connection_string).await?); let p = Arc::new(PostgresSessionStorage::new(connection_string).await?);
Ok((Some(p.clone()), p)) make_result!(p)
} }
None => Ok((None, Arc::new(NonPersistentBitVFactory {}))), None => Ok((None, Arc::new(NonPersistentBitVFactory {}))),
} }

View file

@ -341,6 +341,7 @@ async fn async_main(opts: Opts) -> anyhow::Result<()> {
socks_proxy_url: socks_url, socks_proxy_url: socks_url,
concurrent_init_limit: Some(opts.concurrent_init_limit), concurrent_init_limit: Some(opts.concurrent_init_limit),
root_span: None, root_span: None,
fastresume: false,
}; };
let stats_printer = |session: Arc<Session>| async move { let stats_printer = |session: Arc<Session>| async move {