From 4b230aa46c2fff0df0e6be96c99ea1a76aed85a7 Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Wed, 15 May 2019 17:21:14 -0700 Subject: [PATCH] feat(session): instantiated sessions lazily Do not instantiate a bitswap session if all operations are local --- blockservice.go | 3 +-- blockservice_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/blockservice.go b/blockservice.go index 3b5a1df..0a442c8 100644 --- a/blockservice.go +++ b/blockservice.go @@ -114,9 +114,8 @@ func (s *blockService) Exchange() exchange.Interface { func NewSession(ctx context.Context, bs BlockService) *Session { exch := bs.Exchange() if sessEx, ok := exch.(exchange.SessionExchange); ok { - ses := sessEx.NewSession(ctx) return &Session{ - ses: ses, + ses: nil, sessEx: sessEx, bs: bs.Blockstore(), } diff --git a/blockservice_test.go b/blockservice_test.go index fd64eb6..4f093d7 100644 --- a/blockservice_test.go +++ b/blockservice_test.go @@ -1,6 +1,7 @@ package blockservice import ( + "context" "testing" blocks "github.com/ipfs/go-block-format" @@ -8,6 +9,7 @@ import ( dssync "github.com/ipfs/go-datastore/sync" blockstore "github.com/ipfs/go-ipfs-blockstore" butil "github.com/ipfs/go-ipfs-blocksutil" + exchange "github.com/ipfs/go-ipfs-exchange-interface" offline "github.com/ipfs/go-ipfs-exchange-offline" ) @@ -35,6 +37,52 @@ func TestWriteThroughWorks(t *testing.T) { } } +func TestLazySessionInitialization(t *testing.T) { + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())) + bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())) + bstore3 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())) + session := offline.Exchange(bstore2) + exchange := offline.Exchange(bstore3) + sessionExch := &fakeSessionExchange{Interface: exchange, session: session} + bservSessEx := NewWriteThrough(bstore, sessionExch) + bgen := butil.NewBlockGenerator() + + block := bgen.Next() + bstore.Put(block) + + block2 := bgen.Next() + session.HasBlock(block2) + + bsession := NewSession(ctx, bservSessEx) + if bsession.ses != nil { + t.Fatal("Session exchange should not instantiated session immediately") + } + returnedBlock, err := bsession.GetBlock(ctx, block.Cid()) + if err != nil { + t.Fatal("Should have fetched block locally") + } + if returnedBlock.Cid() != block.Cid() { + t.Fatal("Got incorrect block") + } + if bsession.ses != nil { + t.Fatal("Session exchange should not instantiated session if local store had block") + } + returnedBlock, err = bsession.GetBlock(ctx, block2.Cid()) + if err != nil { + t.Fatal("Should have fetched block remotely") + } + if returnedBlock.Cid() != block2.Cid() { + t.Fatal("Got incorrect block") + } + if bsession.ses != session { + t.Fatal("Should have initialized session to fetch block") + } +} + var _ blockstore.Blockstore = (*PutCountingBlockstore)(nil) type PutCountingBlockstore struct { @@ -46,3 +94,14 @@ func (bs *PutCountingBlockstore) Put(block blocks.Block) error { bs.PutCounter++ return bs.Blockstore.Put(block) } + +var _ exchange.SessionExchange = (*fakeSessionExchange)(nil) + +type fakeSessionExchange struct { + exchange.Interface + session exchange.Fetcher +} + +func (fe *fakeSessionExchange) NewSession(context.Context) exchange.Fetcher { + return fe.session +}