-
Notifications
You must be signed in to change notification settings - Fork 1
/
defaults.go
78 lines (69 loc) · 1.92 KB
/
defaults.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
/*
Copyright (C) CESS. All rights reserved.
Copyright (C) Cumulus Encrypted Storage System. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package p2pgo
import (
"os"
)
// DefaultListenPort configures libp2p to use default port.
var DefaultListenPort = func(cfg *Config) error {
port := 4001
return cfg.Apply(ListenPort(port))
}
// DefaultWorkSpace configures libp2p to use default work space.
var DefaultWorkSpace = func(cfg *Config) error {
dir, err := os.Getwd()
if err != nil {
return err
}
return cfg.Apply(Workspace(dir))
}
// DefaultWorkSpace configures libp2p to use default work space.
var DefaultDialTimeout = func(cfg *Config) error {
return cfg.Apply(DialTimeout(10))
}
// DefaultWorkSpace configures libp2p to use default work space.
var DefaultRecordCacheLen = func(cfg *Config) error {
return cfg.Apply(RecordCacheLen(1000))
}
// Complete list of default options and when to fallback on them.
//
// Please *DON'T* specify default options any other way. Putting this all here
// makes tracking defaults *much* easier.
var defaults = []struct {
fallback func(cfg *Config) bool
opt Option
}{
{
fallback: func(cfg *Config) bool { return cfg.ListenPort == 0 },
opt: DefaultListenPort,
},
{
fallback: func(cfg *Config) bool { return cfg.Workspace == "" },
opt: DefaultWorkSpace,
},
{
fallback: func(cfg *Config) bool { return cfg.DialTimeout == 0 },
opt: DefaultDialTimeout,
},
{
fallback: func(cfg *Config) bool { return cfg.RecordCacheLen <= 0 },
opt: DefaultRecordCacheLen,
},
}
// FallbackDefaults applies default options to the libp2p node if and only if no
// other relevant options have been applied. will be appended to the options
// passed into New.
var FallbackDefaults Option = func(cfg *Config) error {
for _, def := range defaults {
if !def.fallback(cfg) {
continue
}
if err := cfg.Apply(def.opt); err != nil {
return err
}
}
return nil
}