@@ -18,12 +18,12 @@ type QueueConnectionHandler struct {
18
18
name string
19
19
cfg queue.Cfg
20
20
21
- uuid uuid.UUID
22
- registered bool
23
- err error
24
- mutex sync.Mutex
25
- masterUpdated chan struct {}
26
- masterCnt int32
21
+ uuid uuid.UUID
22
+ registered bool
23
+ err error
24
+ mutex sync.Mutex
25
+ updated chan struct {}
26
+ masterCnt int32
27
27
}
28
28
29
29
// QueueConnectionHandler implements the ConnectionHandler interface.
@@ -32,9 +32,9 @@ var _ connection_pool.ConnectionHandler = &QueueConnectionHandler{}
32
32
// NewQueueConnectionHandler creates a QueueConnectionHandler object.
33
33
func NewQueueConnectionHandler (name string , cfg queue.Cfg ) * QueueConnectionHandler {
34
34
return & QueueConnectionHandler {
35
- name : name ,
36
- cfg : cfg ,
37
- masterUpdated : make (chan struct {}, 10 ),
35
+ name : name ,
36
+ cfg : cfg ,
37
+ updated : make (chan struct {}, 10 ),
38
38
}
39
39
}
40
40
@@ -53,14 +53,25 @@ func (h *QueueConnectionHandler) Discovered(conn *tarantool.Connection,
53
53
}
54
54
55
55
master := role == connection_pool .MasterRole
56
- if master {
57
- defer func () {
58
- h .masterUpdated <- struct {}{}
59
- }()
56
+
57
+ q := queue .New (conn , h .name )
58
+
59
+ // Check is queue ready to work.
60
+ if state , err := q .State (); err != nil {
61
+ h .updated <- struct {}{}
62
+ h .err = err
63
+ return err
64
+ } else if master && state != queue .RunningState {
65
+ return fmt .Errorf ("queue state is not RUNNING: %d" , state )
66
+ } else if ! master && state != queue .InitState && state != queue .WaitingState {
67
+ return fmt .Errorf ("queue state is not INIT and not WAITING: %d" , state )
60
68
}
61
69
70
+ defer func () {
71
+ h .updated <- struct {}{}
72
+ }()
73
+
62
74
// Set up a queue module configuration for an instance.
63
- q := queue .New (conn , h .name )
64
75
opts := queue.CfgOpts {InReplicaset : true , Ttr : 60 * time .Second }
65
76
66
77
if h .err = q .Cfg (opts ); h .err != nil {
@@ -106,7 +117,7 @@ func (h *QueueConnectionHandler) Deactivated(conn *tarantool.Connection,
106
117
107
118
// Closes closes a QueueConnectionHandler object.
108
119
func (h * QueueConnectionHandler ) Close () {
109
- close (h .masterUpdated )
120
+ close (h .updated )
110
121
}
111
122
112
123
// Example demonstrates how to use the queue package with the connection_pool
@@ -155,8 +166,10 @@ func Example_connectionPool() {
155
166
}
156
167
defer connPool .Close ()
157
168
158
- // Wait for a master instance identification in the queue.
159
- <- h .masterUpdated
169
+ // Wait for a queue initialization and master instance identification in
170
+ // the queue.
171
+ <- h .updated
172
+ <- h .updated
160
173
if h .err != nil {
161
174
fmt .Printf ("Unable to identify in the pool: %s" , h .err )
162
175
return
@@ -183,8 +196,10 @@ func Example_connectionPool() {
183
196
return
184
197
}
185
198
186
- // Wait for a new master instance re-identification.
187
- <- h .masterUpdated
199
+ // Wait for a replica instance connection and a new master instance
200
+ // re-identification.
201
+ <- h .updated
202
+ <- h .updated
188
203
h .mutex .Lock ()
189
204
err = h .err
190
205
h .mutex .Unlock ()
0 commit comments