forked from willscott/go-selfish-bitswap-client
/
session.go
223 lines (191 loc) · 4.72 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package bitswap
import (
"context"
"encoding/binary"
"io"
"os"
"sync"
"time"
"github.com/ipfs/go-cid"
pool "github.com/libp2p/go-buffer-pool"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-msgio"
"github.com/ipfs/go-bitswap/message"
pb "github.com/ipfs/go-bitswap/message/pb"
)
// Session holds state for a related set of CID requests from a single remote peer
type Session struct {
host.Host
peer peer.ID
initated sync.Once
close context.CancelFunc
conn network.Stream
connErr error
// 1 message sent on the ready chan once the connection is established
ready chan struct{}
wants chan cid.Cid
lbuf []byte
interestMtx sync.Mutex
interests map[cid.Cid]func([]byte, error)
}
// New initiates a bitswap retrieval session
func New(h host.Host, peer peer.ID) *Session {
return &Session{
Host: h,
peer: peer,
ready: make(chan struct{}),
wants: make(chan cid.Cid, 5),
lbuf: make([]byte, binary.MaxVarintLen64),
interests: make(map[cid.Cid]func([]byte, error)),
}
}
var (
// ProtocolBitswapNoVers is a legacy bitswap protocol id
ProtocolBitswapNoVers protocol.ID = "/ipfs/bitswap"
// ProtocolBitswapOneZero is the prefix for the legacy bitswap protocol
ProtocolBitswapOneZero protocol.ID = "/ipfs/bitswap/1.0.0"
// ProtocolBitswapOneOne is the the prefix for version 1.1.0
ProtocolBitswapOneOne protocol.ID = "/ipfs/bitswap/1.1.0"
// ProtocolBitswap is the current version of the bitswap protocol: 1.2.0
ProtocolBitswap protocol.ID = "/ipfs/bitswap/1.2.0"
)
func (s *Session) connect() {
sessionCtx, cncl := context.WithCancel(context.Background())
s.close = cncl
go s.writeLoop(sessionCtx)
// todo: configuratble timeout
ctx, cncl := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
defer cncl()
stream, err := s.Host.NewStream(ctx, s.peer, ProtocolBitswap, ProtocolBitswapOneZero, ProtocolBitswapOneOne, ProtocolBitswapNoVers)
s.connErr = err
s.conn = stream
if s.connErr != nil {
return
}
s.Host.SetStreamHandler(stream.Protocol(), s.onStream)
go s.onStream(s.conn)
s.ready <- struct{}{}
}
func (s *Session) onStream(stream network.Stream) {
reader := msgio.NewVarintReaderSize(stream, network.MessageSizeMax)
for {
received, err := message.FromMsgReader(reader)
if err != nil {
if os.IsTimeout(err) {
continue
}
if err == io.EOF {
return
}
s.connErr = err
s.Close()
return
}
for _, b := range received.Blocks() {
s.resolve(b.Cid(), b.RawData(), err)
}
}
}
// writeLoop is the event loop handling outbound messages
func (s *Session) writeLoop(ctx context.Context) {
cids := make([]cid.Cid, 0)
timeout := time.NewTicker(time.Millisecond * 50)
ready := false
defer close(s.ready)
defer timeout.Stop()
for {
select {
case c := <-s.wants:
cids = append(cids, c)
case <-s.ready:
ready = true
if len(cids) > 0 {
s.send(cids)
cids = make([]cid.Cid, 0)
}
case <-timeout.C:
if !ready {
continue
}
if len(cids) > 0 {
s.send(cids)
cids = make([]cid.Cid, 0)
}
case <-ctx.Done():
return
}
}
}
func (s *Session) send(cids []cid.Cid) error {
msg := message.New(false)
for _, c := range cids {
msg.AddEntry(c, 0, pb.Message_Wantlist_Block, false)
}
m := msg.ToProtoV1()
size := m.Size()
buf := pool.Get(size + binary.MaxVarintLen64)
defer pool.Put(buf)
n := binary.PutUvarint(buf, uint64(size))
written, err := m.MarshalTo(buf[n:])
if err != nil {
return err
}
n += written
_, err = s.conn.Write(buf[:n])
return err
}
// Close stops the session.
func (s *Session) Close() error {
if s.close != nil {
s.close()
}
if s.connErr != nil {
s.interestMtx.Lock()
defer s.interestMtx.Unlock()
for _, i := range s.interests {
i(nil, s.connErr)
}
// delete all interests
s.interests = make(map[cid.Cid]func([]byte, error))
}
return nil
}
func (s *Session) on(c cid.Cid, cb func([]byte, error)) {
s.interestMtx.Lock()
defer s.interestMtx.Unlock()
// todo: support multiple
s.interests[c] = cb
}
func (s *Session) resolve(c cid.Cid, data []byte, err error) {
s.interestMtx.Lock()
defer s.interestMtx.Unlock()
cb, ok := s.interests[c]
if ok {
delete(s.interests, c)
cb(data, err)
}
}
// Get a specific block of data in this session.
func (s *Session) Get(c cid.Cid) ([]byte, error) {
// confirm connected.
s.initated.Do(s.connect)
if s.connErr != nil {
return nil, s.connErr
}
s.wants <- c
// wait for want to be handled.
wg := sync.WaitGroup{}
wg.Add(1)
var data []byte
var err error
s.on(c, func(rb []byte, re error) {
data = rb
err = re
wg.Done()
})
wg.Wait()
return data, err
}