Compare commits
2 Commits
release-20
...
release-20
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d15392e148 | ||
|
|
f3121898e2 |
@@ -45,6 +45,7 @@ pub struct AppState {
|
||||
pub db: db::Db,
|
||||
#[cfg(feature = "push-notifications")]
|
||||
pub push_store: push::PushSubscriptionStore,
|
||||
pub notify_poll: Arc<tokio::sync::Notify>,
|
||||
}
|
||||
|
||||
async fn auth_middleware(
|
||||
@@ -336,6 +337,8 @@ async fn main() {
|
||||
#[cfg(not(feature = "push-notifications"))]
|
||||
let push_store = ();
|
||||
|
||||
let notify_poll = Arc::new(tokio::sync::Notify::new());
|
||||
|
||||
let app_state = AppState {
|
||||
tx: tx.clone(),
|
||||
event_bus: event_bus.clone(),
|
||||
@@ -343,6 +346,7 @@ async fn main() {
|
||||
db: db.clone(),
|
||||
#[cfg(feature = "push-notifications")]
|
||||
push_store,
|
||||
notify_poll: notify_poll.clone(),
|
||||
};
|
||||
|
||||
// Spawn background task to poll rTorrent
|
||||
@@ -351,6 +355,7 @@ async fn main() {
|
||||
let socket_path = args.socket.clone(); // Clone for background task
|
||||
#[cfg(feature = "push-notifications")]
|
||||
let push_store_clone = app_state.push_store.clone();
|
||||
let notify_poll_clone = notify_poll.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let client = xmlrpc::RtorrentClient::new(&socket_path);
|
||||
@@ -438,8 +443,13 @@ async fn main() {
|
||||
|
||||
previous_torrents = new_torrents;
|
||||
|
||||
// Success case: sleep for the determined interval
|
||||
tokio::time::sleep(loop_interval).await;
|
||||
// Success case: wait for the determined interval OR a wakeup notification
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(loop_interval) => {},
|
||||
_ = notify_poll_clone.notified() => {
|
||||
tracing::debug!("Background loop awakened by new client connection");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Error fetching torrents in background: {}", e);
|
||||
|
||||
@@ -11,6 +11,8 @@ pub enum ScgiError {
|
||||
#[allow(dead_code)]
|
||||
#[error("Protocol Error: {0}")]
|
||||
Protocol(String),
|
||||
#[error("Timeout: SCGI request took too long")]
|
||||
Timeout,
|
||||
}
|
||||
|
||||
pub struct ScgiRequest {
|
||||
@@ -78,20 +80,30 @@ impl ScgiRequest {
|
||||
}
|
||||
|
||||
pub async fn send_request(socket_path: &str, request: ScgiRequest) -> Result<Bytes, ScgiError> {
|
||||
let perform_request = async {
|
||||
let mut stream = UnixStream::connect(socket_path).await?;
|
||||
let data = request.encode();
|
||||
stream.write_all(&data).await?;
|
||||
|
||||
let mut response = Vec::new();
|
||||
stream.read_to_end(&mut response).await?;
|
||||
Ok::<Vec<u8>, std::io::Error>(response)
|
||||
};
|
||||
|
||||
let response = tokio::time::timeout(std::time::Duration::from_secs(10), perform_request)
|
||||
.await
|
||||
.map_err(|_| ScgiError::Timeout)??;
|
||||
|
||||
let double_newline = b"\r\n\r\n";
|
||||
if let Some(pos) = response
|
||||
let mut response_vec = response;
|
||||
if let Some(pos) = response_vec
|
||||
.windows(double_newline.len())
|
||||
.position(|window| window == double_newline)
|
||||
{
|
||||
Ok(Bytes::from(response.split_off(pos + double_newline.len())))
|
||||
Ok(Bytes::from(
|
||||
response_vec.split_off(pos + double_newline.len()),
|
||||
))
|
||||
} else {
|
||||
Ok(Bytes::from(response))
|
||||
Ok(Bytes::from(response_vec))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,6 +195,9 @@ pub async fn fetch_global_stats(client: &RtorrentClient) -> Result<GlobalStats,
|
||||
pub async fn sse_handler(
|
||||
State(state): State<AppState>,
|
||||
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
|
||||
// Notify background worker to wake up and poll immediately
|
||||
state.notify_poll.notify_one();
|
||||
|
||||
// Get initial value synchronously (from the watch channel's current state)
|
||||
let initial_rx = state.tx.subscribe();
|
||||
let initial_torrents = initial_rx.borrow().clone();
|
||||
|
||||
Reference in New Issue
Block a user