Apply megacheck to torrent package

This commit is contained in:
Matt Joiner 2017-11-07 16:11:59 +11:00
parent be3f32e962
commit 9757f04cf8
8 changed files with 24 additions and 36 deletions

View File

@ -836,25 +836,13 @@ type deadlineReader struct {
r io.Reader
}
func (r deadlineReader) Read(b []byte) (n int, err error) {
func (r deadlineReader) Read(b []byte) (int, error) {
// Keep-alives should be received every 2 mins. Give a bit of gracetime.
err = r.nc.SetReadDeadline(time.Now().Add(150 * time.Second))
err := r.nc.SetReadDeadline(time.Now().Add(150 * time.Second))
if err != nil {
err = fmt.Errorf("error setting read deadline: %s", err)
return 0, fmt.Errorf("error setting read deadline: %s", err)
}
n, err = r.r.Read(b)
// Convert common errors into io.EOF.
// if err != nil {
// if opError, ok := err.(*net.OpError); ok && opError.Op == "read" && opError.Err == syscall.ECONNRESET {
// err = io.EOF
// } else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
// if n != 0 {
// panic(n)
// }
// err = io.EOF
// }
// }
return
return r.r.Read(b)
}
func handleEncryption(

View File

@ -423,7 +423,7 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) {
}
func assertReadAllGreeting(t *testing.T, r io.ReadSeeker) {
pos, err := r.Seek(0, os.SEEK_SET)
pos, err := r.Seek(0, io.SeekStart)
assert.NoError(t, err)
assert.EqualValues(t, 0, pos)
_greeting, err := ioutil.ReadAll(r)
@ -573,6 +573,7 @@ func TestCompletedPieceWrongSize(t *testing.T) {
},
}
b, err := bencode.Marshal(info)
require.NoError(t, err)
tt, new, err := cl.AddTorrentSpec(&TorrentSpec{
InfoBytes: b,
InfoHash: metainfo.HashBytes(b),
@ -634,12 +635,12 @@ func TestResponsive(t *testing.T) {
reader.SetReadahead(0)
reader.SetResponsive()
b := make([]byte, 2)
_, err = reader.Seek(3, os.SEEK_SET)
_, err = reader.Seek(3, io.SeekStart)
require.NoError(t, err)
_, err = io.ReadFull(reader, b)
assert.Nil(t, err)
assert.EqualValues(t, "lo", string(b))
_, err = reader.Seek(11, os.SEEK_SET)
_, err = reader.Seek(11, io.SeekStart)
require.NoError(t, err)
n, err := io.ReadFull(reader, b)
assert.Nil(t, err)
@ -677,13 +678,13 @@ func TestTorrentDroppedDuringResponsiveRead(t *testing.T) {
reader.SetReadahead(0)
reader.SetResponsive()
b := make([]byte, 2)
_, err = reader.Seek(3, os.SEEK_SET)
_, err = reader.Seek(3, io.SeekStart)
require.NoError(t, err)
_, err = io.ReadFull(reader, b)
assert.Nil(t, err)
assert.EqualValues(t, "lo", string(b))
go leecherTorrent.Drop()
_, err = reader.Seek(11, os.SEEK_SET)
_, err = reader.Seek(11, io.SeekStart)
require.NoError(t, err)
n, err := reader.Read(b)
assert.EqualError(t, err, "torrent closed")

View File

@ -145,7 +145,7 @@ func eventAgeString(t time.Time) string {
if t.IsZero() {
return "never"
}
return fmt.Sprintf("%.2fs ago", time.Now().Sub(t).Seconds())
return fmt.Sprintf("%.2fs ago", time.Since(t).Seconds())
}
func (cn *connection) connectionFlags() (ret string) {
@ -871,7 +871,7 @@ func (c *connection) mainReadLoop() error {
case pp.Piece:
c.receiveChunk(&msg)
if len(msg.Piece) == int(t.chunkSize) {
t.chunkPool.Put(msg.Piece)
t.chunkPool.Put(&msg.Piece)
}
case pp.Extended:
switch msg.ExtendedID {
@ -1043,9 +1043,9 @@ func (c *connection) receiveChunk(msg *pp.Message) {
c.UsefulChunksReceived++
c.lastUsefulChunkReceived = time.Now()
if t.fastestConn != c {
// if t.fastestConn != c {
// log.Printf("setting fastest connection %p", c)
}
// }
t.fastestConn = c
// Need to record that it hasn't been written yet, before we attempt to do
@ -1087,7 +1087,6 @@ func (c *connection) receiveChunk(msg *pp.Message) {
cl.event.Broadcast()
t.publishPieceChange(int(req.Index))
return
}
// Also handles choking and unchoking of the remote peer.

View File

@ -48,6 +48,6 @@ func BenchmarkDecodePieces(t *testing.B) {
var msg Message
require.NoError(t, d.Decode(&msg))
// WWJD
d.Pool.Put(msg.Piece)
d.Pool.Put(&msg.Piece)
}
}

View File

@ -211,7 +211,7 @@ func (d *Decoder) Decode(msg *Message) (err error) {
break
}
//msg.Piece, err = ioutil.ReadAll(r)
b := d.Pool.Get().([]byte)
b := *d.Pool.Get().(*[]byte)
n, err := io.ReadFull(r, b)
if err != nil {
if err != io.ErrUnexpectedEOF || n != int(length-9) {

View File

@ -4,7 +4,6 @@ import (
"errors"
"io"
"log"
"os"
"sync"
"github.com/anacrolix/missinggo"
@ -236,11 +235,11 @@ func (r *Reader) Seek(off int64, whence int) (ret int64, err error) {
r.mu.Lock()
defer r.mu.Unlock()
switch whence {
case os.SEEK_SET:
case io.SeekStart:
r.pos = off
case os.SEEK_CUR:
case io.SeekCurrent:
r.pos += off
case os.SEEK_END:
case io.SeekEnd:
r.pos = r.t.info.TotalLength() + off
default:
err = errors.New("bad whence")

View File

@ -176,7 +176,8 @@ func (t *Torrent) setChunkSize(size pp.Integer) {
t.chunkSize = size
t.chunkPool = &sync.Pool{
New: func() interface{} {
return make([]byte, size)
b := make([]byte, size)
return &b
},
}
}
@ -1000,7 +1001,7 @@ func (t *Torrent) getConnPieceInclination() []int {
}
func (t *Torrent) putPieceInclination(pi []int) {
t.connPieceInclinationPool.Put(pi)
t.connPieceInclinationPool.Put(&pi)
pieceInclinationsPut.Add(1)
}

View File

@ -111,7 +111,7 @@ func (me *trackerScraper) Run() {
me.lastAnnounce = ar
me.t.cl.mu.Unlock()
intervalChan := time.After(ar.Completed.Add(ar.Interval).Sub(time.Now()))
intervalChan := time.After(time.Until(ar.Completed.Add(ar.Interval)))
select {
case <-me.t.closed.LockedChan(&me.t.cl.mu):