Skip to content

Commit ca3fd35

Browse files
authored
Merge pull request ipfs/go-blockservice#6 from ipfs/feat/avoid-alloc-sess
Avoid allocating a session unless we need it This commit was moved from ipfs/go-blockservice@19ff679
2 parents 0f8e872 + c717873 commit ca3fd35

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

blockservice/blockservice.go

+34-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11+
"sync"
1112

1213
blocks "github.com/ipfs/go-block-format"
1314
cid "github.com/ipfs/go-cid"
@@ -116,8 +117,9 @@ func NewSession(ctx context.Context, bs BlockService) *Session {
116117
if sessEx, ok := exch.(exchange.SessionExchange); ok {
117118
ses := sessEx.NewSession(ctx)
118119
return &Session{
119-
ses: ses,
120-
bs: bs.Blockstore(),
120+
ses: ses,
121+
sessEx: sessEx,
122+
bs: bs.Blockstore(),
121123
}
122124
}
123125
return &Session{
@@ -199,15 +201,19 @@ func (s *blockService) AddBlocks(bs []blocks.Block) error {
199201
func (s *blockService) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
200202
log.Debugf("BlockService GetBlock: '%s'", c)
201203

202-
var f exchange.Fetcher
204+
var f func() exchange.Fetcher
203205
if s.exchange != nil {
204-
f = s.exchange
206+
f = s.getExchange
205207
}
206208

207209
return getBlock(ctx, c, s.blockstore, f) // hash security
208210
}
209211

210-
func getBlock(ctx context.Context, c cid.Cid, bs blockstore.Blockstore, f exchange.Fetcher) (blocks.Block, error) {
212+
func (s *blockService) getExchange() exchange.Fetcher {
213+
return s.exchange
214+
}
215+
216+
func getBlock(ctx context.Context, c cid.Cid, bs blockstore.Blockstore, fget func() exchange.Fetcher) (blocks.Block, error) {
211217
err := verifcid.ValidateCid(c) // hash security
212218
if err != nil {
213219
return nil, err
@@ -218,7 +224,9 @@ func getBlock(ctx context.Context, c cid.Cid, bs blockstore.Blockstore, f exchan
218224
return block, nil
219225
}
220226

221-
if err == blockstore.ErrNotFound && f != nil {
227+
if err == blockstore.ErrNotFound && fget != nil {
228+
f := fget() // Don't load the exchange until we have to
229+
222230
// TODO be careful checking ErrNotFound. If the underlying
223231
// implementation changes, this will break.
224232
log.Debug("Blockservice: Searching bitswap")
@@ -245,10 +253,10 @@ func getBlock(ctx context.Context, c cid.Cid, bs blockstore.Blockstore, f exchan
245253
// the returned channel.
246254
// NB: No guarantees are made about order.
247255
func (s *blockService) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block {
248-
return getBlocks(ctx, ks, s.blockstore, s.exchange) // hash security
256+
return getBlocks(ctx, ks, s.blockstore, s.getExchange) // hash security
249257
}
250258

251-
func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, f exchange.Fetcher) <-chan blocks.Block {
259+
func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, fget func() exchange.Fetcher) <-chan blocks.Block {
252260
out := make(chan blocks.Block)
253261

254262
go func() {
@@ -284,6 +292,7 @@ func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, f ex
284292
return
285293
}
286294

295+
f := fget() // don't load exchange unless we have to
287296
rblocks, err := f.GetBlocks(ctx, misses)
288297
if err != nil {
289298
log.Debugf("Error with GetBlocks: %s", err)
@@ -318,18 +327,31 @@ func (s *blockService) Close() error {
318327

319328
// Session is a helper type to provide higher level access to bitswap sessions
320329
type Session struct {
321-
bs blockstore.Blockstore
322-
ses exchange.Fetcher
330+
bs blockstore.Blockstore
331+
ses exchange.Fetcher
332+
sessEx exchange.SessionExchange
333+
sessCtx context.Context
334+
lk sync.Mutex
335+
}
336+
337+
func (s *Session) getSession() exchange.Fetcher {
338+
s.lk.Lock()
339+
defer s.lk.Unlock()
340+
if s.ses == nil {
341+
s.ses = s.sessEx.NewSession(s.sessCtx)
342+
}
343+
344+
return s.ses
323345
}
324346

325347
// GetBlock gets a block in the context of a request session
326348
func (s *Session) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
327-
return getBlock(ctx, c, s.bs, s.ses) // hash security
349+
return getBlock(ctx, c, s.bs, s.getSession) // hash security
328350
}
329351

330352
// GetBlocks gets blocks in the context of a request session
331353
func (s *Session) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block {
332-
return getBlocks(ctx, ks, s.bs, s.ses) // hash security
354+
return getBlocks(ctx, ks, s.bs, s.getSession) // hash security
333355
}
334356

335357
var _ BlockGetter = (*Session)(nil)

0 commit comments

Comments
 (0)