Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .agents/skills/cloud139-e2e-test/references/offline.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ cargo test --locked --offline -p cloud139 --test download_dir_paging_test --test
| 13.8 | `cargo test --locked --offline -p cloud139 --lib services::download::tests` 验证下载断流、错误响应、已有文件及进度重试。[download_dir_paging_test](../../../../packages/lib/tests/download_dir_paging_test.rs) 核对下载链接的业务错误和空 CDN 地址的回退。 |
| 13.9 | [personal_pc_test](../../../../packages/lib/tests/personal_pc_test.rs) 和 `cargo test --locked --offline -p cloud139 --lib client::api::upload::tests` 核对 PC 请求头、上传权益由远端判断、分片和完成失败时保留原同步目标。用 mock 覆盖大文件权益错误。 |
| 13.12 | `cargo test --locked --offline -p cloud139 --test personal_pc_test rapid_upload_` 核对 `rapidUpload: true` 且无分片时返回成功,未确认秒传且无分片时返回失败。 |
| 13.13 | `cargo test --locked --offline -p cloud139 --lib client::api::personal::transfer::tests` 使用虚拟时钟验证单个分片持续传输时可超过 30 秒,最后一次发送数据后停滞 30 秒则超时。 |

## 同步和清理

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ cloud139 ls / -o result.json

单个文件的上传大小上限因账号而异。超过上限时,报错会显示文件大小和云盘返回的错误码。

分片上传在持续发送数据时可以超过 30 秒。连续 30 秒未发送数据或收到分片响应时,报错会显示超时的分片编号。

上传目录时,单个文件失败后仍会处理其余文件,最后汇总成功和失败数量。有文件上传失败时,退出码为 1。创建目录失败会停止上传。

```bash
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cloud139-cli"
version = "3.0.0"
version = "3.0.1"
edition = "2024"
rust-version = "1.88"

Expand Down
4 changes: 2 additions & 2 deletions packages/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ toml = "1.1"
base64 = "0.22"
sha2 = "0.11"
md-5 = "0.11"
tokio = { version = "1.51", features = ["fs", "io-util", "rt", "sync", "time"] }
tokio = { version = "1.51", features = ["fs", "io-util", "macros", "rt", "sync", "time"] }
chrono = { version = "0.4", default-features = false, features = ["clock"] }
thiserror = "2.0"
anyhow = "1"
Expand All @@ -33,4 +33,4 @@ windows-sys = { version = "0.61", features = ["Win32_Storage_FileSystem"] }

[dev-dependencies]
httpmock = "0.8"
tokio = { version = "1.51", features = ["macros"] }
tokio = { version = "1.51", features = ["test-util"] }
87 changes: 83 additions & 4 deletions packages/lib/src/client/api/personal/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ use crate::client::api::response::format_api_error;
use crate::client::http::{HttpClientWrapper, insert_static_headers};
use crate::error::ClientError;
use crate::protocol;
use futures_util::Stream;
use futures_util::{Stream, StreamExt};
use reqwest::{
Body, Response, StatusCode,
header::{CONTENT_LENGTH, HeaderMap, HeaderValue},
};
use std::io;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Notify;
use tokio::time::{error::Elapsed, timeout};

const UPLOAD_IDLE_TIMEOUT: Duration = Duration::from_secs(30);

pub(crate) struct DownloadLink<'a> {
client: &'a HttpClientWrapper,
Expand Down Expand Up @@ -115,14 +121,39 @@ pub(crate) async fn put_upload_part(
size: u64,
stream: impl Stream<Item = Result<Vec<u8>, io::Error>> + Send + 'static,
) -> Result<(), ClientError> {
let response = client
let activity = Arc::new(Notify::new());
let stream_activity = Arc::clone(&activity);
let stream = stream.inspect(move |chunk| {
if chunk.as_ref().is_ok_and(|bytes| !bytes.is_empty()) {
stream_activity.notify_one();
}
});
let request = client
.client
.put(url)
.headers(build_upload_part_headers(size))
.body(Body::wrap_stream(stream))
.send()
.send();
let response = with_upload_idle_timeout(request, &activity)
.await
.map_err(reqwest::Error::without_url)?;
.map_err(|_| {
io::Error::new(
io::ErrorKind::TimedOut,
format!(
"分片 {part_number} 上传超时,连续 {} 秒未发送数据或收到响应",
UPLOAD_IDLE_TIMEOUT.as_secs()
),
)
})?
.map_err(|error| {
crate::debug!(
"发送上传分片请求失败 part={} connect={} timeout={}",
part_number,
error.is_connect(),
error.is_timeout()
);
error.without_url()
})?;

let status = response.status();
if status != StatusCode::OK {
Expand All @@ -133,3 +164,51 @@ pub(crate) async fn put_upload_part(
}
Ok(())
}

async fn with_upload_idle_timeout<T>(
request: impl Future<Output = T>,
activity: &Notify,
) -> Result<T, Elapsed> {
tokio::select! {
biased;
response = request => Ok(response),
expired = async {
loop {
timeout(UPLOAD_IDLE_TIMEOUT, activity.notified()).await?;
}
} => expired,
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::future::pending;
use tokio::time::{Instant, sleep};

#[tokio::test(start_paused = true)]
async fn upload_can_take_longer_than_the_idle_timeout_with_progress() {
let activity = Notify::new();
let request = async {
sleep(Duration::from_secs(20)).await;
activity.notify_one();
sleep(Duration::from_secs(20)).await;
};

with_upload_idle_timeout(request, &activity).await.unwrap();
}

#[tokio::test(start_paused = true)]
async fn upload_times_out_without_progress_after_sending_data() {
let activity = Notify::new();
let start = Instant::now();
let request = async {
sleep(Duration::from_secs(20)).await;
activity.notify_one();
pending::<()>().await;
};

assert!(with_upload_idle_timeout(request, &activity).await.is_err());
assert_eq!(start.elapsed(), Duration::from_secs(50));
}
}
20 changes: 14 additions & 6 deletions packages/lib/src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ impl Default for HttpClientWrapper {

impl HttpClientWrapper {
pub fn new() -> Self {
Self {
client: http_client_builder()
Self::with_client(
http_client_builder()
.read_timeout(Duration::from_secs(30))
.build()
.expect("HTTP 客户端初始化失败"),
}
)
}

pub(crate) fn for_upload_parts() -> Self {
// reqwest 的读取计时会覆盖请求体发送,分片上传单独按传输活动计时。
Self::with_client(
http_client_builder()
.build()
.expect("分片上传 HTTP 客户端初始化失败"),
)
}

pub fn with_client(client: reqwest::Client) -> Self {
Expand All @@ -47,9 +57,7 @@ impl HttpClientWrapper {
}

fn http_client_builder() -> ClientBuilder {
Client::builder()
.connect_timeout(Duration::from_secs(10))
.read_timeout(Duration::from_secs(30))
Client::builder().connect_timeout(Duration::from_secs(10))
}

fn build_signed_post(
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/services/upload/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async fn upload_parts(params: UploadPartsParams<'_>) -> Result<(), ClientError>
let file_size = layout.file_size();
let content_hash = params.content_hash;
let file = tokio::fs::File::open(local_path).await?;
let data_client = HttpClientWrapper::new();
let data_client = HttpClientWrapper::for_upload_parts();
if file.metadata().await?.len() < file_size as u64 {
return Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
Expand Down
Loading