Unverified Commit 3d0e4721 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

perf(turbopack): Drop `tokio::fs` usages (#80674)

### What?

`std::fs` with manual `spawn_blocking` is better than using `tokio::fs`

### Why?

`tokio::fs` inevitably calls `spawn_blocking` internally, but it calls `spawn_blocking` way too much time

---------
Co-authored-by: 's avatargraphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 3327565c
This diff is collapsed.
use std::{future::Future, io, io::ErrorKind, path::Path, thread::sleep, time::Duration};
use futures_retry::{ErrorHandler, FutureRetry, RetryPolicy};
use std::{
io::{self, ErrorKind},
path::{Path, PathBuf},
thread::sleep,
time::Duration,
};
const MAX_RETRY_ATTEMPTS: usize = 10;
pub(crate) async fn retry_future<'a, R, F, Fut>(func: F) -> io::Result<R>
where
F: FnMut() -> Fut + Unpin,
Fut: Future<Output = io::Result<R>> + 'a,
{
match FutureRetry::new(
func,
FsRetryHandler {
max_attempts: MAX_RETRY_ATTEMPTS,
},
)
.await
{
Ok((r, _attempts)) => Ok(r),
Err((err, _attempts)) => Err(err),
}
}
pub(crate) async fn retry_blocking<R, F>(path: impl AsRef<Path>, func: F) -> io::Result<R>
pub(crate) async fn retry_blocking<R, F>(path: PathBuf, func: F) -> io::Result<R>
where
F: Fn(&Path) -> io::Result<R> + Send + 'static,
R: Send + 'static,
{
let path = path.as_ref().to_owned();
turbo_tasks::spawn_blocking(move || {
let mut attempt = 1;
......@@ -60,19 +43,3 @@ fn can_retry(err: &io::Error) -> bool {
fn get_retry_wait_time(attempt: usize) -> Duration {
Duration::from_millis((attempt as u64) * 100)
}
struct FsRetryHandler {
max_attempts: usize,
}
impl ErrorHandler<io::Error> for FsRetryHandler {
type OutError = io::Error;
fn handle(&mut self, attempt: usize, e: io::Error) -> RetryPolicy<io::Error> {
if attempt == self.max_attempts || !can_retry(&e) {
RetryPolicy::ForwardError(e)
} else {
RetryPolicy::WaitRetry(get_retry_wait_time(attempt))
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment