Skip to content

Commit

Permalink
ファイルコピーの並列数を制限
Browse files Browse the repository at this point in the history
  • Loading branch information
mousecrusher2 committed Jun 17, 2024
1 parent 6ccb069 commit 5537a63
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tokio = { version = "1.37.0", features = [
"io-util",
"fs",
"time",
"sync",
] }

[profile.release]
Expand Down
20 changes: 13 additions & 7 deletions src/backup.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use chrono::{DateTime, Local};
use regex::Regex;
use std::path::PathBuf;
use tokio::fs;
use std::{path::PathBuf, sync::Arc};
use tokio::{fs, io::AsyncWriteExt, sync::Semaphore};

pub struct Backuper {
backup_folder: PathBuf,
origin_folder: PathBuf,
backup_count: usize,
}

const PARALLEL_LIMIT: usize = 100;

impl Backuper {
pub(super) fn new(backup_folder: PathBuf, origin_folder: PathBuf, backup_count: usize) -> Self {
if !backup_folder.is_dir() {
Expand Down Expand Up @@ -84,20 +86,24 @@ impl Backuper {
let origin_path = self.origin_folder.join(&path);
let target_path = target_backup_folder.join(&path);
tasks.push(tokio::spawn(async move {
static SEMAPHORE: Semaphore = Semaphore::const_new(PARALLEL_LIMIT);
let _permit = SEMAPHORE
.acquire()
.await
.expect("Failed to acquire semaphore");
fs::create_dir_all(target_path.parent().unwrap())
.await
.expect("Failed to create parent directories");
fs::copy(origin_path, &target_path)
.await
.expect("Failed to copy file");
fs::OpenOptions::new()
let mut f = fs::OpenOptions::new()
.write(true)
.open(target_path)
.await
.expect("Failed to open file")
.set_len(size)
.await
.expect("Failed to set file size");
.expect("Failed to open file");
f.set_len(size).await.expect("Failed to set file size");
f.flush().await.expect("Failed to flush file");
}));
}
for task in tasks {
Expand Down

0 comments on commit 5537a63

Please sign in to comment.