Change torrent capacity to not return a pointer

It's an unnecessary complication for a storage implementer.
This commit is contained in:
Matt Joiner 2021-09-15 10:30:37 +10:00
parent 72b0fee1eb
commit 2b443d695b
7 changed files with 26 additions and 16 deletions

2
go.mod
View File

@ -16,7 +16,7 @@ require (
github.com/anacrolix/missinggo/perf v1.0.0
github.com/anacrolix/missinggo/v2 v2.5.2
github.com/anacrolix/multiless v0.1.1-0.20210529082330-de2f6cf29619
github.com/anacrolix/squirrel v0.1.0
github.com/anacrolix/squirrel v0.1.1-0.20210914065657-81bc5ecdc43a
github.com/anacrolix/sync v0.4.0
github.com/anacrolix/tagflag v1.3.0
github.com/anacrolix/upnp v0.1.2-0.20200416075019-5e9378ed1425

2
go.sum
View File

@ -124,6 +124,8 @@ github.com/anacrolix/multiless v0.1.1-0.20210529082330-de2f6cf29619 h1:ZkusP2EHx
github.com/anacrolix/multiless v0.1.1-0.20210529082330-de2f6cf29619/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4=
github.com/anacrolix/squirrel v0.1.0 h1:Zz7XUFUr2ozhsTvzwLdmrFpduoTHtBNTB/KZQ4Ivh00=
github.com/anacrolix/squirrel v0.1.0/go.mod h1:YzgVvikMdFD441oTWlNG189bpKabO9Sbf3uCSVgca04=
github.com/anacrolix/squirrel v0.1.1-0.20210914065657-81bc5ecdc43a h1:8LAUQgDPqnzuF/WrGQzTY6i+bVO/FpA90Hi6jXA+2vQ=
github.com/anacrolix/squirrel v0.1.1-0.20210914065657-81bc5ecdc43a/go.mod h1:YzgVvikMdFD441oTWlNG189bpKabO9Sbf3uCSVgca04=
github.com/anacrolix/stm v0.1.0/go.mod h1:ZKz7e7ERWvP0KgL7WXfRjBXHNRhlVRlbBQecqFtPq+A=
github.com/anacrolix/stm v0.1.1-0.20191106051447-e749ba3531cf/go.mod h1:zoVQRvSiGjGoTmbM0vSLIiaKjWtNPeTvXUSdJQA4hsg=
github.com/anacrolix/stm v0.2.0/go.mod h1:zoVQRvSiGjGoTmbM0vSLIiaKjWtNPeTvXUSdJQA4hsg=

View File

@ -6,6 +6,7 @@ import (
"sync"
"github.com/anacrolix/multiless"
"github.com/anacrolix/torrent/storage"
pp "github.com/anacrolix/torrent/peer_protocol"
"github.com/anacrolix/torrent/types"
@ -96,8 +97,8 @@ func getRequestablePieces(input Input) (ret []requestablePiece) {
pieces := make([]filterPiece, 0, maxPieces)
ret = make([]requestablePiece, 0, maxPieces)
// Storage capacity left for this run, keyed by the storage capacity pointer on the storage
// TorrentImpl.
storageLeft := make(map[*func() *int64]*int64)
// TorrentImpl. A nil value means no capacity limit.
storageLeft := make(map[storage.TorrentCapacity]*int64)
for _t := range input.Torrents {
// TODO: We could do metainfo requests here.
t := &filterTorrent{
@ -107,7 +108,12 @@ func getRequestablePieces(input Input) (ret []requestablePiece) {
key := t.Capacity
if key != nil {
if _, ok := storageLeft[key]; !ok {
storageLeft[key] = (*key)()
capacity, ok := (*key)()
if ok {
storageLeft[key] = &capacity
} else {
storageLeft[key] = nil
}
}
t.storageLeft = storageLeft[key]
}

View File

@ -1,8 +1,12 @@
package request_strategy
import (
"github.com/anacrolix/torrent/storage"
)
type Torrent struct {
Pieces []Piece
Capacity *func() *int64
Capacity storage.TorrentCapacity
Peers []Peer // not closed.
// Some value that's unique and stable between runs. Could even use the infohash?
StableId uintptr

View File

@ -9,11 +9,9 @@ import (
type Client struct{}
var capacity int64
func (c Client) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storage.TorrentImpl, error) {
capFunc := func() *int64 {
return &capacity
capFunc := func() (int64, bool) {
return 0, true
}
return storage.TorrentImpl{
Piece: func(piece metainfo.Piece) storage.PieceImpl {
@ -26,10 +24,6 @@ func (c Client) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storag
}, nil
}
func (c Client) capacity() *int64 {
return &capacity
}
type Piece struct{}
func (Piece) ReadAt(p []byte, off int64) (n int, err error) {

View File

@ -16,12 +16,16 @@ type ClientImpl interface {
OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error)
}
type TorrentCapacity *func() (cap int64, capped bool)
// Data storage bound to a torrent.
type TorrentImpl struct {
Piece func(p metainfo.Piece) PieceImpl
Close func() error
// Storages that share the same value, will provide a pointer to the same function.
Capacity *func() *int64
// Storages that share the same space, will provide equal pointers. The function is called once
// to determine the storage for torrents sharing the same function pointer, and mutated in
// place.
Capacity TorrentCapacity
}
// Interacts with torrent piece data. Optional interfaces to implement include:

View File

@ -35,7 +35,7 @@ func NewWrappingClient(cache *squirrel.Cache) storage.ClientImpl {
type client struct {
*squirrel.Cache
capacity func() *int64
capacity func() (int64, bool)
}
func (c *client) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storage.TorrentImpl, error) {