feat: Enter a tracing span in stream_file
This commit is contained in:
parent
0db27f3671
commit
2d62688db2
|
@ -2,12 +2,12 @@ pub mod error;
|
|||
|
||||
use error::{handle_rejection, InternalServerError, NotFoundError};
|
||||
use hyper::{body, Body};
|
||||
use std::path::PathBuf;
|
||||
use std::{path::PathBuf, time::Duration};
|
||||
|
||||
use bytes::Bytes;
|
||||
use clap::Parser as _;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tracing::{debug, warn, Level};
|
||||
use tracing::{debug, span, warn, Instrument, Level};
|
||||
use warp::{
|
||||
filters::{any::any, path::param},
|
||||
http::HeaderMap,
|
||||
|
@ -49,7 +49,10 @@ pub async fn serve_local_tracks(
|
|||
// FIXME: for now, file paths need to be URL safe to be able to be requested
|
||||
location.push(PathBuf::from(track_name));
|
||||
|
||||
let Ok(file) = tokio::fs::File::options().read(true).open(&location).await else {
|
||||
let (Ok(file), Some(file_name)) = (
|
||||
tokio::fs::File::options().read(true).open(&location).await,
|
||||
location.file_name().map(|name| name.to_string_lossy()),
|
||||
) else {
|
||||
return Err(NotFoundError(
|
||||
format!("The requested song could not be found on disk. Tried loading {location:?}")
|
||||
.into(),
|
||||
|
@ -61,8 +64,11 @@ pub async fn serve_local_tracks(
|
|||
|
||||
let (sender, body) = Body::channel();
|
||||
|
||||
debug!("Starting to stream file");
|
||||
tokio::task::spawn(stream_file(sender, file));
|
||||
tokio::task::spawn(stream_file(sender, file).instrument(span!(
|
||||
Level::DEBUG,
|
||||
"stream_file",
|
||||
file_name = file_name.as_ref()
|
||||
)));
|
||||
|
||||
if let Ok(response) = hyper::Response::builder()
|
||||
.header("Content-Type", "audio/mpeg")
|
||||
|
@ -76,13 +82,15 @@ pub async fn serve_local_tracks(
|
|||
}
|
||||
|
||||
pub async fn stream_file(mut dest: body::Sender, mut file: tokio::fs::File) {
|
||||
debug!("Starting stream");
|
||||
|
||||
let mut buf = [0u8; 512];
|
||||
|
||||
loop {
|
||||
let bytes_read = match file.read(&mut buf).await {
|
||||
Ok(0) => break debug!("Done streaming a file"),
|
||||
Ok(0) => break debug!("Done streaming the file"),
|
||||
Ok(bytes_read) => bytes_read,
|
||||
Err(err) => break warn!("Couldn't read part of a file. Got err: {err}"),
|
||||
Err(err) => break warn!("Couldn't read part of the file. Got err: {err}"),
|
||||
};
|
||||
|
||||
let res = dest
|
||||
|
@ -90,8 +98,10 @@ pub async fn stream_file(mut dest: body::Sender, mut file: tokio::fs::File) {
|
|||
.await;
|
||||
|
||||
if res.is_err() {
|
||||
warn!("Failed to send some data to a client");
|
||||
warn!("Connection dropped by peer");
|
||||
break;
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue