]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add missing return and tidy waitAvailable
authorMatt Joiner <anacrolix@gmail.com>
Thu, 8 May 2025 05:50:56 +0000 (15:50 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 8 May 2025 05:50:56 +0000 (15:50 +1000)
reader.go

index 0a1c2297965faac3ff1dfe749f68f365d68b31bb..ab6df459f1e2aa3885dfbd63037d9f15b5f9d930 100644 (file)
--- a/reader.go
+++ b/reader.go
@@ -219,37 +219,40 @@ func init() {
 }
 
 // Wait until some data should be available to read. Tickles the client if it isn't. Returns how
-// much should be readable without blocking.
-func (r *reader) waitAvailable(ctx context.Context, pos, wanted int64, wait bool) (avail int64, err error) {
+// much should be readable without blocking. `block` is whether to block if nothing is available,
+// for successive reads for example.
+func (r *reader) waitAvailable(
+       ctx context.Context,
+       pos, wanted int64,
+       block bool,
+) (avail int64, err error) {
        t := r.t
        for {
-               r.t.cl.rLock()
+               t.cl.rLock()
                avail = r.available(pos, wanted)
                readerCond := t.piece(int((r.offset + pos) / t.info.PieceLength)).readerCond.Signaled()
-               r.t.cl.rUnlock()
+               t.cl.rUnlock()
                if avail != 0 {
                        return
                }
                var dontWait <-chan struct{}
-               if !wait || wanted == 0 {
+               if !block || wanted == 0 {
                        dontWait = closedChan
                }
                select {
+               case <-readerCond:
+                       continue
                case <-r.t.closed.Done():
                        err = errTorrentClosed
-                       return
                case <-ctx.Done():
                        err = ctx.Err()
-                       return
                case <-r.t.dataDownloadDisallowed.On():
                        err = errors.New("torrent data downloading disabled")
                case <-r.t.networkingEnabled.Off():
                        err = errors.New("torrent networking disabled")
-                       return
                case <-dontWait:
-                       return
-               case <-readerCond:
                }
+               return
        }
 }