]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Avoid holding client lock while scanning pieces in File.State
authorMatt Joiner <anacrolix@gmail.com>
Fri, 17 Jul 2015 10:58:25 +0000 (20:58 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 17 Jul 2015 10:58:25 +0000 (20:58 +1000)
Some torrent data backends are expensive to query.

file.go

diff --git a/file.go b/file.go
index 688fc2faa92ae6af221d706a94fe4d27461f5eb2..f76b99d0823cda8d7fc0a6bba94ce303ef8b68c7 100644 (file)
--- a/file.go
+++ b/file.go
@@ -35,8 +35,6 @@ type FilePieceState struct {
 
 // Returns the state of pieces in this file.
 func (f *File) State() (ret []FilePieceState) {
-       f.t.cl.mu.Lock()
-       defer f.t.cl.mu.Unlock()
        pieceSize := int64(f.t.usualPieceSize())
        off := f.offset % pieceSize
        remaining := f.length
@@ -48,7 +46,10 @@ func (f *File) State() (ret []FilePieceState) {
                if len1 > remaining {
                        len1 = remaining
                }
-               ret = append(ret, FilePieceState{len1, f.t.pieceState(i)})
+               f.t.cl.mu.RLock()
+               ps := f.t.pieceState(i)
+               f.t.cl.mu.RUnlock()
+               ret = append(ret, FilePieceState{len1, ps})
                off = 0
                remaining -= len1
        }