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: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Feb 15, 2018 at 21:11 UTC
5df160e9e576f6a722ae030fb31eb39f0052f00d
2 files changed
+18
-10
blockservice/blockservice.go
+9
-7
@@ -11,7 +11,6 @@ import (
11
12
"github.com/ipfs/go-ipfs/blocks/blockstore"
13
exchange "github.com/ipfs/go-ipfs/exchange"
14
- bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
14
15
logging "gx/ipfs/QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8/go-log"
16
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
@@ -107,19 +106,22 @@ func (s *blockService) Exchange() exchange.Interface {
106
return s.exchange
107
}
108
110
-// NewSession creates a bitswap session that allows for controlled exchange of
111
-// wantlists to decrease the bandwidth overhead.
109
+// NewSession creates a new session that allows for
110
+// controlled exchange of wantlists to decrease the bandwidth overhead.
111
+// If the current exchange is a SessionExchange, a new exchange
112
+// session will be created. Otherwise, the current exchange will be used
113
+// directly.
114
func NewSession(ctx context.Context, bs BlockService) *Session {
113
- exchange := bs.Exchange()
114
- if bswap, ok := exchange.(*bitswap.Bitswap); ok {
115
- ses := bswap.NewSession(ctx)
115
+ exch := bs.Exchange()
116
+ if sessEx, ok := exch.(exchange.SessionExchange); ok {
117
+ ses := sessEx.NewSession(ctx)
118
return &Session{
119
ses: ses,
120
bs: bs.Blockstore(),
121
}
122
}
123
return &Session{
122
- ses: exchange,
124
+ ses: exch,
125
bs: bs.Blockstore(),
126
}
127
}
exchange/interface.go
+9
-3
@@ -1,4 +1,4 @@
1
-// package exchange defines the IPFS exchange interface
1
+// Package exchange defines the IPFS exchange interface
2
package exchange
3
4
import (
@@ -10,8 +10,7 @@ import (
10
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
11
)
12
13
-// Any type that implements exchange.Interface may be used as an IPFS block
14
-// exchange protocol.
13
+// Interface defines the functionality of the IPFS block exchange protocol.
14
type Interface interface { // type Exchanger interface
15
Fetcher
16
@@ -30,3 +29,10 @@ type Fetcher interface {
29
GetBlock(context.Context, *cid.Cid) (blocks.Block, error)
30
GetBlocks(context.Context, []*cid.Cid) (<-chan blocks.Block, error)
31
}
32
+
33
+// SessionExchange is an exchange.Interface which supports
34
+// sessions.
35
+type SessionExchange interface {
36
+ Interface
37
+ NewSession(context.Context) Interface
38
+}