]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add Reader.SetReadaheadFunc
authorMatt Joiner <anacrolix@gmail.com>
Wed, 10 Nov 2021 23:24:28 +0000 (10:24 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 14 Nov 2021 13:53:42 +0000 (00:53 +1100)
Fixes https://github.com/anacrolix/torrent/issues/687.

reader.go

index bdeb34f64812da2ed455d41d9009d278a6c58ec4..c8af14640bb813e98eabeca5305aa02532ffaba1 100644 (file)
--- a/reader.go
+++ b/reader.go
@@ -17,8 +17,12 @@ type Reader interface {
        io.ReadSeekCloser
        missinggo.ReadContexter
        // Configure the number of bytes ahead of a read that should also be prioritized in preparation
-       // for further reads.
+       // for further reads. Overridden by non-nil readahead func, see SetReadaheadFunc.
        SetReadahead(int64)
+       // If non-nil, the provided function is called when the implementation needs to know the
+       // readahead for the current reader. Calls occur during Reads and Seeks, and while the Client is
+       // locked.
+       SetReadaheadFunc(func() int64)
        // Don't wait for pieces to complete and be verified. Read calls return as soon as they can when
        // the underlying chunks become available.
        SetResponsive()
@@ -75,6 +79,13 @@ func (r *reader) SetReadahead(readahead int64) {
        r.mu.Unlock()
 }
 
+func (r *reader) SetReadaheadFunc(f func() int64) {
+       r.mu.Lock()
+       r.readaheadFunc = f
+       r.posChanged()
+       r.mu.Unlock()
+}
+
 // How many bytes are available to read. Max is the most we could require.
 func (r *reader) available(off, max int64) (ret int64) {
        off += r.offset