From f028bff042f471a68dff681af9c79ef96bc952e5 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 9 Sep 2022 02:30:19 +0800 Subject: Fix file buffer does not take effect. No idea why golang's io utility is so suck. Use ad-hoc buffered copy loop instead. --- recording/runner.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'recording') diff --git a/recording/runner.go b/recording/runner.go index 974fa0e..2f15abd 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -8,7 +8,6 @@ package recording import ( "bilibili-livestream-archiver/bilibili" "bilibili-livestream-archiver/common" - "bufio" "context" "encoding/json" "errors" @@ -26,6 +25,8 @@ type TaskResult struct { Error error } +const kReadChunkSize = 64 * 1024 + // RunTask start a monitor&download task and // put its execution result into a channel. func RunTask(ctx context.Context, wg *sync.WaitGroup, task *TaskConfig) { @@ -189,19 +190,17 @@ func record( } defer func() { _ = file.Close() }() - // buffered writer - fWriter := bufio.NewWriterSize(file, task.Download.DiskWriteBufferBytes) - defer func() { - err := fWriter.Flush() - if err != nil { - logger := log.Default() - logger.Printf("Failed to flush buffered file write data: %v", err) - } - }() - logger.Printf("Write buffer size: %v byte", fWriter.Size()) - + writeBufferSize := task.Download.DiskWriteBufferBytes + if writeBufferSize < kReadChunkSize { + writeBufferSize = kReadChunkSize + } + if mod := writeBufferSize % kReadChunkSize; mod != 0 { + writeBufferSize += kReadChunkSize - mod + } + writeBuffer := make([]byte, writeBufferSize) + logger.Printf("Write buffer size: %v byte", writeBufferSize) logger.Printf("Recording live stream to file \"%v\"...", filePath) - err = bi.CopyLiveStream(ctx, task.RoomId, streamSource, fWriter) + err = bi.CopyLiveStream(ctx, task.RoomId, streamSource, file, writeBuffer, kReadChunkSize) cancelled = err == nil || errors.Is(err, context.Canceled) if !cancelled { // real error happens -- cgit v1.2.3