-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
123 lines (102 loc) · 2.46 KB
/
config.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
package rpcx
import (
"fmt"
"time"
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/fileutil"
"github.com/meshplus/bitxhub-kit/log"
)
const (
blockChanNumber = 1024
defaultTimeout = 1 * time.Second
defaultPoolSize = 4
)
type config struct {
logger Logger
poolSize int
privateKey crypto.PrivateKey
nodesInfo []*NodeInfo
ipfsAddrs []string
timeoutLimit time.Duration // timeout limit config for dialing grpc
}
type NodeInfo struct {
Addr string
EnableTLS bool
CertPath string
CommonName string
AccessCert string
AccessKey string
}
type Option func(*config)
func WithNodesInfo(nodesInfo ...*NodeInfo) Option {
return func(config *config) {
config.nodesInfo = nodesInfo
}
}
func WithLogger(logger Logger) Option {
return func(config *config) {
config.logger = logger
}
}
func WithPrivateKey(key crypto.PrivateKey) Option {
return func(config *config) {
config.privateKey = key
}
}
func WithIPFSInfo(addrs []string) Option {
return func(config *config) {
config.ipfsAddrs = addrs
}
}
func WithTimeoutLimit(limit time.Duration) Option {
return func(config *config) {
config.timeoutLimit = limit
}
}
func WithPoolSize(size int) Option {
return func(config *config) {
config.poolSize = size
}
}
func generateConfig(opts ...Option) (*config, error) {
config := &config{}
for _, opt := range opts {
opt(config)
}
if err := checkConfig(config); err != nil {
return nil, err
}
return config, nil
}
func checkConfig(config *config) error {
if config.privateKey == nil {
return fmt.Errorf("private key is empty")
}
if len(config.nodesInfo) == 0 {
return fmt.Errorf("bitxhub addrs cant not be 0")
}
if config.logger == nil {
config.logger = log.NewWithModule("rpcx")
}
if config.timeoutLimit == 0 {
config.timeoutLimit = defaultTimeout
}
if config.poolSize == 0 {
config.poolSize = defaultPoolSize
}
// if EnableTLS is set, then tls certs must be provided
for _, nodeInfo := range config.nodesInfo {
if nodeInfo.EnableTLS {
if !fileutil.Exist(nodeInfo.CertPath) {
return fmt.Errorf("ca cert file %s is not found while tls is enabled", nodeInfo.CertPath)
}
if !fileutil.Exist(nodeInfo.AccessCert) {
return fmt.Errorf("access cert file %s is not found while tls is enabled", nodeInfo.AccessCert)
}
if !fileutil.Exist(nodeInfo.AccessKey) {
return fmt.Errorf("access key file %s is not found while tls is enabled", nodeInfo.AccessKey)
}
}
}
return nil
}