/
config.go
88 lines (78 loc) · 2.14 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
package configs
import (
"github.com/aacfactory/configures"
"github.com/aacfactory/errors"
"github.com/aacfactory/json"
"github.com/hazelcast/hazelcast-go-client"
"time"
)
type Config struct {
Addr []string `json:"addr"`
Username string `json:"username"`
Password string `json:"password"`
SSL SSLConfig `json:"ssl"`
KeepAlive KeepAliveConfig `json:"keepAlive"`
Shared json.RawMessage `json:"shared"`
Barrier json.RawMessage `json:"barrier"`
}
func (config *Config) As() (v hazelcast.Config, err error) {
v = hazelcast.NewConfig()
v.Cluster.Network.SetAddresses(config.Addr...)
v.Cluster.Security.Credentials.Username = config.Username
v.Cluster.Security.Credentials.Password = config.Password
if config.SSL.Enable {
tlsConfig, tlsErr := config.SSL.Load()
if tlsErr != nil {
err = tlsErr
return
}
v.Cluster.Network.SSL.Enabled = true
v.Cluster.Network.SSL.SetTLSConfig(tlsConfig)
}
return
}
func (config *Config) SharedConfig() (v configures.Config, err error) {
if !json.Validate(config.Shared) {
config.Shared = []byte{'{', '}'}
}
v, err = configures.NewJsonConfig(config.Shared)
if err != nil {
err = errors.Warning("hazelcast: get shared config failed").WithCause(err)
return
}
return
}
func (config *Config) BarrierConfig() (v configures.Config, err error) {
if !json.Validate(config.Barrier) {
config.Barrier = []byte{'{', '}'}
}
v, err = configures.NewJsonConfig(config.Barrier)
if err != nil {
err = errors.Warning("hazelcast: get barrier config failed").WithCause(err)
return
}
return
}
type KeepAliveConfig struct {
TTL time.Duration `json:"ttl"`
Interval time.Duration `json:"interval"`
}
func (config *KeepAliveConfig) GetTTL() time.Duration {
if config.TTL < 1*time.Second {
config.TTL = 60 * time.Second
}
return config.TTL
}
func (config *KeepAliveConfig) GetInterval() time.Duration {
if config.Interval <= config.TTL {
config.Interval = config.TTL / 2
}
return config.Interval
}
type SharedConfig struct {
LockersSize int `json:"lockersSize"`
StoreSize int `json:"storeSize"`
}
type BarrierConfig struct {
Size int `json:"size"`
}