perf(app): throttle upload progress callbacks to 256KB steps
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+10
-2
@@ -38,11 +38,15 @@ sealed class PutOutcome {
|
||||
* - 请求头:严格携带 Dart 传入的 credential.headers(Content-Type 经
|
||||
* RequestBody.contentType 表达,Content-Length 由 okhttp 按声明长度
|
||||
* 发送,二者不重复注入);
|
||||
* - 进度:写 body 驱动 [onProgress](已发字节/总字节,IO 线程回调);
|
||||
* - 进度:写 body 驱动 [onProgress](已发字节/总字节,IO 线程回调;
|
||||
* 256KB 步长节流,末块必报 sent==total);
|
||||
* - OkHttp 独立实例(4.12,与 Flutter 引擎无耦合;连接失败内置静默重试)。
|
||||
*/
|
||||
object UploadPutExecutor {
|
||||
|
||||
/** 进度回调最小步长(字节);末块必报。 */
|
||||
private const val PROGRESS_STEP_BYTES = 256L * 1024
|
||||
|
||||
private val defaultClient: OkHttpClient = OkHttpClient.Builder()
|
||||
.connectTimeout(Duration.ofSeconds(15))
|
||||
.writeTimeout(Duration.ofMinutes(3))
|
||||
@@ -75,6 +79,7 @@ object UploadPutExecutor {
|
||||
file.inputStream().use { input ->
|
||||
val buffer = ByteArray(64 * 1024)
|
||||
var sent = 0L
|
||||
var lastReported = 0L
|
||||
while (true) {
|
||||
val read = input.read(buffer)
|
||||
if (read < 0) {
|
||||
@@ -82,7 +87,10 @@ object UploadPutExecutor {
|
||||
}
|
||||
sink.write(buffer, 0, read)
|
||||
sent += read
|
||||
onProgress?.invoke(sent, total)
|
||||
if (sent - lastReported >= PROGRESS_STEP_BYTES || sent == total) {
|
||||
lastReported = sent
|
||||
onProgress?.invoke(sent, total)
|
||||
}
|
||||
}
|
||||
sink.flush()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user