-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreliable_channel.go
More file actions
177 lines (167 loc) · 4.47 KB
/
Copy pathreliable_channel.go
File metadata and controls
177 lines (167 loc) · 4.47 KB
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
package ignite
import (
"context"
"errors"
"fmt"
"github.com/source-c/go-ignit-thin/logger"
"math/rand"
"strings"
"sync"
"sync/atomic"
"time"
)
type reliableChannel struct {
attemptsLimit int
currCh atomic.Value
cfg *clientConfiguration
mux sync.Mutex
closed atomic.Bool
log *logger.Logger
}
func (r *reliableChannel) send(ctx context.Context, opCode int16, requestWriter func(output BinaryOutputStream) error, responseReader func(input BinaryInputStream, err error)) {
if r.closed.Load() {
responseReader(nil, createClientConnectionError("channel is closed", nil))
return
}
connectFailed := false
attemptsCnt := 0
for {
currCh, err := r.currentChannel(ctx)
if err != nil {
r.log.Errorf("connection failed: %s", err)
responseReader(NewBinaryInputStream(nil, 0), err)
return
}
attemptsLimit := r.attemptsLimit
attemptsCnt++
currCh.send(ctx, opCode, requestWriter, func(input BinaryInputStream, err error) {
var connErr *ClientConnectionError
if errors.As(err, &connErr) {
connectFailed = true
}
if !connectFailed || attemptsCnt == attemptsLimit {
responseReader(input, err)
}
})
if !connectFailed || attemptsCnt == attemptsLimit {
return
}
r.log.Debug(func() string {
return fmt.Sprintf("retrying operation, retries left: %d", attemptsLimit-attemptsCnt)
})
}
}
func (r *reliableChannel) currentChannel(ctx context.Context) (*tcpChannel, error) {
if r.closed.Load() {
return nil, errors.New("channel is closed")
}
for {
currCh := r.currCh.Load()
if currCh != nil && !currCh.(*tcpChannel).isClosed() {
return currCh.(*tcpChannel), nil
}
if err := r.initConnection(ctx); err != nil {
return nil, err
}
}
}
func (r *reliableChannel) protocolContext() *ProtocolContext {
currCh := r.currCh.Load()
if currCh != nil {
return currCh.(*tcpChannel).protocolContext()
}
return nil
}
func (r *reliableChannel) close(ctx context.Context) {
if r.closed.CompareAndSwap(false, true) {
r.mux.Lock()
defer r.mux.Unlock()
r.log.Infof("closing connection to %s", r.currCh.Load())
currCh := r.currCh.Load()
if currCh != nil {
currCh.(*tcpChannel).close(ctx)
}
r.closed.Store(true)
}
}
func (r *reliableChannel) isClosed() bool {
return r.closed.Load()
}
func (r *reliableChannel) initConnection(ctx context.Context) error {
r.mux.Lock()
defer r.mux.Unlock()
oldCh := r.currCh.Load()
if oldCh != nil && !oldCh.(*tcpChannel).isClosed() {
return nil
}
addresses, err := r.cfg.addressesSupplier(ctx)
if err != nil {
return fmt.Errorf("failed to obtain addresses: %w", err)
}
if len(addresses) == 0 {
return errors.New("addresses are empty")
}
if len(addresses) > 1 && r.cfg.shuffleAddresses {
rand.New(rand.NewSource(time.Now().UnixNano()))
rand.Shuffle(len(addresses), func(i, j int) {
addresses[i], addresses[j] = addresses[j], addresses[i]
})
}
var cliConnErr *ClientConnectionError
for i := 0; i < len(addresses); i++ {
if oldCh != nil && oldCh.(*tcpChannel).addr == addresses[i] {
if i == len(addresses)-1 {
break
} else {
continue
}
}
r.log.Debug(func() string {
return fmt.Sprintf("trying to init connection to %s", addresses[i])
})
var ch *tcpChannel
ch, err = createTcpChannel(ctx, addresses[i], r.cfg)
if err == nil {
if r.cfg.retryLimit > 0 && r.cfg.retryLimit < len(addresses) {
r.attemptsLimit = r.cfg.retryLimit
} else {
r.attemptsLimit = len(addresses)
}
r.currCh.Store(ch)
r.log.Debug(func() string {
return fmt.Sprintf("successfully connected to %s", ch)
})
return nil
} else if i == len(addresses)-1 || !errors.As(err, &cliConnErr) {
break
}
}
if err != nil && errors.As(err, &cliConnErr) {
return err
} else if isHandshakeError(err) {
return err
} else {
return &ClientConnectionError{ClientError{
Message: fmt.Sprintf("connection failed to channels [%s]", strings.Join(addresses, ", ")),
}, err}
}
}
func isHandshakeError(err error) bool {
var cliAuthErr *ClientAuthenticationError
var protoErr *ClientProtocolError
return err != nil && (errors.As(err, &cliAuthErr) || errors.As(err, &protoErr))
}
func createReliableChannel(ctx context.Context, cfg *clientConfiguration) (channel, error) {
if cfg.addressesSupplier == nil {
return nil, errors.New("address supplier is nil")
}
ret := &reliableChannel{
cfg: cfg,
log: cfg.logger,
}
if err := ret.initConnection(ctx); err != nil {
ret.log.Errorf("connection failed: %s", err)
return nil, err
}
return ret, nil
}