forked from libp2p/go-libp2p-routing-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompsequential.go
238 lines (205 loc) · 6.45 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
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{}
type composableSequential struct {
routers []*SequentialRouter
}
func NewComposableSequential(routers []*SequentialRouter) *composableSequential {
return &composableSequential{
routers: 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) error {
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.
func (r *composableSequential) ProvideMany(ctx context.Context, keys []multihash.Multihash) error {
return executeSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) error {
pm, ok := r.(ProvideManyRouter)
if !ok {
return nil
}
return pm.ProvideMany(ctx, keys)
},
)
}
// Ready will call all supported ProvideMany Routers 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.(ProvideManyRouter)
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 {
var totalCount int64
return 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
},
)
}
// 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) (peer.AddrInfo, error) {
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) error {
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) {
ch := getChannelOrErrorSequential(ctx, r.routers,
func(ctx context.Context, r routing.Routing) (<-chan []byte, error) {
return r.SearchValue(ctx, key, opts...)
},
func() bool { return false },
)
return ch, 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) error {
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 := context.WithTimeout(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 := context.WithTimeout(ctx, router.Timeout)
if err := f(ctx, router.Router); err != nil &&
!errors.Is(err, routing.ErrNotFound) &&
!router.IgnoreError {
cancel()
return err
}
cancel()
}
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 := context.WithTimeout(ctx, router.Timeout)
rch, err := f(ctx, router.Router)
if err != nil &&
!errors.Is(err, routing.ErrNotFound) &&
!router.IgnoreError {
cancel()
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:
}
}
}
cancel()
}
close(chanOut)
}()
return chanOut
}