forked from hazelcast/hazelcast-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
207 lines (187 loc) · 7.01 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hazelcast
import (
"fmt"
"time"
"github.com/hazelcast/hazelcast-go-client/cluster"
"github.com/hazelcast/hazelcast-go-client/internal/check"
"github.com/hazelcast/hazelcast-go-client/internal/hzerrors"
"github.com/hazelcast/hazelcast-go-client/logger"
"github.com/hazelcast/hazelcast-go-client/serialization"
"github.com/hazelcast/hazelcast-go-client/types"
)
// Config contains configuration for a client.
// Zero value of Config is the default configuration.
type Config struct {
lifecycleListeners map[types.UUID]LifecycleStateChangeHandler
membershipListeners map[types.UUID]cluster.MembershipStateChangeHandler
FlakeIDGenerators map[string]FlakeIDGeneratorConfig `json:",omitempty"`
Labels []string `json:",omitempty"`
ClientName string `json:",omitempty"`
Logger logger.Config `json:",omitempty"`
Failover cluster.FailoverConfig `json:",omitempty"`
Serialization serialization.Config `json:",omitempty"`
Cluster cluster.Config `json:",omitempty"`
Stats StatsConfig `json:",omitempty"`
}
// NewConfig creates the default configuration.
func NewConfig() Config {
return Config{}
}
// AddLifecycleListener adds a lifecycle listener.
// The listener is attached to the client before the client starts, so all lifecycle events can be received.
// Use the returned subscription ID to remove the listener.
// The handler must not block.
func (c *Config) AddLifecycleListener(handler LifecycleStateChangeHandler) types.UUID {
c.ensureLifecycleListeners()
id := types.NewUUID()
c.lifecycleListeners[id] = handler
return id
}
// AddMembershipListener adds a membership listener.
// The listener is attached to the client before the client starts, so all membership events can be received.
// Use the returned subscription ID to remove the listener.
func (c *Config) AddMembershipListener(handler cluster.MembershipStateChangeHandler) types.UUID {
c.ensureMembershipListeners()
id := types.NewUUID()
c.membershipListeners[id] = handler
return id
}
// SetLabels sets the labels for the client.
// These labels are displayed in the Hazelcast Management Center.
func (c *Config) SetLabels(labels ...string) {
c.Labels = labels
}
// Clone returns a copy of the configuration.
func (c *Config) Clone() Config {
c.ensureLifecycleListeners()
c.ensureMembershipListeners()
newLabels := make([]string, len(c.Labels))
copy(newLabels, c.Labels)
newFlakeIDConfigs := make(map[string]FlakeIDGeneratorConfig, len(c.FlakeIDGenerators))
for k, v := range c.FlakeIDGenerators {
newFlakeIDConfigs[k] = v
}
return Config{
ClientName: c.ClientName,
Labels: newLabels,
FlakeIDGenerators: newFlakeIDConfigs,
Cluster: c.Cluster.Clone(),
Failover: c.Failover.Clone(),
Serialization: c.Serialization.Clone(),
Logger: c.Logger.Clone(),
Stats: c.Stats.clone(),
// both lifecycleListeners and membershipListeners are not used verbatim in client creator
// so no need to copy them
lifecycleListeners: c.lifecycleListeners,
membershipListeners: c.membershipListeners,
}
}
// Validate validates the configuration and replaces missing configuration with defaults.
func (c *Config) Validate() error {
if err := c.Cluster.Validate(); err != nil {
return err
}
if err := c.Failover.Validate(c.Cluster); err != nil {
return err
}
if err := c.Serialization.Validate(); err != nil {
return err
}
if err := c.Logger.Validate(); err != nil {
return err
}
if err := c.Stats.Validate(); err != nil {
return err
}
c.ensureFlakeIDGenerators()
for _, v := range c.FlakeIDGenerators {
if err := v.Validate(); err != nil {
return err
}
}
return nil
}
func (c *Config) ensureLifecycleListeners() {
if c.lifecycleListeners == nil {
c.lifecycleListeners = map[types.UUID]LifecycleStateChangeHandler{}
}
}
func (c *Config) ensureMembershipListeners() {
if c.membershipListeners == nil {
c.membershipListeners = map[types.UUID]cluster.MembershipStateChangeHandler{}
}
}
func (c *Config) ensureFlakeIDGenerators() {
if c.FlakeIDGenerators == nil {
c.FlakeIDGenerators = map[string]FlakeIDGeneratorConfig{}
}
}
// AddFlakeIDGenerator validates the values and adds new FlakeIDGeneratorConfig with the given name.
func (c *Config) AddFlakeIDGenerator(name string, prefetchCount int32, prefetchExpiry types.Duration) error {
if _, ok := c.FlakeIDGenerators[name]; ok {
return hzerrors.NewIllegalArgumentError(fmt.Sprintf("config already exists for %s", name), nil)
}
idConfig := FlakeIDGeneratorConfig{PrefetchCount: prefetchCount, PrefetchExpiry: prefetchExpiry}
if err := idConfig.Validate(); err != nil {
return err
}
c.ensureFlakeIDGenerators()
c.FlakeIDGenerators[name] = idConfig
return nil
}
// StatsConfig contains configuration for Management Center.
type StatsConfig struct {
// Enabled enables collecting statistics.
Enabled bool `json:",omitempty"`
// Period is the period of statistics collection.
Period types.Duration `json:",omitempty"`
}
func (c StatsConfig) clone() StatsConfig {
return c
}
// Validate validates the stats configuration and replaces missing configuration with defaults.
func (c *StatsConfig) Validate() error {
if err := check.NonNegativeDuration(&c.Period, 5*time.Second, "invalid period"); err != nil {
return err
}
return nil
}
const (
maxFlakeIDPrefetchCount = 100_000
defaultFlakeIDPrefetchCount = 100
defaultFlakeIDPrefetchExpiry = types.Duration(10 * time.Minute)
)
// FlakeIDGeneratorConfig contains configuration for the pre-fetching behavior of FlakeIDGenerator.
type FlakeIDGeneratorConfig struct {
// PrefetchCount defines the number of pre-fetched IDs from cluster.
// The allowed range is [1, 100_000] and defaults to 100.
PrefetchCount int32 `json:",omitempty"`
// PrefetchExpiry defines the expiry duration of pre-fetched IDs. Defaults to 10 minutes.
PrefetchExpiry types.Duration `json:",omitempty"`
}
func (f *FlakeIDGeneratorConfig) Validate() error {
if f.PrefetchCount == 0 {
f.PrefetchCount = defaultFlakeIDPrefetchCount
} else if err := check.WithinRangeInt32(f.PrefetchCount, 1, maxFlakeIDPrefetchCount); err != nil {
return err
}
if err := check.NonNegativeDuration(&f.PrefetchExpiry, time.Duration(defaultFlakeIDPrefetchExpiry), "invalid duration"); err != nil {
return err
}
return nil
}