File tree Expand file tree Collapse file tree 1 file changed +16
-3
lines changed Expand file tree Collapse file tree 1 file changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,7 @@ type DiscoveryServer struct {
94
94
initialized bool
95
95
started bool
96
96
syncStarted bool
97
+ syncChannel chan interface{}
97
98
}
98
99
99
100
// NewDiscoveryServer creates a new discovery server backed by the
@@ -228,12 +229,20 @@ func (d *DiscoveryServer) startSync() {
228
229
d.outputError("start_sync", "Discovery already STARTed, cannot START_SYNC")
229
230
return
230
231
}
232
+ syncChannel := make(chan interface{}, 10) // buffer up to 10 events
231
233
if err := d.impl.StartSync(d.syncEvent); err != nil {
232
234
d.outputError("start_sync", "Cannot START_SYNC: "+err.Error())
235
+ close(syncChannel) // do not leak channel...
233
236
return
234
237
}
238
+ d.syncChannel = syncChannel
235
239
d.syncStarted = true
236
240
d.outputOk("start_sync")
241
+ go func() {
242
+ for e := range syncChannel {
243
+ d.output(e)
244
+ }
245
+ }()
237
246
}
238
247
239
248
func (d *DiscoveryServer) stop() {
@@ -246,7 +255,11 @@ func (d *DiscoveryServer) stop() {
246
255
return
247
256
}
248
257
d.started = false
249
- d.syncStarted = false
258
+ if d.syncStarted {
259
+ close(d.syncChannel)
260
+ d.syncChannel = nil
261
+ d.syncStarted = false
262
+ }
250
263
d.outputOk("stop")
251
264
}
252
265
@@ -255,10 +268,10 @@ func (d *DiscoveryServer) syncEvent(event string, port *Port) {
255
268
EventType string `json:"eventType"`
256
269
Port *Port `json:"port"`
257
270
}
258
- d.output( &syncOutputJSON{
271
+ d.syncChannel <- &syncOutputJSON{
259
272
EventType: event,
260
273
Port: port,
261
- })
274
+ }
262
275
}
263
276
264
277
type genericMessageJSON struct {
You can’t perform that action at this time.
0 commit comments