Skip to content

Commit

Permalink
Merge pull request #185 from ikatson/session-db
Browse files Browse the repository at this point in the history
[Features] Support for pluggable session persistence storages
  • Loading branch information
ikatson authored Aug 15, 2024
2 parents c4fc107 + 275b3b0 commit 9d39411
Show file tree
Hide file tree
Showing 18 changed files with 581 additions and 365 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion crates/librqbit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ sha1w = { path = "../sha1w", default-features = false, package = "librqbit-sha1-
dht = { path = "../dht", package = "librqbit-dht", version = "5.0.4" }
librqbit-upnp = { path = "../upnp", version = "0.1.0" }

tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1", features = [
"macros",
"rt-multi-thread",
"fs",
"io-util",
] }
axum = { version = "0.7.4" }
tower-http = { version = "0.5", features = ["cors", "trace"] }
tokio-stream = "0.1"
Expand Down Expand Up @@ -79,6 +84,7 @@ memmap2 = { version = "0.9.4" }
lru = { version = "0.12.3", optional = true }
mime_guess = { version = "2.0.5", default-features = false }
tokio-socks = "0.5.2"
async-trait = "0.1.81"

[build-dependencies]
anyhow = "1"
Expand Down
2 changes: 1 addition & 1 deletion crates/librqbit/examples/custom_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn main() -> anyhow::Result<()> {
Default::default(),
SessionOptions {
disable_dht_persistence: true,
persistence: false,
persistence: None,
listen_port_range: None,
enable_upnp_port_forwarding: false,
..Default::default()
Expand Down
19 changes: 12 additions & 7 deletions crates/librqbit/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,46 +96,51 @@ impl Api {
.per_peer_stats_snapshot(filter))
}

pub fn api_torrent_action_pause(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
pub async fn api_torrent_action_pause(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
let handle = self.mgr_handle(idx)?;
handle
.pause()
self.session()
.pause(&handle)
.await
.context("error pausing torrent")
.with_error_status_code(StatusCode::BAD_REQUEST)?;
Ok(Default::default())
}

pub fn api_torrent_action_start(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
pub async fn api_torrent_action_start(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
let handle = self.mgr_handle(idx)?;
self.session
.unpause(&handle)
.await
.context("error unpausing torrent")
.with_error_status_code(StatusCode::BAD_REQUEST)?;
Ok(Default::default())
}

pub fn api_torrent_action_forget(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
pub async fn api_torrent_action_forget(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
self.session
.delete(idx, false)
.await
.context("error forgetting torrent")?;
Ok(Default::default())
}

pub fn api_torrent_action_delete(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
pub async fn api_torrent_action_delete(&self, idx: TorrentId) -> Result<EmptyJsonResponse> {
self.session
.delete(idx, true)
.await
.context("error deleting torrent with files")?;
Ok(Default::default())
}

pub fn api_torrent_action_update_only_files(
pub async fn api_torrent_action_update_only_files(
&self,
idx: TorrentId,
only_files: &HashSet<usize>,
) -> Result<EmptyJsonResponse> {
let handle = self.mgr_handle(idx)?;
self.session
.update_only_files(&handle, only_files)
.await
.context("error updating only_files")?;
Ok(Default::default())
}
Expand Down
9 changes: 5 additions & 4 deletions crates/librqbit/src/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,28 +368,28 @@ impl HttpApi {
State(state): State<ApiState>,
Path(idx): Path<usize>,
) -> Result<impl IntoResponse> {
state.api_torrent_action_pause(idx).map(axum::Json)
state.api_torrent_action_pause(idx).await.map(axum::Json)
}

async fn torrent_action_start(
State(state): State<ApiState>,
Path(idx): Path<usize>,
) -> Result<impl IntoResponse> {
state.api_torrent_action_start(idx).map(axum::Json)
state.api_torrent_action_start(idx).await.map(axum::Json)
}

async fn torrent_action_forget(
State(state): State<ApiState>,
Path(idx): Path<usize>,
) -> Result<impl IntoResponse> {
state.api_torrent_action_forget(idx).map(axum::Json)
state.api_torrent_action_forget(idx).await.map(axum::Json)
}

async fn torrent_action_delete(
State(state): State<ApiState>,
Path(idx): Path<usize>,
) -> Result<impl IntoResponse> {
state.api_torrent_action_delete(idx).map(axum::Json)
state.api_torrent_action_delete(idx).await.map(axum::Json)
}

#[derive(Deserialize)]
Expand All @@ -404,6 +404,7 @@ impl HttpApi {
) -> Result<impl IntoResponse> {
state
.api_torrent_action_update_only_files(idx, &req.only_files.into_iter().collect())
.await
.map(axum::Json)
}

Expand Down
3 changes: 2 additions & 1 deletion crates/librqbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod peer_connection;
mod peer_info_reader;
mod read_buf;
mod session;
mod session_persistence;
mod spawn_utils;
pub mod storage;
mod stream_connect;
Expand All @@ -53,7 +54,7 @@ pub use dht;
pub use peer_connection::PeerConnectionOptions;
pub use session::{
AddTorrent, AddTorrentOptions, AddTorrentResponse, ListOnlyResponse, Session, SessionOptions,
SUPPORTED_SCHEMES,
SessionPersistenceConfig, SUPPORTED_SCHEMES,
};
pub use spawn_utils::spawn as librqbit_spawn;
pub use torrent_state::{
Expand Down
Loading

0 comments on commit 9d39411

Please sign in to comment.