forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
201 lines (159 loc) · 3.6 KB
/
control.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
package gocql
import (
"errors"
"fmt"
"sync/atomic"
"time"
)
// Ensure that the atomic variable is aligned to a 64bit boundary
// so that atomic operations can be applied on 32bit architectures.
type controlConn struct {
connecting uint64
session *Session
conn atomic.Value
retry RetryPolicy
quit chan struct{}
}
func createControlConn(session *Session) *controlConn {
control := &controlConn{
session: session,
quit: make(chan struct{}),
retry: &SimpleRetryPolicy{NumRetries: 3},
}
control.conn.Store((*Conn)(nil))
go control.heartBeat()
return control
}
func (c *controlConn) heartBeat() {
for {
select {
case <-c.quit:
return
case <-time.After(5 * time.Second):
}
resp, err := c.writeFrame(&writeOptionsFrame{})
if err != nil {
goto reconn
}
switch resp.(type) {
case *supportedFrame:
continue
case error:
goto reconn
default:
panic(fmt.Sprintf("gocql: unknown frame in response to options: %T", resp))
}
reconn:
c.reconnect(true)
// time.Sleep(5 * time.Second)
continue
}
}
func (c *controlConn) reconnect(refreshring bool) {
if !atomic.CompareAndSwapUint64(&c.connecting, 0, 1) {
return
}
success := false
defer func() {
// debounce reconnect a little
if success {
go func() {
time.Sleep(500 * time.Millisecond)
atomic.StoreUint64(&c.connecting, 0)
}()
} else {
atomic.StoreUint64(&c.connecting, 0)
}
}()
oldConn := c.conn.Load().(*Conn)
// TODO: should have our own roundrobbin for hosts so that we can try each
// in succession and guantee that we get a different host each time.
host, conn := c.session.pool.Pick(nil)
if conn == nil {
return
}
newConn, err := Connect(conn.addr, conn.cfg, c, c.session)
if err != nil {
host.Mark(err)
// TODO: add log handler for things like this
return
}
host.Mark(nil)
c.conn.Store(newConn)
success = true
if oldConn != nil {
oldConn.Close()
}
if refreshring && c.session.cfg.DiscoverHosts {
c.session.hostSource.refreshRing()
}
}
func (c *controlConn) HandleError(conn *Conn, err error, closed bool) {
if !closed {
return
}
oldConn := c.conn.Load().(*Conn)
if oldConn != conn {
return
}
c.reconnect(true)
}
func (c *controlConn) writeFrame(w frameWriter) (frame, error) {
conn := c.conn.Load().(*Conn)
if conn == nil {
return nil, errNoControl
}
framer, err := conn.exec(w, nil)
if err != nil {
return nil, err
}
return framer.parseFrame()
}
func (c *controlConn) withConn(fn func(*Conn) *Iter) *Iter {
const maxConnectAttempts = 5
connectAttempts := 0
for i := 0; i < maxConnectAttempts; i++ {
conn := c.conn.Load().(*Conn)
if conn == nil {
if connectAttempts > maxConnectAttempts {
break
}
connectAttempts++
c.reconnect(false)
continue
}
return fn(conn)
}
return &Iter{err: errNoControl}
}
// query will return nil if the connection is closed or nil
func (c *controlConn) query(statement string, values ...interface{}) (iter *Iter) {
q := c.session.Query(statement, values...).Consistency(One)
for {
iter = c.withConn(func(conn *Conn) *Iter {
return conn.executeQuery(q)
})
q.attempts++
if iter.err == nil || !c.retry.Attempt(q) {
break
}
}
return
}
func (c *controlConn) awaitSchemaAgreement() error {
return c.withConn(func(conn *Conn) *Iter {
return &Iter{err: conn.awaitSchemaAgreement()}
}).err
}
func (c *controlConn) addr() string {
conn := c.conn.Load().(*Conn)
if conn == nil {
return ""
}
return conn.addr
}
func (c *controlConn) close() {
// TODO: handle more gracefully
close(c.quit)
}
var errNoControl = errors.New("gocql: no control connection available")