diff options
author | Keuin <[email protected]> | 2022-09-09 02:30:19 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-09 02:30:41 +0800 |
commit | f028bff042f471a68dff681af9c79ef96bc952e5 (patch) | |
tree | 40763feb1d0ec05260e56d6822622462b35b165a /recording | |
parent | 719946a8211f3c8c68234a7c9e9c5af0226386aa (diff) |
Fix file buffer does not take effect. No idea why golang's io utility is so suck. Use ad-hoc buffered copy loop instead.
Diffstat (limited to 'recording')
-rw-r--r-- | recording/runner.go | 25 |
1 files changed, 12 insertions, 13 deletions
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 |