18
routing "github.com/libp2p/go-libp2p/core/routing"
19
)
20
21
-type RoutingOption func(
22
- context.Context,
23
- host.Host,
24
- datastore.Batching,
25
- record.Validator,
26
- ...peer.AddrInfo,
27
-) (routing.Routing, error)
21
+type RoutingOptionArgs struct {
22
+ Ctx context.Context
23
+ Host host.Host
24
+ Datastore datastore.Batching
25
+ Validator record.Validator
26
+ BootstrapPeers []peer.AddrInfo
27
+ OptimisticProvide bool
28
+ OptimisticProvideJobsPoolSize int
29
+}
30
+
31
+type RoutingOption func(args RoutingOptionArgs) (routing.Routing, error)
32
33
// Default HTTP routers used in parallel to DHT when Routing.Type = "auto"
34
var defaultHTTPRouters = []string{
44
}
45
46
// ConstructDefaultRouting returns routers used when Routing.Type is unset or set to "auto"
43
-func ConstructDefaultRouting(peerID string, addrs []string, privKey string, routingOpt RoutingOption) func(
44
- ctx context.Context,
45
- host host.Host,
46
- dstore datastore.Batching,
47
- validator record.Validator,
48
- bootstrapPeers ...peer.AddrInfo,
49
-) (routing.Routing, error) {
50
- return func(
51
- ctx context.Context,
52
- host host.Host,
53
- dstore datastore.Batching,
54
- validator record.Validator,
55
- bootstrapPeers ...peer.AddrInfo,
56
- ) (routing.Routing, error) {
47
+func ConstructDefaultRouting(peerID string, addrs []string, privKey string, routingOpt RoutingOption) RoutingOption {
48
+ return func(args RoutingOptionArgs) (routing.Routing, error) {
49
// Defined routers will be queried in parallel (optimizing for response speed)
50
// Different trade-offs can be made by setting Routing.Type = "custom" with own Routing.Routers
51
var routers []*routinghelpers.ParallelRouter
52
61
- dhtRouting, err := routingOpt(ctx, host, dstore, validator, bootstrapPeers...)
53
+ dhtRouting, err := routingOpt(args)
54
if err != nil {
55
return nil, err
56
}
89
}
90
91
// constructDHTRouting is used when Routing.Type = "dht"
100
-func constructDHTRouting(mode dht.ModeOpt) func(
101
- ctx context.Context,
102
- host host.Host,
103
- dstore datastore.Batching,
104
- validator record.Validator,
105
- bootstrapPeers ...peer.AddrInfo,
106
-) (routing.Routing, error) {
107
- return func(
108
- ctx context.Context,
109
- host host.Host,
110
- dstore datastore.Batching,
111
- validator record.Validator,
112
- bootstrapPeers ...peer.AddrInfo,
113
- ) (routing.Routing, error) {
92
+func constructDHTRouting(mode dht.ModeOpt) RoutingOption {
93
+ return func(args RoutingOptionArgs) (routing.Routing, error) {
94
+ dhtOpts := []dht.Option{
95
+ dht.Concurrency(10),
96
+ dht.Mode(mode),
97
+ dht.Datastore(args.Datastore),
98
+ dht.Validator(args.Validator),
99
+ }
100
+ if args.OptimisticProvide {
101
+ dhtOpts = append(dhtOpts, dht.EnableOptimisticProvide())
102
+ }
103
+ if args.OptimisticProvideJobsPoolSize != 0 {
104
+ dhtOpts = append(dhtOpts, dht.OptimisticProvideJobsPoolSize(args.OptimisticProvideJobsPoolSize))
105
+ }
106
return dual.New(
115
- ctx, host,
116
- dual.DHTOption(
117
- dht.Concurrency(10),
118
- dht.Mode(mode),
119
- dht.Datastore(dstore),
120
- dht.Validator(validator)),
121
- dual.WanDHTOption(dht.BootstrapPeers(bootstrapPeers...)),
107
+ args.Ctx, args.Host,
108
+ dual.DHTOption(dhtOpts...),
109
+ dual.WanDHTOption(dht.BootstrapPeers(args.BootstrapPeers...)),
110
)
111
}
112
}
113
114
// ConstructDelegatedRouting is used when Routing.Type = "custom"
127
-func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, peerID string, addrs []string, privKey string) func(
128
- ctx context.Context,
129
- host host.Host,
130
- dstore datastore.Batching,
131
- validator record.Validator,
132
- bootstrapPeers ...peer.AddrInfo,
133
-) (routing.Routing, error) {
134
- return func(
135
- ctx context.Context,
136
- host host.Host,
137
- dstore datastore.Batching,
138
- validator record.Validator,
139
- bootstrapPeers ...peer.AddrInfo,
140
- ) (routing.Routing, error) {
115
+func ConstructDelegatedRouting(routers config.Routers, methods config.Methods, peerID string, addrs []string, privKey string) RoutingOption {
116
+ return func(args RoutingOptionArgs) (routing.Routing, error) {
117
return irouting.Parse(routers, methods,
118
&irouting.ExtraDHTParams{
143
- BootstrapPeers: bootstrapPeers,
144
- Host: host,
145
- Validator: validator,
146
- Datastore: dstore,
147
- Context: ctx,
119
+ BootstrapPeers: args.BootstrapPeers,
120
+ Host: args.Host,
121
+ Validator: args.Validator,
122
+ Datastore: args.Datastore,
123
+ Context: args.Ctx,
124
},
125
&irouting.ExtraHTTPParams{
126
PeerID: peerID,
130
}
131
}
132
157
-func constructNilRouting(
158
- ctx context.Context,
159
- host host.Host,
160
- dstore datastore.Batching,
161
- validator record.Validator,
162
- bootstrapPeers ...peer.AddrInfo,
163
-) (routing.Routing, error) {
133
+func constructNilRouting(_ RoutingOptionArgs) (routing.Routing, error) {
134
return routinghelpers.Null{}, nil
135
}
136