From 1009e88ff752525966708c56190c2dfa32bc9537 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 8 Sep 2022 00:54:21 +0800 Subject: Remove unnecessary boolean flag. --- bilibili/streaming.go | 8 ++++---- common/copy.go | 6 +----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/bilibili/streaming.go b/bilibili/streaming.go index ec99565..5407043 100644 --- a/bilibili/streaming.go +++ b/bilibili/streaming.go @@ -3,6 +3,7 @@ package bilibili import ( "bilibili-livestream-archiver/common" "context" + "errors" "fmt" "io" "net/http" @@ -59,14 +60,13 @@ func (b Bilibili) CopyLiveStream( defer cancelGuardian() // blocking copy - n, err, isCancelled := common.Copy(ctx, out, resp.Body) + n, err := common.Copy(ctx, out, resp.Body) - if isCancelled { + if errors.Is(err, context.Canceled) { // cancelled by context // this error is useless err = nil - } - if !isCancelled && err != nil { + } else { // real error happens b.error.Printf("Stream copying was interrupted unexpectedly: %v", err) } diff --git a/common/copy.go b/common/copy.go index a896fb4..6f47a62 100644 --- a/common/copy.go +++ b/common/copy.go @@ -18,10 +18,7 @@ func (rf readerFunc) Read(p []byte) (n int, err error) { return rf(p) } // Copy slightly modified function signature: // - context has been added in order to propagate cancellation // - (undo by Keuin) I do not return the number of bytes written, has it is not useful in my use case -// - (added by Keuin) add a isCancelled return value indicating the copy is stopped by cancelling the context -func Copy(ctx context.Context, out io.Writer, in io.Reader) (written int64, err error, isCancelled bool) { - isCancelled = false - +func Copy(ctx context.Context, out io.Writer, in io.Reader) (written int64, err error) { // Copy will call the Reader and Writer interface multiple time, in order // to copy by chunk (avoiding loading the whole file in memory). // I insert the ability to cancel before read time as it is the earliest @@ -34,7 +31,6 @@ func Copy(ctx context.Context, out io.Writer, in io.Reader) (written int64, err // if context has been canceled case <-ctx.Done(): // stop process and propagate "context canceled" error - isCancelled = true return 0, ctx.Err() default: // otherwise just run default io.Reader implementation -- cgit v1.2.3