forked from hazelcast/hazelcast-go-client
/
connection_strategy_config.go
128 lines (112 loc) · 4.02 KB
/
connection_strategy_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
/*
* 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 cluster
import (
"fmt"
"time"
"github.com/JorgenPo/hazelcast-go-client/hzerrors"
validate "github.com/JorgenPo/hazelcast-go-client/internal/util/validationutil"
"github.com/JorgenPo/hazelcast-go-client/types"
)
// ReconnectMode enables or disables reconnecting to a cluster.
type ReconnectMode int
func (rm *ReconnectMode) UnmarshalText(b []byte) error {
text := string(b)
switch text {
case "on":
*rm = ReconnectModeOn
case "off":
*rm = ReconnectModeOff
default:
return fmt.Errorf("invalid reconnect mode %s: %w", text, hzerrors.ErrIllegalState)
}
return nil
}
func (rm ReconnectMode) MarshalText() ([]byte, error) {
switch rm {
case ReconnectModeOn:
return []byte("on"), nil
case ReconnectModeOff:
return []byte("off"), nil
}
return nil, hzerrors.ErrIllegalState
}
const (
// ReconnectModeOn enables reconnecting to a cluster.
ReconnectModeOn ReconnectMode = iota
// ReconnectModeOff disables reconnecting to a cluster.
ReconnectModeOff
)
// ConnectionStrategyConfig contains configuration for reconnecting to a cluster.
type ConnectionStrategyConfig struct {
// Retry contains the backoff configuration.
Retry ConnectionRetryConfig
// Timeout is the maximum time before giving up reconnecting to a cluster.
// Default is 0, infinite duration when failover is not enabled and 120 seconds when it is enabled.
Timeout types.Duration `json:",omitempty"`
// ReconnectMode enables or disables reconnecting to a cluster.
ReconnectMode ReconnectMode `json:",omitempty"`
}
func (c ConnectionStrategyConfig) Clone() ConnectionStrategyConfig {
return c
}
func (c *ConnectionStrategyConfig) Validate() error {
// we set the default to 0 in order to be able to override it in failover and cluster configs
if err := validate.NonNegativeDuration(&c.Timeout, 0, "invalid timeout"); err != nil {
return err
}
return c.Retry.Validate()
}
/*
ConnectionRetryConfig contains configuration to computer the waiting the duration between connection attempts.
The waiting duration before the next reconnection attempt is found using the following formula:
backoff = minimum(MaxBackoff, InitialBackoff)
duration = backoff + backoff*Jitter*2.0*(RandomFloat64()-1.0)
next(backoff) = minimum(MaxBackoff, backoff*Multiplier)
*/
type ConnectionRetryConfig struct {
// InitialBackoff is the duration to wait for before the first reconnection attempt.
// Defaults to 1 second.
InitialBackoff types.Duration `json:",omitempty"`
// MaxBackoff is the maximum duration to wait for before the next reconnection attempt.
// Defaults to 30 seconds.
MaxBackoff types.Duration `json:",omitempty"`
// Multiplier controls the speed of increasing backoff duration.
// Defaults to 1.05.
// Should be greater than or equal to 1.
Multiplier float64 `json:",omitempty"`
// Jitter controls the amount of randomness introduces to reduce contention.
// Defaults to 0.
Jitter float64 `json:",omitempty"`
}
func (c ConnectionRetryConfig) Clone() ConnectionRetryConfig {
return c
}
func (c *ConnectionRetryConfig) Validate() error {
if err := validate.NonNegativeDuration(&c.InitialBackoff, 1*time.Second, "invalid initial backoff"); err != nil {
return err
}
if err := validate.NonNegativeDuration(&c.MaxBackoff, 30*time.Second, "invalid max backoff"); err != nil {
return err
}
if c.Multiplier == 0 {
c.Multiplier = 1.05
}
if c.Multiplier < 1.0 {
return fmt.Errorf("invalid multiplier: %w", hzerrors.ErrIllegalArgument)
}
return nil
}