mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-28 17:03:58 +08:00
Merge pull request #4709 from ipfs/feat/session-exchanges
Add exchange.SessionExchange interface for exchanges that support sessions
This commit is contained in:
@ -10,7 +10,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
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"
|
||||||
blockstore "gx/ipfs/QmTVDM4LCSUMFNQzbDLL9zQwp8usE6QHymFdh3h8vL9v6b/go-ipfs-blockstore"
|
blockstore "gx/ipfs/QmTVDM4LCSUMFNQzbDLL9zQwp8usE6QHymFdh3h8vL9v6b/go-ipfs-blockstore"
|
||||||
@ -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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// package exchange defines the IPFS exchange interface
|
// Package exchange defines the IPFS exchange interface
|
||||||
package exchange
|
package exchange
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -10,8 +10,7 @@ import (
|
|||||||
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
|
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Any type that implements exchange.Interface may be used as an IPFS block
|
// Interface defines the functionality of the IPFS block exchange protocol.
|
||||||
// exchange protocol.
|
|
||||||
type Interface interface { // type Exchanger interface
|
type Interface interface { // type Exchanger interface
|
||||||
Fetcher
|
Fetcher
|
||||||
|
|
||||||
@ -30,3 +29,10 @@ type Fetcher interface {
|
|||||||
GetBlock(context.Context, *cid.Cid) (blocks.Block, error)
|
GetBlock(context.Context, *cid.Cid) (blocks.Block, error)
|
||||||
GetBlocks(context.Context, []*cid.Cid) (<-chan blocks.Block, error)
|
GetBlocks(context.Context, []*cid.Cid) (<-chan blocks.Block, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SessionExchange is an exchange.Interface which supports
|
||||||
|
// sessions.
|
||||||
|
type SessionExchange interface {
|
||||||
|
Interface
|
||||||
|
NewSession(context.Context) Interface
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user