Avoid holding client lock while scanning pieces in File.State

Some torrent data backends are expensive to query.
This commit is contained in:
Matt Joiner 2015-07-17 20:58:25 +10:00
parent 7e9fe4f447
commit 35fa1e3365
1 changed files with 4 additions and 3 deletions

View File

@ -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
}