Store chunk data without holding client lock

This commit is contained in:
Matt Joiner 2015-07-15 16:00:59 +10:00
parent 4e80d48692
commit 7e9fe4f447
4 changed files with 24 additions and 6 deletions

View File

@ -1432,6 +1432,7 @@ another:
func (me *Client) sendChunk(t *torrent, c *connection, r request) error {
b := make([]byte, r.Length)
t.Pieces[r.Index].pendingWrites.Wait()
p := t.Info.Piece(int(r.Index))
n, err := dataReadAt(t.data, b, p.Offset()+int64(r.Begin))
if err != nil {
@ -2523,12 +2524,18 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er
me.upload(t, c)
// Write the chunk out.
err := t.writeChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
if err != nil {
log.Printf("error writing chunk: %s", err)
return nil
}
piece.pendingWrites.Add(1)
go func() {
defer piece.pendingWrites.Done()
// Write the chunk out.
tr := perf.NewTimer()
err := t.writeChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
if err != nil {
log.Printf("error writing chunk: %s", err)
return
}
tr.Stop("write chunk")
}()
// log.Println("got chunk", req)
piece.Event.Broadcast()

View File

@ -30,6 +30,7 @@ type piece struct {
EverHashed bool
Event sync.Cond
Priority piecePriority
pendingWrites sync.WaitGroup
}
func (p *piece) pendingChunk(cs chunkSpec, chunkSize pp.Integer) bool {

View File

@ -121,6 +121,14 @@ again:
avail := r.available(pos, int64(len(b)))
// log.Println("available", avail)
b1 := b[:avail]
pi := int(pos / r.t.Info().PieceLength)
tp := r.t.torrent.Pieces[pi]
ip := r.t.Info().Piece(pi)
po := pos % ip.Length()
if int64(len(b1)) > ip.Length()-po {
b1 = b1[:ip.Length()-po]
}
tp.pendingWrites.Wait()
n, err = dataReadAt(r.t.data, b1, pos)
if n != 0 {
err = nil

View File

@ -638,6 +638,8 @@ func (t *torrent) pieceLength(piece int) (len_ pp.Integer) {
func (t *torrent) hashPiece(piece pp.Integer) (ps pieceSum) {
hash := pieceHash.New()
p := t.Pieces[piece]
p.pendingWrites.Wait()
t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, t.Info.PieceLength)
util.CopyExact(ps[:], hash.Sum(nil))
return