From 2b443d695b63a68487f4a611a64942f62aabea55 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 15 Sep 2021 10:30:37 +1000 Subject: [PATCH] Change torrent capacity to not return a pointer It's an unnecessary complication for a storage implementer. --- go.mod | 2 +- go.sum | 2 ++ request-strategy/order.go | 12 +++++++++--- request-strategy/torrent.go | 6 +++++- storage/disabled/disabled.go | 10 ++-------- storage/interface.go | 8 ++++++-- storage/sqlite/direct.go | 2 +- 7 files changed, 26 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 843bee23..edad54ca 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 507f5040..eb9323d5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/request-strategy/order.go b/request-strategy/order.go index e2b8e51e..92125c14 100644 --- a/request-strategy/order.go +++ b/request-strategy/order.go @@ -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] } diff --git a/request-strategy/torrent.go b/request-strategy/torrent.go index 2090c7a8..c7ed3c60 100644 --- a/request-strategy/torrent.go +++ b/request-strategy/torrent.go @@ -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 diff --git a/storage/disabled/disabled.go b/storage/disabled/disabled.go index 22d51434..f511222a 100644 --- a/storage/disabled/disabled.go +++ b/storage/disabled/disabled.go @@ -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) { diff --git a/storage/interface.go b/storage/interface.go index a52ee6dc..ee6e6336 100644 --- a/storage/interface.go +++ b/storage/interface.go @@ -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: diff --git a/storage/sqlite/direct.go b/storage/sqlite/direct.go index b36867fa..4fda7d0c 100644 --- a/storage/sqlite/direct.go +++ b/storage/sqlite/direct.go @@ -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) {