Commit 649e876d authored by Hector Sanjuan's avatar Hector Sanjuan

Add exchange.SessionExchange interface for exchanges that support sessions.

Blockservice has an explicit dependency on bitswap so it can
call NewSession. It should rely on the exchange interfaces though, not
on specific implementations.

License: MIT
Signed-off-by: default avatarHector Sanjuan <hector@protocol.ai>
parent 06a91e4a
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"github.com/ipfs/go-ipfs/blocks/blockstore" "github.com/ipfs/go-ipfs/blocks/blockstore"
exchange "github.com/ipfs/go-ipfs/exchange" exchange "github.com/ipfs/go-ipfs/exchange"
bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log" logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
...@@ -107,19 +106,22 @@ func (s *blockService) Exchange() exchange.Interface { ...@@ -107,19 +106,22 @@ func (s *blockService) Exchange() exchange.Interface {
return s.exchange return s.exchange
} }
// NewSession creates a bitswap session that allows for controlled exchange of // NewSession creates a new session that allows for
// wantlists to decrease the bandwidth overhead. // controlled exchange of wantlists to decrease the bandwidth overhead.
// If the current exchange is a SessionExchange, a new exchange
// session will be created. Otherwise, the current exchange will be used
// directly.
func NewSession(ctx context.Context, bs BlockService) *Session { func NewSession(ctx context.Context, bs BlockService) *Session {
exchange := bs.Exchange() exch := bs.Exchange()
if bswap, ok := exchange.(*bitswap.Bitswap); ok { if sessEx, ok := exch.(exchange.SessionExchange); ok {
ses := bswap.NewSession(ctx) ses := sessEx.NewSession(ctx)
return &Session{ return &Session{
ses: ses, ses: ses,
bs: bs.Blockstore(), bs: bs.Blockstore(),
} }
} }
return &Session{ return &Session{
ses: exchange, ses: exch,
bs: bs.Blockstore(), bs: bs.Blockstore(),
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment