From: Matt Joiner Date: Fri, 17 Jul 2015 10:58:25 +0000 (+1000) Subject: Avoid holding client lock while scanning pieces in File.State X-Git-Tag: v1.0.0~1120 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=35fa1e336527be3c5103f610a7d7ab76a50b8160;p=btrtrc.git Avoid holding client lock while scanning pieces in File.State Some torrent data backends are expensive to query. --- diff --git a/file.go b/file.go index 688fc2fa..f76b99d0 100644 --- 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 }