-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bitswap sessions #3867
Bitswap sessions #3867
Changes from 1 commit
47479b6
bda8c3a
e43d131
b680f49
b1247d3
579fd46
1ffb44c
eab2024
124afdb
3be5c91
dd7589b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
License: MIT Signed-off-by: Jeromy <[email protected]>
- v0.34.0-rc1
- v0.33.2
- v0.33.1
- v0.33.0
- v0.33.0-rc3
- v0.33.0-rc2
- v0.33.0-rc1
- v0.32.1
- v0.32.0
- v0.32.0-rc2
- v0.32.0-rc1
- v0.31.0
- v0.31.0-rc2
- v0.31.0-rc1
- v0.30.0
- v0.30.0-rc3
- v0.30.0-rc2
- v0.30.0-rc1
- v0.29.0
- v0.29.0-rc2
- v0.29.0-rc1
- v0.28.0
- v0.28.0-rc1
- v0.27.0
- v0.27.0-rc2
- v0.27.0-rc1
- v0.26.0
- v0.26.0-rc1
- v0.25.0
- v0.25.0-rc1
- v0.24.0
- v0.24.0-rc2
- v0.24.0-rc1
- v0.23.0
- v0.23.0-rc1
- v0.22.0
- v0.22.0-rc1
- v0.21.1
- v0.21.0
- v0.21.0-rc3
- v0.21.0-rc2
- v0.21.0-rc1
- v0.20.0
- v0.20.0-rc2
- v0.20.0-rc1
- v0.19.2
- v0.19.1
- v0.19.0
- v0.19.0-rc1
- v0.18.1
- v0.18.0
- v0.18.0-rc2
- v0.18.0-rc1
- v0.17.0
- v0.17.0-rc2
- v0.17.0-rc1
- v0.16.0
- v0.16.0-rc1
- v0.15.0
- v0.15.0-rc1
- v0.14.0
- v0.14.0-rc1
- v0.13.1
- v0.13.0
- v0.13.0-rc1
- v0.12.2
- v0.12.1
- v0.12.0
- v0.12.0-rc1
- v0.11.1
- v0.11.0
- v0.11.0-rc2
- v0.11.0-rc1
- v0.10.0
- v0.10.0-rc2
- v0.10.0-rc1
- v0.9.1
- v0.9.0
- v0.9.0-rc2
- v0.9.0-rc1
- v0.8.0
- v0.8.0-rc2
- v0.8.0-rc1
- v0.7.0
- v0.7.0-rc2
- v0.7.0-rc1
- v0.6.0
- v0.6.0-rc7
- v0.6.0-rc6
- v0.6.0-rc5
- v0.6.0-rc4
- v0.6.0-rc3
- v0.6.0-rc2
- v0.6.0-rc1
- v0.5.1
- v0.5.0
- v0.5.0-rc4
- v0.5.0-rc3
- v0.5.0-rc2
- v0.5.0-rc1
- v0.4.23
- v0.4.23-rc2
- v0.4.23-rc1
- v0.4.22
- v0.4.22-rc1
- v0.4.21
- v0.4.21-rc3
- v0.4.21-rc2
- v0.4.21-rc1
- v0.4.20
- v0.4.20-rc2
- v0.4.20-rc1
- v0.4.19
- v0.4.19-rc2
- v0.4.19-rc1
- v0.4.18
- v0.4.18-rc2
- v0.4.18-rc1
- v0.4.17
- v0.4.17-rc1
- v0.4.16
- v0.4.16-rc3
- v0.4.16-rc2
- v0.4.16-rc1
- v0.4.15
- v0.4.15-rc1
- v0.4.14
- v0.4.14-rc3
- v0.4.14-rc2
- v0.4.14-rc1
- v0.4.13
- v0.4.13-rc1
- v0.4.12
- v0.4.12-rc2
- v0.4.12-rc1
- v0.4.11
- v0.4.11-rc2
- v0.4.11-rc1
- v0.4.11-pre
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,10 @@ import ( | |
|
||
"github.com/ipfs/go-ipfs/blocks/blockstore" | ||
exchange "github.com/ipfs/go-ipfs/exchange" | ||
blocks "gx/ipfs/QmXxGS5QsUxpR3iqL5DjmsYPHR1Yz74siRQ4ChJqWFosMh/go-block-format" | ||
bitswap "github.com/ipfs/go-ipfs/exchange/bitswap" | ||
|
||
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log" | ||
blocks "gx/ipfs/QmXxGS5QsUxpR3iqL5DjmsYPHR1Yz74siRQ4ChJqWFosMh/go-block-format" | ||
cid "gx/ipfs/Qma4RJSuh7mMeJQYCqMbKzekn6EwBo7HEs5AQYjVRMQATB/go-cid" | ||
) | ||
|
||
|
@@ -31,6 +32,7 @@ type BlockService interface { | |
GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error) | ||
GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block | ||
DeleteBlock(o blocks.Block) error | ||
NewSession(context.Context) *Session | ||
Close() error | ||
} | ||
|
||
|
@@ -77,6 +79,21 @@ func (bs *blockService) Exchange() exchange.Interface { | |
return bs.exchange | ||
} | ||
|
||
func (bs *blockService) NewSession(ctx context.Context) *Session { | ||
bswap, ok := bs.Exchange().(*bitswap.Bitswap) | ||
if ok { | ||
ses := bswap.NewSession(ctx) | ||
return &Session{ | ||
ses: ses, | ||
bs: bs.blockstore, | ||
} | ||
} | ||
return &Session{ | ||
ses: bs.exchange, | ||
bs: bs.blockstore, | ||
} | ||
} | ||
|
||
// AddBlock adds a particular block to the service, Putting it into the datastore. | ||
// TODO pass a context into this if the remote.HasBlock is going to remain here. | ||
func (s *blockService) AddBlock(o blocks.Block) (*cid.Cid, error) { | ||
|
@@ -141,16 +158,25 @@ func (s *blockService) AddBlocks(bs []blocks.Block) ([]*cid.Cid, error) { | |
func (s *blockService) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error) { | ||
log.Debugf("BlockService GetBlock: '%s'", c) | ||
|
||
block, err := s.blockstore.Get(c) | ||
var f exchange.Fetcher | ||
if s.exchange != nil { | ||
f = s.exchange | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed, can't we just pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, because then the response wouldnt be |
||
|
||
return getBlock(ctx, c, s.blockstore, f) | ||
} | ||
|
||
func getBlock(ctx context.Context, c *cid.Cid, bs blockstore.Blockstore, f exchange.Fetcher) (blocks.Block, error) { | ||
block, err := bs.Get(c) | ||
if err == nil { | ||
return block, nil | ||
} | ||
|
||
if err == blockstore.ErrNotFound && s.exchange != nil { | ||
if err == blockstore.ErrNotFound && f != nil { | ||
// TODO be careful checking ErrNotFound. If the underlying | ||
// implementation changes, this will break. | ||
log.Debug("Blockservice: Searching bitswap") | ||
blk, err := s.exchange.GetBlock(ctx, c) | ||
blk, err := f.GetBlock(ctx, c) | ||
if err != nil { | ||
if err == blockstore.ErrNotFound { | ||
return nil, ErrNotFound | ||
|
@@ -172,12 +198,16 @@ func (s *blockService) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, | |
// the returned channel. | ||
// NB: No guarantees are made about order. | ||
func (s *blockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block { | ||
return getBlocks(ctx, ks, s.blockstore, s.exchange) | ||
} | ||
|
||
func getBlocks(ctx context.Context, ks []*cid.Cid, bs blockstore.Blockstore, f exchange.Fetcher) <-chan blocks.Block { | ||
out := make(chan blocks.Block) | ||
go func() { | ||
defer close(out) | ||
var misses []*cid.Cid | ||
for _, c := range ks { | ||
hit, err := s.blockstore.Get(c) | ||
hit, err := bs.Get(c) | ||
if err != nil { | ||
misses = append(misses, c) | ||
continue | ||
|
@@ -194,7 +224,7 @@ func (s *blockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan bloc | |
return | ||
} | ||
|
||
rblocks, err := s.exchange.GetBlocks(ctx, misses) | ||
rblocks, err := f.GetBlocks(ctx, misses) | ||
if err != nil { | ||
log.Debugf("Error with GetBlocks: %s", err) | ||
return | ||
|
@@ -220,3 +250,16 @@ func (s *blockService) Close() error { | |
log.Debug("blockservice is shutting down...") | ||
return s.exchange.Close() | ||
} | ||
|
||
type Session struct { | ||
bs blockstore.Blockstore | ||
ses exchange.Fetcher | ||
} | ||
|
||
func (s *Session) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error) { | ||
return getBlock(ctx, c, s.bs, s.ses) | ||
} | ||
|
||
func (s *Session) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block { | ||
return getBlocks(ctx, ks, s.bs, s.ses) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ import ( | |
"testing" | ||
"time" | ||
|
||
blocks "github.com/ipfs/go-ipfs/blocks" | ||
blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil" | ||
blocks "gx/ipfs/QmXxGS5QsUxpR3iqL5DjmsYPHR1Yz74siRQ4ChJqWFosMh/go-block-format" | ||
|
||
cid "gx/ipfs/Qma4RJSuh7mMeJQYCqMbKzekn6EwBo7HEs5AQYjVRMQATB/go-cid" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spacing |
||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a public interface and
Session
has private fields.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really didnt feel like making yet another only-implemented-once interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't this just be a helper function?
(we could also make this non-bitswap specific by adding a
NewSession(context.Context) exchange.Interface
method toexchange.Interface
but we don't need that now).