Store chunk data without holding client lock
This commit is contained in:
parent
4e80d48692
commit
7e9fe4f447
19
client.go
19
client.go
|
@ -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()
|
||||
|
|
1
piece.go
1
piece.go
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue