-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
129 lines (100 loc) · 3.62 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
124
125
126
127
128
129
package gossip
import (
"github.com/google/uuid"
log "github.com/treeforest/logger"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"time"
)
type Config struct {
// LogLevel 日志等级(调试用)
LogLevel log.Level
// Id 节点id
Id string
// Port 监听端口
Port int
// Endpoint 暴露的地址
Endpoint string
// DialTimeout 拨号超时时间
DialTimeout time.Duration
// DialOptions 拨号参数设置
DialOptions []grpc.DialOption
// ServerOptions 服务端参数设置
ServerOptions []grpc.ServerOption
// Delegate 消息代理接口(用户必须实现)
Delegate Delegate
// EventDelegate 事件代理(用于通知节点上下线)
EventDelegate EventDelegate
// GossipInterval 广播消息的间隔时间
GossipInterval time.Duration
// GossipNodes 随机选取多少个节点进行发送,-1则表示发送给所有的邻居节点
GossipNodes int
// GossipMidCacheCap 对于广播消息id缓存的最大容量
GossipMidCacheCap int
// GossipMidCacheTimeout 广播消息id缓存的超时时间,超时后将会
// 将消息id从缓存中删除
GossipMidCacheTimeout time.Duration
// PullMembershipInterval 成员同步间隔时间
PullMembershipInterval time.Duration
// HandshakeTimeout 握手超时时间
HandshakeTimeout time.Duration
// ConnTimeout 连接超时时间
ConnTimeout time.Duration
// AliveTimeInterval 主动发送心跳包的间隔时间
AliveTimeInterval time.Duration
// AliveExpirationTimeout 存活节点的超时时间,若在超时时间内没有收到
// 它的心跳包,则默认其失效,将从alive转换到dead。
AliveExpirationTimeout time.Duration
// DeadExpirationTimeout 失效节点到移除的超时时间,若在超时时间内没有
// 重连成功,则移除该节点的信息,并广播到集群中
DeadExpirationTimeout time.Duration
// AliveExpirationCheckInterval 检查存活节点是否超时的时间间隔
AliveExpirationCheckInterval time.Duration
// ReconnectInterval 重连的时间间隔
ReconnectInterval time.Duration
// MaxConnectionAttempts 最大的连接次数
MaxConnectionAttempts int
// PullInterval 拉取远端peer状态的间隔时间
PullInterval time.Duration
// PropagateIterations gossip消息的推送次数
PropagateIterations int
// BootstrapPeers 启动时连接的节点
BootstrapPeers []string
}
func DefaultConfig() *Config {
return &Config{
LogLevel: log.INFO,
Id: uuid.NewString(),
Port: 4399,
Endpoint: "localhost:4399",
DialTimeout: time.Second * 3,
DialOptions: []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
},
ServerOptions: []grpc.ServerOption{
grpc.Creds(insecure.NewCredentials()),
},
Delegate: nil,
EventDelegate: nil,
GossipInterval: time.Millisecond * 100,
GossipNodes: 6,
GossipMidCacheCap: 1024 * 1024 * 4,
GossipMidCacheTimeout: time.Second * 30,
PullMembershipInterval: time.Second * 4,
HandshakeTimeout: time.Second * 5,
ConnTimeout: time.Second * 2,
AliveTimeInterval: time.Second * 4,
AliveExpirationTimeout: time.Second * 40,
AliveExpirationCheckInterval: time.Second * 2,
ReconnectInterval: time.Second * 2,
MaxConnectionAttempts: 120, // about 4min
PullInterval: time.Second * 3,
PropagateIterations: 1,
}
}
func (c *Config) SetDialOption(opts ...grpc.DialOption) {
c.DialOptions = opts
}
func (c *Config) SetServerOption(opts ...grpc.ServerOption) {
c.ServerOptions = opts
}