Skip to content

Commit

Permalink
[feature] HTTP API timeouts (#290)
Browse files Browse the repository at this point in the history
* write an extractor for timeout

* Use timeout

* Default timeout 10min
  • Loading branch information
ikatson authored Dec 4, 2024
1 parent a1de63e commit fce467e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
81 changes: 75 additions & 6 deletions crates/librqbit/src/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,67 @@ async fn simple_basic_auth(
}
}

mod timeout {
use std::time::Duration;

use anyhow::Context;
use axum::{extract::Query, RequestPartsExt};
use http::request::Parts;
use serde::Deserialize;

use crate::ApiError;

pub struct Timeout<const DEFAULT_MS: usize, const MAX_MS: usize>(pub Duration);

#[async_trait::async_trait]
impl<S, const DEFAULT_MS: usize, const MAX_MS: usize> axum::extract::FromRequestParts<S>
for Timeout<DEFAULT_MS, MAX_MS>
where
S: Send + Sync,
{
type Rejection = ApiError;

/// Perform the extraction.
async fn from_request_parts(
parts: &mut Parts,
_state: &S,
) -> Result<Self, Self::Rejection> {
#[derive(Deserialize)]
struct QueryT {
timeout_ms: Option<usize>,
}

let q = parts
.extract::<Query<QueryT>>()
.await
.context("error running Timeout extractor")?;

let timeout_ms = q
.timeout_ms
.map(Ok)
.or_else(|| {
parts
.headers
.get("x-req-timeout-ms")
.map(|v| {
std::str::from_utf8(v.as_bytes())
.context("invalid utf-8 in timeout value")
})
.map(|v| {
v.and_then(|v| v.parse::<usize>().context("invalid timeout integer"))
})
})
.transpose()
.context("error parsing timeout")?
.unwrap_or(DEFAULT_MS);
let timeout_ms = timeout_ms.min(MAX_MS);
Ok(Timeout(Duration::from_millis(timeout_ms as u64)))
}
}
}

use timeout::Timeout;

impl HttpApi {
pub fn new(api: Api, opts: Option<HttpApiOptions>) -> Self {
Self {
Expand Down Expand Up @@ -152,6 +213,7 @@ impl HttpApi {
async fn torrents_post(
State(state): State<ApiState>,
Query(params): Query<TorrentAddQueryParams>,
Timeout(timeout): Timeout<600_000, 3_600_000>,
data: Bytes,
) -> Result<impl IntoResponse> {
let is_url = params.is_url;
Expand Down Expand Up @@ -185,7 +247,10 @@ impl HttpApi {
}
_ => AddTorrent::TorrentFileBytes(data.into()),
};
state.api_add_torrent(add, Some(opts)).await.map(axum::Json)
tokio::time::timeout(timeout, state.api_add_torrent(add, Some(opts)))
.await
.context("timeout")?
.map(axum::Json)
}

async fn torrent_details(
Expand Down Expand Up @@ -257,19 +322,23 @@ impl HttpApi {

async fn resolve_magnet(
State(state): State<ApiState>,
Timeout(timeout): Timeout<600_000, 3_600_000>,
inp_headers: HeaderMap,
url: String,
) -> Result<impl IntoResponse> {
let added = state
.session()
.add_torrent(
let added = tokio::time::timeout(
timeout,
state.session().add_torrent(
AddTorrent::from_url(&url),
Some(AddTorrentOptions {
list_only: true,
..Default::default()
}),
)
.await?;
),
)
.await
.context("timeout")??;

let (info, content) = match added {
crate::AddTorrentResponse::AlreadyManaged(_, handle) => (
handle.shared().info.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/librqbit/webui/src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
// Define API URL and base path
const apiUrl = (() => {
if (window.origin === "null" || window.origin === "http://localhost:3031") {
return "http://localhost:3030"
return "http://localhost:3030";
}
let port = /http.*:\/\/.*:(\d+)/.exec(window.origin)?.[1];
if (port == "3031") {
Expand Down

0 comments on commit fce467e

Please sign in to comment.