generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathcontentrouter.go
297 lines (251 loc) · 7.15 KB
/
contentrouter.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package contentrouter
import (
"context"
"reflect"
"strings"
"time"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/routing/http/internal"
"github.com/ipfs/boxo/routing/http/types"
"github.com/ipfs/boxo/routing/http/types/iter"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multihash"
)
var logger = logging.Logger("routing/http/contentrouter")
const ttl = 24 * time.Hour
type Client interface {
FindProviders(ctx context.Context, key cid.Cid) (iter.ResultIter[types.Record], error)
ProvideBitswap(ctx context.Context, keys []cid.Cid, ttl time.Duration) (time.Duration, error)
FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultIter[*types.PeerRecord], err error)
GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error)
PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error
}
type contentRouter struct {
client Client
maxProvideConcurrency int
maxProvideBatchSize int
}
var _ routing.ContentRouting = (*contentRouter)(nil)
var _ routing.PeerRouting = (*contentRouter)(nil)
var _ routing.ValueStore = (*contentRouter)(nil)
var _ routinghelpers.ProvideManyRouter = (*contentRouter)(nil)
var _ routinghelpers.ReadyAbleRouter = (*contentRouter)(nil)
type option func(c *contentRouter)
func WithMaxProvideConcurrency(max int) option {
return func(c *contentRouter) {
c.maxProvideConcurrency = max
}
}
func WithMaxProvideBatchSize(max int) option {
return func(c *contentRouter) {
c.maxProvideBatchSize = max
}
}
func NewContentRoutingClient(c Client, opts ...option) *contentRouter {
cr := &contentRouter{
client: c,
maxProvideConcurrency: 5,
maxProvideBatchSize: 100,
}
for _, opt := range opts {
opt(cr)
}
return cr
}
func (c *contentRouter) Provide(ctx context.Context, key cid.Cid, announce bool) error {
// If 'true' is passed, it also announces it, otherwise it is just kept in the local
// accounting of which objects are being provided.
if !announce {
return nil
}
_, err := c.client.ProvideBitswap(ctx, []cid.Cid{key}, ttl)
return err
}
// ProvideMany provides a set of keys to the remote delegate.
// Large sets of keys are chunked into multiple requests and sent concurrently, according to the concurrency configuration.
// TODO: switch to use [client.Provide] when ready.
func (c *contentRouter) ProvideMany(ctx context.Context, mhKeys []multihash.Multihash) error {
keys := make([]cid.Cid, 0, len(mhKeys))
for _, m := range mhKeys {
keys = append(keys, cid.NewCidV1(cid.Raw, m))
}
if len(keys) <= c.maxProvideBatchSize {
_, err := c.client.ProvideBitswap(ctx, keys, ttl)
return err
}
return internal.DoBatch(
ctx,
c.maxProvideBatchSize,
c.maxProvideConcurrency,
keys,
func(ctx context.Context, batch []cid.Cid) error {
_, err := c.client.ProvideBitswap(ctx, batch, ttl)
return err
},
)
}
// Ready is part of the existing [routing.ReadyAbleRouter] interface.
func (c *contentRouter) Ready() bool {
return true
}
// readProviderResponses reads peer records (and bitswap records for legacy
// compatibility) from the iterator into the given channel.
func readProviderResponses(ctx context.Context, iter iter.ResultIter[types.Record], ch chan<- peer.AddrInfo) {
defer close(ch)
defer iter.Close()
for iter.Next() {
res := iter.Val()
if res.Err != nil {
logger.Warnf("error iterating provider responses: %s", res.Err)
continue
}
v := res.Val
switch v.GetSchema() {
case types.SchemaPeer:
result, ok := v.(*types.PeerRecord)
if !ok {
logger.Errorw(
"problem casting find providers result",
"Schema", v.GetSchema(),
"Type", reflect.TypeOf(v).String(),
)
continue
}
var addrs []multiaddr.Multiaddr
for _, a := range result.Addrs {
addrs = append(addrs, a.Multiaddr)
}
select {
case <-ctx.Done():
return
case ch <- peer.AddrInfo{
ID: *result.ID,
Addrs: addrs}:
}
//lint:ignore SA1019 // ignore staticcheck
case types.SchemaBitswap:
//lint:ignore SA1019 // ignore staticcheck
result, ok := v.(*types.BitswapRecord)
if !ok {
logger.Errorw(
"problem casting find providers result",
"Schema", v.GetSchema(),
"Type", reflect.TypeOf(v).String(),
)
continue
}
var addrs []multiaddr.Multiaddr
for _, a := range result.Addrs {
addrs = append(addrs, a.Multiaddr)
}
select {
case <-ctx.Done():
return
case ch <- peer.AddrInfo{
ID: *result.ID,
Addrs: addrs}:
}
}
}
}
func (c *contentRouter) FindProvidersAsync(ctx context.Context, key cid.Cid, numResults int) <-chan peer.AddrInfo {
resultsIter, err := c.client.FindProviders(ctx, key)
if err != nil {
logger.Warnw("error finding providers", "CID", key, "Error", err)
ch := make(chan peer.AddrInfo)
close(ch)
return ch
}
ch := make(chan peer.AddrInfo)
go readProviderResponses(ctx, resultsIter, ch)
return ch
}
func (c *contentRouter) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error) {
iter, err := c.client.FindPeers(ctx, pid)
if err != nil {
return peer.AddrInfo{}, err
}
defer iter.Close()
for iter.Next() {
res := iter.Val()
if res.Err != nil {
logger.Warnf("error iterating peer responses: %s", res.Err)
continue
}
if *res.Val.ID != pid {
logger.Warnf("searched for peerID %s, got response for %s:", pid, *res.Val.ID)
continue
}
var addrs []multiaddr.Multiaddr
for _, a := range res.Val.Addrs {
addrs = append(addrs, a.Multiaddr)
}
// If there are no addresses there's nothing of value to return
if len(addrs) == 0 {
continue
}
return peer.AddrInfo{
ID: pid,
Addrs: addrs,
}, nil
}
return peer.AddrInfo{}, routing.ErrNotFound
}
func (c *contentRouter) PutValue(ctx context.Context, key string, data []byte, opts ...routing.Option) error {
if !strings.HasPrefix(key, "/ipns/") {
return routing.ErrNotSupported
}
name, err := ipns.NameFromRoutingKey([]byte(key))
if err != nil {
return err
}
record, err := ipns.UnmarshalRecord(data)
if err != nil {
return err
}
return c.client.PutIPNS(ctx, name, record)
}
func (c *contentRouter) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
if !strings.HasPrefix(key, "/ipns/") {
return nil, routing.ErrNotSupported
}
name, err := ipns.NameFromRoutingKey([]byte(key))
if err != nil {
return nil, err
}
record, err := c.client.GetIPNS(ctx, name)
if err != nil {
return nil, err
}
return ipns.MarshalRecord(record)
}
func (c *contentRouter) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
if !strings.HasPrefix(key, "/ipns/") {
return nil, routing.ErrNotSupported
}
name, err := ipns.NameFromRoutingKey([]byte(key))
if err != nil {
return nil, err
}
ch := make(chan []byte)
go func() {
record, err := c.client.GetIPNS(ctx, name)
if err != nil {
close(ch)
return
}
raw, err := ipns.MarshalRecord(record)
if err != nil {
close(ch)
return
}
ch <- raw
close(ch)
}()
return ch, nil
}