-
Notifications
You must be signed in to change notification settings - Fork 6
/
cfg.go
82 lines (74 loc) · 2.35 KB
/
cfg.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
package grid
import (
"runtime"
"time"
)
// Logger hides the logging function Printf behind a simple
// interface so libraries such as logrus can be used.
type Logger interface {
Printf(string, ...interface{})
}
// ClientCfg where the only required argument is Namespace,
// other fields with their zero value will receive defaults.
type ClientCfg struct {
// Namespace of grid.
Namespace string
// Timeout for communication with etcd, and internal gossip.
Timeout time.Duration
// PeersRefreshInterval for polling list of peers in etcd.
PeersRefreshInterval time.Duration
// ConnectionsPerPeer sets the number gRPC connections to
// establish to each remote. Default is max(1, numCPUs/2).
// More connections allow for more messages per second,
// but increases the number of file-handles used.
ConnectionsPerPeer int
// Logger optionally used for logging, default is to not log.
Logger Logger
// XDSCreds specifies whether the server should use xDS APIs to receive security configuration
XDSCreds bool
}
// setClientCfgDefaults for those fields that have their zero value.
func setClientCfgDefaults(cfg *ClientCfg) {
if cfg.PeersRefreshInterval == 0 {
cfg.PeersRefreshInterval = 2 * time.Second
}
if cfg.Timeout == 0 {
cfg.Timeout = 10 * time.Second
}
if cfg.ConnectionsPerPeer == 0 {
cfg.ConnectionsPerPeer = maxInt(1, runtime.NumCPU()/2)
}
}
// ServerCfg where the only required argument is Namespace,
// other fields with their zero value will receive defaults.
type ServerCfg struct {
// Namespace of grid.
Namespace string
// DisallowLeadership to prevent leader from running on a node.
DisallowLeadership bool
// Timeout for communication with etcd, and internal gossip.
Timeout time.Duration
// LeaseDuration for data in etcd.
LeaseDuration time.Duration
// Logger optionally used for logging, default is to not log.
Logger Logger
// Annotations optionally used annotating a grid server with metadata
Annotations []string
// XDSCreds specifies whether the server should use xDS APIs to receive security configuration
XDSCreds bool
}
// setServerCfgDefaults for those fields that have their zero value.
func setServerCfgDefaults(cfg *ServerCfg) {
if cfg.Timeout == 0 {
cfg.Timeout = 10 * time.Second
}
if cfg.LeaseDuration == 0 {
cfg.LeaseDuration = 60 * time.Second
}
}
func maxInt(a, b int) int {
if a < b {
return a
}
return b
}