From: Matt Joiner Date: Thu, 27 Jun 2024 04:34:54 +0000 (+1000) Subject: Add a Context param to ClientImpl.OpenTorrent for logging X-Git-Tag: v1.57.0~28 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=299028488a55a28115574a51f3551c3966e43227;p=btrtrc.git Add a Context param to ClientImpl.OpenTorrent for logging This is very likely going to be a breaking change, but the alternative is a nightmare of backwards compatibility. --- diff --git a/bad_storage.go b/bad_storage.go index fc15beb9..7a2537a6 100644 --- a/bad_storage.go +++ b/bad_storage.go @@ -1,6 +1,7 @@ package torrent import ( + "context" "errors" "math/rand" "strings" @@ -14,7 +15,11 @@ type badStorage struct{} var _ storage.ClientImpl = badStorage{} -func (bs badStorage) OpenTorrent(*metainfo.Info, metainfo.Hash) (storage.TorrentImpl, error) { +func (bs badStorage) OpenTorrent( + context.Context, + *metainfo.Info, + metainfo.Hash, +) (storage.TorrentImpl, error) { return storage.TorrentImpl{ Piece: bs.Piece, }, nil diff --git a/client_test.go b/client_test.go index 781f04a9..f4771ac1 100644 --- a/client_test.go +++ b/client_test.go @@ -1,6 +1,7 @@ package torrent import ( + "context" "encoding/binary" "fmt" "io" @@ -413,7 +414,7 @@ func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf info, err := greetingMetainfo.UnmarshalInfo() require.NoError(t, err) ih := greetingMetainfo.HashInfoBytes() - greetingData, err := storage.NewClient(filePieceStore).OpenTorrent(&info, ih) + greetingData, err := storage.NewClient(filePieceStore).OpenTorrent(context.Background(), &info, ih) require.NoError(t, err) writeTorrentData(greetingData, info, []byte(testutil.GreetingFileContents)) // require.Equal(t, len(testutil.GreetingFileContents), written) diff --git a/request-strategy-impls_test.go b/request-strategy-impls_test.go index 322b9913..aab3b34e 100644 --- a/request-strategy-impls_test.go +++ b/request-strategy-impls_test.go @@ -1,6 +1,7 @@ package torrent import ( + "context" "io" "runtime" "testing" @@ -68,6 +69,7 @@ type storageClient struct { } func (s *storageClient) OpenTorrent( + _ context.Context, info *metainfo.Info, infoHash metainfo.Hash, ) (storage.TorrentImpl, error) { diff --git a/storage/bolt.go b/storage/bolt.go index 945b2499..4186c82d 100644 --- a/storage/bolt.go +++ b/storage/bolt.go @@ -4,6 +4,7 @@ package storage import ( + "context" "encoding/binary" "path/filepath" "time" @@ -42,7 +43,11 @@ func (me *boltClient) Close() error { return me.db.Close() } -func (me *boltClient) OpenTorrent(_ *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) { +func (me *boltClient) OpenTorrent( + _ context.Context, + _ *metainfo.Info, + infoHash metainfo.Hash, +) (TorrentImpl, error) { t := &boltTorrent{me, infoHash} return TorrentImpl{ Piece: t.Piece, diff --git a/storage/file.go b/storage/file.go index 54d1a62b..f9fe743a 100644 --- a/storage/file.go +++ b/storage/file.go @@ -1,11 +1,14 @@ package storage import ( + "context" "fmt" "io" + "log/slog" "os" "path/filepath" + "github.com/anacrolix/log" "github.com/anacrolix/missinggo/v2" "github.com/anacrolix/torrent/common" @@ -55,8 +58,14 @@ func (me fileClientImpl) Close() error { return me.opts.PieceCompletion.Close() } -func (fs fileClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (_ TorrentImpl, err error) { +func (fs fileClientImpl) OpenTorrent( + ctx context.Context, + info *metainfo.Info, + infoHash metainfo.Hash, +) (_ TorrentImpl, err error) { dir := fs.opts.TorrentDirMaker(fs.opts.ClientBaseDir, info, infoHash) + logger := log.ContextLogger(ctx).Slogger() + logger.DebugContext(ctx, "opened file torrent storage", slog.String("dir", dir)) upvertedFiles := info.UpvertedFiles() files := make([]file, 0, len(upvertedFiles)) for i, fileInfo := range upvertedFiles { diff --git a/storage/file_test.go b/storage/file_test.go index aada9193..103e8288 100644 --- a/storage/file_test.go +++ b/storage/file_test.go @@ -2,6 +2,7 @@ package storage import ( "bytes" + "context" "io" "os" "path/filepath" @@ -24,7 +25,7 @@ func TestShortFile(t *testing.T) { PieceLength: missinggo.MiB, Pieces: make([]byte, 20), } - ts, err := s.OpenTorrent(info, metainfo.Hash{}) + ts, err := s.OpenTorrent(context.Background(), info, metainfo.Hash{}) assert.NoError(t, err) f, err := os.Create(filepath.Join(td, "a")) require.NoError(t, err) diff --git a/storage/interface.go b/storage/interface.go index 41b4835e..981ed88d 100644 --- a/storage/interface.go +++ b/storage/interface.go @@ -1,6 +1,7 @@ package storage import ( + "context" "io" g "github.com/anacrolix/generics" @@ -15,7 +16,7 @@ type ClientImplCloser interface { // Represents data storage for an unspecified torrent. type ClientImpl interface { - OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) + OpenTorrent(ctx context.Context, info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) } type TorrentCapacity *func() (cap int64, capped bool) diff --git a/storage/issue95_test.go b/storage/issue95_test.go index 5b484b30..2beb4161 100644 --- a/storage/issue95_test.go +++ b/storage/issue95_test.go @@ -1,6 +1,7 @@ package storage import ( + "context" "testing" "github.com/anacrolix/missinggo/v2/resource" @@ -19,10 +20,10 @@ func testIssue95(t *testing.T, ci ClientImpl) { PieceLength: 1, } c := NewClient(ci) - t1, err := c.OpenTorrent(&info, metainfo.HashBytes([]byte("a"))) + t1, err := c.OpenTorrent(context.Background(), &info, metainfo.HashBytes([]byte("a"))) require.NoError(t, err) defer t1.Close() - t2, err := c.OpenTorrent(&info, metainfo.HashBytes([]byte("b"))) + t2, err := c.OpenTorrent(context.Background(), &info, metainfo.HashBytes([]byte("b"))) require.NoError(t, err) defer t2.Close() t2p := t2.Piece(info.Piece(0)) diff --git a/storage/issue96_test.go b/storage/issue96_test.go index cac5d96b..5c43574d 100644 --- a/storage/issue96_test.go +++ b/storage/issue96_test.go @@ -1,6 +1,7 @@ package storage import ( + "context" "testing" g "github.com/anacrolix/generics" @@ -19,7 +20,7 @@ func testMarkedCompleteMissingOnRead(t *testing.T, csf func(string) ClientImplCl Files: []metainfo.FileInfo{{Path: []string{"a"}, Length: 1}}, Pieces: make([]byte, 20), } - ts, err := cs.OpenTorrent(info, metainfo.Hash{}) + ts, err := cs.OpenTorrent(context.Background(), info, metainfo.Hash{}) require.NoError(t, err) p := ts.PieceWithHash(info.Piece(0), g.None[[]byte]()) require.NoError(t, p.MarkComplete()) diff --git a/storage/mmap.go b/storage/mmap.go index f7536591..8af98199 100644 --- a/storage/mmap.go +++ b/storage/mmap.go @@ -4,6 +4,7 @@ package storage import ( + "context" "errors" "fmt" "io" @@ -34,7 +35,11 @@ func NewMMapWithCompletion(baseDir string, completion PieceCompletion) *mmapClie } } -func (s *mmapClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (_ TorrentImpl, err error) { +func (s *mmapClientImpl) OpenTorrent( + _ context.Context, + info *metainfo.Info, + infoHash metainfo.Hash, +) (_ TorrentImpl, err error) { span, err := mMapTorrent(info, s.baseDir) t := &mmapTorrentStorage{ infoHash: infoHash, diff --git a/storage/mmap_test.go b/storage/mmap_test.go index 54260ecb..c72f9a9c 100644 --- a/storage/mmap_test.go +++ b/storage/mmap_test.go @@ -1,6 +1,7 @@ package storage import ( + "context" "testing" qt "github.com/frankban/quicktest" @@ -17,7 +18,7 @@ func TestMmapWindows(t *testing.T) { }() info, err := mi.UnmarshalInfo() c.Assert(err, qt.IsNil) - ts, err := cs.OpenTorrent(&info, mi.HashInfoBytes()) + ts, err := cs.OpenTorrent(context.Background(), &info, mi.HashInfoBytes()) c.Assert(err, qt.IsNil) defer func() { c.Check(ts.Close(), qt.IsNil) diff --git a/storage/piece-resource.go b/storage/piece-resource.go index 80b7f05f..51a8f027 100644 --- a/storage/piece-resource.go +++ b/storage/piece-resource.go @@ -2,6 +2,7 @@ package storage import ( "bytes" + "context" "encoding/hex" "fmt" "io" @@ -50,7 +51,11 @@ func (piecePerResourceTorrentImpl) Close() error { return nil } -func (s piecePerResource) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) { +func (s piecePerResource) OpenTorrent( + ctx context.Context, + info *metainfo.Info, + infoHash metainfo.Hash, +) (TorrentImpl, error) { t := piecePerResourceTorrentImpl{ s, make([]sync.RWMutex, info.NumPieces()), diff --git a/storage/safe-path_test.go b/storage/safe-path_test.go index 452ab283..13701e4b 100644 --- a/storage/safe-path_test.go +++ b/storage/safe-path_test.go @@ -1,6 +1,7 @@ package storage import ( + "context" "fmt" "log" "path/filepath" @@ -60,7 +61,7 @@ func TestFileOptsSafeFilePathHandling(t *testing.T) { ClientBaseDir: t.TempDir(), }) defer func() { c.Check(client.Close(), qt.IsNil) }() - torImpl, err := client.OpenTorrent(&info, metainfo.Hash{}) + torImpl, err := client.OpenTorrent(context.Background(), &info, metainfo.Hash{}) if _case.expectErr { c.Check(err, qt.Not(qt.IsNil)) } else { diff --git a/storage/sqlite/direct.go b/storage/sqlite/direct.go index e4e6981c..0390d583 100644 --- a/storage/sqlite/direct.go +++ b/storage/sqlite/direct.go @@ -4,6 +4,7 @@ package sqliteStorage import ( + "context" "encoding/hex" "io" "sync" @@ -62,7 +63,11 @@ func (c *client) capacity() (cap int64, capped bool) { return } -func (c *client) OpenTorrent(*metainfo.Info, metainfo.Hash) (storage.TorrentImpl, error) { +func (c *client) OpenTorrent( + context.Context, + *metainfo.Info, + metainfo.Hash, +) (storage.TorrentImpl, error) { t := torrent{c.cache} capFunc := c.capacity return storage.TorrentImpl{PieceWithHash: t.Piece, Close: t.Close, Capacity: &capFunc}, nil diff --git a/storage/test/bench-piece-mark-complete.go b/storage/test/bench-piece-mark-complete.go index 43390e33..b665bf41 100644 --- a/storage/test/bench-piece-mark-complete.go +++ b/storage/test/bench-piece-mark-complete.go @@ -2,6 +2,7 @@ package test_storage import ( "bytes" + "context" "math/rand" "sync" "testing" @@ -34,7 +35,7 @@ func BenchmarkPieceMarkComplete( Length: pieceSize * int64(numPieces), Name: "TorrentName", } - ti, err := ci.OpenTorrent(info, metainfo.Hash{}) + ti, err := ci.OpenTorrent(context.Background(), info, metainfo.Hash{}) c.Assert(err, qt.IsNil) tw := storage.Torrent{ti} defer tw.Close() diff --git a/storage/wrappers.go b/storage/wrappers.go index 8dbfdd0a..92b3e785 100644 --- a/storage/wrappers.go +++ b/storage/wrappers.go @@ -1,6 +1,7 @@ package storage import ( + "context" "io" "os" @@ -18,8 +19,12 @@ func NewClient(cl ClientImpl) *Client { return &Client{cl} } -func (cl Client) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (*Torrent, error) { - t, err := cl.ci.OpenTorrent(info, infoHash) +func (cl Client) OpenTorrent( + ctx context.Context, + info *metainfo.Info, + infoHash metainfo.Hash, +) (*Torrent, error) { + t, err := cl.ci.OpenTorrent(ctx, info, infoHash) if err != nil { return nil, err } diff --git a/test/issue377_test.go b/test/issue377_test.go index c5bfae2c..3d1025e3 100644 --- a/test/issue377_test.go +++ b/test/issue377_test.go @@ -1,6 +1,7 @@ package test import ( + "context" "errors" "io" "os" @@ -125,7 +126,11 @@ func (me *diskFullStorage) Close() error { return nil } -func (d *diskFullStorage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (storage.TorrentImpl, error) { +func (d *diskFullStorage) OpenTorrent( + _ context.Context, + info *metainfo.Info, + infoHash metainfo.Hash, +) (storage.TorrentImpl, error) { return storage.TorrentImpl{Piece: d.Piece, Close: d.Close}, nil } diff --git a/tests/issue-798/main.go b/tests/issue-798/main.go index 2755d484..6e17de56 100644 --- a/tests/issue-798/main.go +++ b/tests/issue-798/main.go @@ -4,6 +4,7 @@ import ( "fmt" app "github.com/anacrolix/gostdapp" + "github.com/anacrolix/torrent" ) diff --git a/torrent.go b/torrent.go index 26c1714d..3dd785cf 100644 --- a/torrent.go +++ b/torrent.go @@ -511,7 +511,8 @@ func (t *Torrent) setInfo(info *metainfo.Info) error { } if t.storageOpener != nil { var err error - t.storage, err = t.storageOpener.OpenTorrent(info, *t.canonicalShortInfohash()) + ctx := log.ContextWithLogger(context.Background(), t.logger) + t.storage, err = t.storageOpener.OpenTorrent(ctx, info, *t.canonicalShortInfohash()) if err != nil { return fmt.Errorf("error opening torrent storage: %s", err) }