coreapi: implement dht api
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@db9865b9ee918e7db54e6cffa41a0e8950b3e170 This commit was moved from ipfs/boxo@1ca0f8b291354c50b54f51c7afd42bafcee8a0a6
Łukasz Magiera committed
Mar 10, 2018 at 19:12 UTC
e11318040f8b6c396e64cfbfad1e31332275b7b8
3 files changed
+34
-1
core/coreiface/coreapi.go
+3
@@ -31,6 +31,9 @@ type CoreAPI interface {
31
// ObjectAPI returns an implementation of Object API
32
Object() ObjectAPI
33
34
+ // Dht returns an implementation of Dht API
35
+ Dht() DhtAPI
36
+
37
// ResolvePath resolves the path using Unixfs resolver
38
ResolvePath(context.Context, Path) (ResolvedPath, error)
39
core/coreiface/dht.go
+5
-1
@@ -17,7 +17,11 @@ type DhtAPI interface {
17
18
// FindProviders finds peers in the DHT who can provide a specific value
19
// given a key.
20
- FindProviders(context.Context, Path) (<-chan peer.ID, error) //TODO: is path the right choice here?
20
+ FindProviders(context.Context, Path, ...options.DhtFindProvidersOption) (<-chan peer.ID, error) //TODO: is path the right choice here?
21
+
22
+ // WithNumProviders is an option for FindProviders which specifies the
23
+ // number of peers to look for. Default is 20
24
+ WithNumProviders(numProviders int) options.DhtFindProvidersOption
25
26
// Provide announces to the network that you are providing given values
27
Provide(context.Context, Path, ...options.DhtProvideOption) error
core/coreiface/options/dht.go
+26
@@ -4,7 +4,12 @@ type DhtProvideSettings struct {
4
Recursive bool
5
}
6
7
+type DhtFindProvidersSettings struct {
8
+ NumProviders int
9
+}
10
+
11
type DhtProvideOption func(*DhtProvideSettings) error
12
+type DhtFindProvidersOption func(*DhtFindProvidersSettings) error
13
14
func DhtProvideOptions(opts ...DhtProvideOption) (*DhtProvideSettings, error) {
15
options := &DhtProvideSettings{
@@ -20,6 +25,20 @@ func DhtProvideOptions(opts ...DhtProvideOption) (*DhtProvideSettings, error) {
25
return options, nil
26
}
27
28
+func DhtFindProvidersOptions(opts ...DhtFindProvidersOption) (*DhtFindProvidersSettings, error) {
29
+ options := &DhtFindProvidersSettings{
30
+ NumProviders: 20,
31
+ }
32
+
33
+ for _, opt := range opts {
34
+ err := opt(options)
35
+ if err != nil {
36
+ return nil, err
37
+ }
38
+ }
39
+ return options, nil
40
+}
41
+
42
type DhtOptions struct{}
43
44
func (api *DhtOptions) WithRecursive(recursive bool) DhtProvideOption {
@@ -28,3 +47,10 @@ func (api *DhtOptions) WithRecursive(recursive bool) DhtProvideOption {
47
return nil
48
}
49
}
50
+
51
+func (api *DhtOptions) WithNumProviders(numProviders int) DhtFindProvidersOption {
52
+ return func(settings *DhtFindProvidersSettings) error {
53
+ settings.NumProviders = numProviders
54
+ return nil
55
+ }
56
+}