-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcompsequential.go
274 lines (230 loc) · 7.7 KB
/
compsequential.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
package routinghelpers
import (
"context"
"errors"
"sync/atomic"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/multiformats/go-multihash"
)
var _ routing.Routing = (*composableSequential)(nil)
var _ ProvideManyRouter = (*composableSequential)(nil)
var _ ReadyAbleRouter = (*composableSequential)(nil)
var _ ComposableRouter = (*composableSequential)(nil)
const sequentialName = "ComposableSequential"
type composableSequential struct {
routers []*SequentialRouter
}
func NewComposableSequential(routers []*SequentialRouter) *composableSequential {
return &composableSequential{
routers: routers,
}
}
func (r *composableSequential) Routers() []routing.Routing {
var routers []routing.Routing
for _, sr := range r.routers {
routers = append(routers, sr.Router)
}
return routers
}
// Provide calls Provide method per each router sequentially.
// If some router fails and the IgnoreError flag is true, we continue to the next router.
// Context timeout error will be also ignored if the flag is set.
func (r *composableSequential) Provide(ctx context.Context, cid cid.Cid, provide bool) (err error) {
ctx, end := tracer.Provide(sequentialName, ctx, cid, provide)
defer func() { end(err) }()
return executeSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) error {
return r.Provide(ctx, cid, provide)
})
}
// ProvideMany will call all supported Routers sequentially, falling back to iterative
// single Provide call for routers which do not support [ProvideManyRouter].
func (r *composableSequential) ProvideMany(ctx context.Context, keys []multihash.Multihash) (err error) {
ctx, end := tracer.ProvideMany(sequentialName, ctx, keys)
defer func() { end(err) }()
return executeSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) error {
if pm, ok := r.(ProvideManyRouter); ok {
return pm.ProvideMany(ctx, keys)
}
for _, k := range keys {
if err := r.Provide(ctx, cid.NewCidV1(cid.Raw, k), true); err != nil {
return err
}
}
return nil
},
)
}
// Ready will call all supported [ReadyAbleRouter] sequentially.
// If some of them are not ready, this method will return false.
func (r *composableSequential) Ready() bool {
for _, ro := range r.routers {
pm, ok := ro.Router.(ReadyAbleRouter)
if !ok {
continue
}
if !pm.Ready() {
return false
}
}
return true
}
// FindProvidersAsync calls FindProvidersAsync per each router sequentially.
// If some router fails and the IgnoreError flag is true, we continue to the next router.
// Context timeout error will be also ignored if the flag is set.
// If count is set, the channel will return up to count results, stopping routers iteration.
func (r *composableSequential) FindProvidersAsync(ctx context.Context, cid cid.Cid, count int) <-chan peer.AddrInfo {
ctx, wrapper := tracer.FindProvidersAsync(sequentialName, ctx, cid, count)
var totalCount int64
return wrapper(getChannelOrErrorSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) (<-chan peer.AddrInfo, error) {
return r.FindProvidersAsync(ctx, cid, count), nil
},
func() bool {
return atomic.AddInt64(&totalCount, 1) > int64(count) && count != 0
},
), nil)
}
// FindPeer calls FindPeer per each router sequentially.
// If some router fails and the IgnoreError flag is true, we continue to the next router.
// Context timeout error will be also ignored if the flag is set.
func (r *composableSequential) FindPeer(ctx context.Context, pid peer.ID) (p peer.AddrInfo, err error) {
ctx, end := tracer.FindPeer(sequentialName, ctx, pid)
defer func() { end(p, err) }()
return getValueOrErrorSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) (peer.AddrInfo, bool, error) {
addr, err := r.FindPeer(ctx, pid)
return addr, addr.ID == "", err
},
)
}
// If some router fails and the IgnoreError flag is true, we continue to the next router.
// Context timeout error will be also ignored if the flag is set.
func (r *composableSequential) PutValue(ctx context.Context, key string, val []byte, opts ...routing.Option) (err error) {
ctx, end := tracer.PutValue(sequentialName, ctx, key, val, opts...)
defer func() { end(err) }()
return executeSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) error {
return r.PutValue(ctx, key, val, opts...)
})
}
// If some router fails and the IgnoreError flag is true, we continue to the next router.
// Context timeout error will be also ignored if the flag is set.
func (r *composableSequential) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
return getValueOrErrorSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) ([]byte, bool, error) {
val, err := r.GetValue(ctx, key, opts...)
return val, len(val) == 0, err
},
)
}
// If some router fails and the IgnoreError flag is true, we continue to the next router.
// Context timeout error will be also ignored if the flag is set.
func (r *composableSequential) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
ctx, wrapper := tracer.SearchValue(sequentialName, ctx, key, opts...)
return wrapper(getChannelOrErrorSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) (<-chan []byte, error) {
return r.SearchValue(ctx, key, opts...)
},
func() bool { return false },
), nil)
}
// If some router fails and the IgnoreError flag is true, we continue to the next router.
// Context timeout error will be also ignored if the flag is set.
func (r *composableSequential) Bootstrap(ctx context.Context) (err error) {
ctx, end := tracer.Bootstrap(sequentialName, ctx)
defer func() { end(err) }()
return executeSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) error {
return r.Bootstrap(ctx)
},
)
}
func getValueOrErrorSequential[T any](
ctx context.Context,
routers []*SequentialRouter,
f func(context.Context, routing.Routing) (T, bool, error),
) (value T, err error) {
for _, router := range routers {
if ctxErr := ctx.Err(); ctxErr != nil {
return value, ctxErr
}
ctx, cancel := withCancelAndOptionalTimeout(ctx, router.Timeout)
defer cancel()
value, empty, err := f(ctx, router.Router)
if err != nil &&
!errors.Is(err, routing.ErrNotFound) &&
!router.IgnoreError {
return value, err
}
if empty {
continue
}
return value, nil
}
return value, routing.ErrNotFound
}
func executeSequential(
ctx context.Context,
routers []*SequentialRouter,
f func(context.Context, routing.Routing,
) error) error {
for _, router := range routers {
if ctxErr := ctx.Err(); ctxErr != nil {
return ctxErr
}
ctx, cancel := withCancelAndOptionalTimeout(ctx, router.Timeout)
defer cancel()
if err := f(ctx, router.Router); err != nil &&
!errors.Is(err, routing.ErrNotFound) &&
!router.IgnoreError {
return err
}
}
return nil
}
func getChannelOrErrorSequential[T any](
ctx context.Context,
routers []*SequentialRouter,
f func(context.Context, routing.Routing) (<-chan T, error),
shouldStop func() bool,
) chan T {
chanOut := make(chan T)
go func() {
for _, router := range routers {
if ctxErr := ctx.Err(); ctxErr != nil {
close(chanOut)
return
}
ctx, cancel := withCancelAndOptionalTimeout(ctx, router.Timeout)
defer cancel()
rch, err := f(ctx, router.Router)
if err != nil &&
!errors.Is(err, routing.ErrNotFound) &&
!router.IgnoreError {
break
}
f:
for {
select {
case <-ctx.Done():
break f
case v, ok := <-rch:
if !ok {
break f
}
select {
case <-ctx.Done():
break f
case chanOut <- v:
}
}
}
}
close(chanOut)
}()
return chanOut
}