// Return a Torrent ready for insertion into a Client.
func (cl *Client) newTorrent(ih metainfo.Hash) (t *Torrent) {
t = &Torrent{
- cl: cl,
- infoHash: ih,
- chunkSize: defaultChunkSize,
- peers: make(map[peersKey]Peer),
+ cl: cl,
+ infoHash: ih,
+ peers: make(map[peersKey]Peer),
halfOpen: make(map[string]struct{}),
pieceStateChanges: pubsub.NewPubSub(),
storageOpener: cl.defaultStorage,
maxEstablishedConns: defaultEstablishedConnsPerTorrent,
}
+ t.setChunkSize(defaultChunkSize)
return
}
cl.mu.Lock()
defer cl.mu.Unlock()
if spec.ChunkSize != 0 {
- t.chunkSize = pp.Integer(spec.ChunkSize)
+ t.setChunkSize(pp.Integer(spec.ChunkSize))
}
t.addTrackers(spec.Trackers)
t.maybeNewConns()
func (c *connection) mainReadLoop() error {
t := c.t
cl := t.cl
- pool := &sync.Pool{
- New: func() interface{} {
- return make([]byte, t.chunkSize)
- },
- }
decoder := pp.Decoder{
R: bufio.NewReader(c.rw),
MaxLength: 256 * 1024,
- Pool: pool,
+ Pool: t.chunkPool,
}
for {
cl.mu.Unlock()
case pp.Piece:
cl.downloadedChunk(t, c, &msg)
if len(msg.Piece) == int(t.chunkSize) {
- pool.Put(msg.Piece)
+ t.chunkPool.Put(msg.Piece)
}
case pp.Extended:
switch msg.ExtendedID {
Length: 1 << 20,
PieceLength: 1 << 20,
},
- chunkSize: defaultChunkSize,
storage: &storage.Torrent{ts},
pieceStateChanges: pubsub.NewPubSub(),
}
+ t.setChunkSize(defaultChunkSize)
t.makePieces()
t.pendingPieces.Add(0)
r, w := io.Pipe()
// The size of chunks to request from peers over the wire. This is
// normally 16KiB by convention these days.
chunkSize pp.Integer
+ chunkPool *sync.Pool
// Total length of the torrent in bytes. Stored because it's not O(1) to
// get this from the info dict.
length int64
stats TorrentStats
}
+func (t *Torrent) setChunkSize(size pp.Integer) {
+ t.chunkSize = size
+ t.chunkPool = &sync.Pool{
+ New: func() interface{} {
+ return make([]byte, size)
+ },
+ }
+}
+
func (t *Torrent) setDisplayName(dn string) {
if t.haveInfo() {
return