@@ -70,10 +70,14 @@ type Server struct {
70
70
wg sync.WaitGroup
71
71
kaWg sync.WaitGroup
72
72
httpWg sync.WaitGroup
73
+ runWg sync.WaitGroup
73
74
74
75
ctx context.Context
75
76
cancel context.CancelFunc
76
77
78
+ runCtx context.Context
79
+ runCancel context.CancelFunc
80
+
77
81
kaCtx context.Context
78
82
kaCancel context.CancelFunc
79
83
@@ -105,6 +109,8 @@ func (s *Server) Start() error {
105
109
106
110
var m cmux.CMux
107
111
112
+ s .runCtx , s .runCancel = context .WithCancel (s .ctx )
113
+
108
114
// protect member from data race. some functions below like GetRelayConfig,
109
115
// GetSourceBoundConfig has a built-in timeout so it will not be stuck for a
110
116
// long time.
@@ -141,10 +147,10 @@ func (s *Server) Start() error {
141
147
142
148
s .setWorker (nil , true )
143
149
144
- s .wg .Add (1 )
150
+ s .runWg .Add (1 )
145
151
go func () {
146
- s .runBackgroundJob (s .ctx )
147
- s .wg .Done ()
152
+ s .runBackgroundJob (s .runCtx )
153
+ s .runWg .Done ()
148
154
}()
149
155
150
156
s .startKeepAlive ()
@@ -160,13 +166,13 @@ func (s *Server) Start() error {
160
166
}
161
167
}
162
168
163
- s .wg .Add (1 )
169
+ s .runWg .Add (1 )
164
170
go func (ctx context.Context ) {
165
- defer s .wg .Done ()
171
+ defer s .runWg .Done ()
166
172
// TODO: handle fatal error from observeRelayConfig
167
173
//nolint:errcheck
168
174
s .observeRelayConfig (ctx , revRelay )
169
- }(s .ctx )
175
+ }(s .runCtx )
170
176
171
177
bound , sourceCfg , revBound , err := ha .GetSourceBoundConfig (s .etcdClient , s .cfg .Name )
172
178
if err != nil {
@@ -180,17 +186,17 @@ func (s *Server) Start() error {
180
186
log .L ().Info ("started to handle mysql source" , zap .String ("sourceCfg" , sourceCfg .String ()))
181
187
}
182
188
183
- s .wg .Add (1 )
189
+ s .runWg .Add (1 )
184
190
go func (ctx context.Context ) {
185
- defer s .wg .Done ()
191
+ defer s .runWg .Done ()
186
192
for {
187
193
err1 := s .observeSourceBound (ctx , revBound )
188
194
if err1 == nil {
189
195
return
190
196
}
191
197
s .restartKeepAlive ()
192
198
}
193
- }(s .ctx )
199
+ }(s .runCtx )
194
200
195
201
// create a cmux
196
202
m = cmux .New (s .rootLis )
@@ -468,8 +474,8 @@ func (s *Server) doClose() {
468
474
return
469
475
}
470
476
// stop server in advance, stop receiving source bound and relay bound
471
- s .cancel ()
472
- s .wg .Wait ()
477
+ s .runCancel ()
478
+ s .runWg .Wait ()
473
479
474
480
// stop worker and wait for return(we already lock the whole Sever, so no need use lock to get source worker)
475
481
if w := s .getSourceWorker (true ); w != nil {
@@ -493,6 +499,10 @@ func (s *Server) doClose() {
493
499
func (s * Server ) Close () {
494
500
s .doClose () // we should stop current sync first, otherwise master may schedule task on new worker while we are closing
495
501
s .stopKeepAlive ()
502
+
503
+ s .cancel ()
504
+ s .wg .Wait ()
505
+
496
506
if s .etcdClient != nil {
497
507
s .etcdClient .Close ()
498
508
}
0 commit comments