-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_option.go
139 lines (119 loc) · 3.54 KB
/
agent_option.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
130
131
132
133
134
135
136
137
138
139
package zongzi
import (
"fmt"
"strings"
"github.com/lni/dragonboat/v4/config"
)
type AgentOption func(*Agent) error
func WithApiAddress(advertiseAddress string, bindAddress ...string) AgentOption {
return func(a *Agent) error {
a.advertiseAddress = advertiseAddress
if len(bindAddress) > 0 {
a.bindAddress = bindAddress[0]
} else {
a.bindAddress = fmt.Sprintf("0.0.0.0:%s", strings.Split(advertiseAddress, ":")[1])
}
return nil
}
}
func WithGossipAddress(advertiseAddress string, bindAddress ...string) AgentOption {
return func(a *Agent) error {
a.hostConfig.Gossip.AdvertiseAddress = advertiseAddress
if len(bindAddress) > 0 {
a.hostConfig.Gossip.BindAddress = bindAddress[0]
} else {
a.hostConfig.Gossip.BindAddress = fmt.Sprintf("0.0.0.0:%s", strings.Split(advertiseAddress, ":")[1])
}
return nil
}
}
func WithRaftAddress(raftAddress string, listenAddress ...string) AgentOption {
return func(a *Agent) error {
a.hostConfig.RaftAddress = raftAddress
if len(listenAddress) > 0 {
a.hostConfig.ListenAddress = listenAddress[0]
} else {
a.hostConfig.ListenAddress = fmt.Sprintf("0.0.0.0:%s", strings.Split(raftAddress, ":")[1])
}
return nil
}
}
func WithRaftDir(dir string) AgentOption {
return func(a *Agent) error {
a.hostConfig.NodeHostDir = dir
return nil
}
}
func WithWALDir(dir string) AgentOption {
return func(a *Agent) error {
a.hostConfig.WALDir = dir
return nil
}
}
func WithHostConfig(cfg HostConfig) AgentOption {
return func(a *Agent) error {
if len(cfg.Gossip.AdvertiseAddress) == 0 && len(a.hostConfig.Gossip.AdvertiseAddress) > 0 {
cfg.Gossip.AdvertiseAddress = a.hostConfig.Gossip.AdvertiseAddress
}
if len(cfg.Gossip.BindAddress) == 0 && len(a.hostConfig.Gossip.BindAddress) > 0 {
cfg.Gossip.BindAddress = a.hostConfig.Gossip.BindAddress
}
if len(cfg.RaftAddress) == 0 && len(a.hostConfig.RaftAddress) > 0 {
cfg.RaftAddress = a.hostConfig.RaftAddress
}
cfg.Expert.LogDBFactory = DefaultHostConfig.Expert.LogDBFactory
cfg.Expert.LogDB = DefaultHostConfig.Expert.LogDB
a.hostConfig = cfg
return nil
}
}
type HostMemory = LogDBConfig
// WithHostMemory sets the maximum memory alloted to the raft log
//
// zongzi.WithHostMemory(zongzi.HostMemory256)
func WithHostMemoryLimit(limit HostMemory) AgentOption {
return func(a *Agent) error {
a.hostConfig.Expert.LogDB = limit
return nil
}
}
var (
// HostMemory256 can be used to set max log memory usage to 256 MB
HostMemory256 = config.GetTinyMemLogDBConfig()
// HostMem1024 can be used to set max log memory usage to 1 GB
HostMemory1024 = config.GetSmallMemLogDBConfig()
// HostMem4096 can be used to set max log memory usage to 4 GB
HostMemory4096 = config.GetMediumMemLogDBConfig()
// HostMem8192 can be used to set max log memory usage to 8 GB (default)
HostMemory8192 = config.GetLargeMemLogDBConfig()
)
func WithRaftEventListener(listener RaftEventListener) AgentOption {
return func(a *Agent) error {
a.hostConfig.RaftEventListener = listener
return nil
}
}
func WithSystemEventListener(listener SystemEventListener) AgentOption {
return func(a *Agent) error {
a.hostConfig.SystemEventListener = listener
return nil
}
}
func WithHostTags(tags ...string) AgentOption {
return func(a *Agent) error {
a.hostTags = tags
return nil
}
}
func WithReplicaConfig(cfg ReplicaConfig) AgentOption {
return func(a *Agent) error {
a.replicaConfig = cfg
return nil
}
}
func WithShardController(c Controller) AgentOption {
return func(a *Agent) error {
a.controllerManager.controller = c
return nil
}
}