Skip to content

Commit eac9ead

Browse files
committed
In sync mode output events after command acknowledge
1 parent ac9e703 commit eac9ead

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

discovery_server.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type DiscoveryServer struct {
9494
initialized bool
9595
started bool
9696
syncStarted bool
97+
syncChannel chan interface{}
9798
}
9899

99100
// NewDiscoveryServer creates a new discovery server backed by the
@@ -228,12 +229,20 @@ func (d *DiscoveryServer) startSync() {
228229
d.outputError("start_sync", "Discovery already STARTed, cannot START_SYNC")
229230
return
230231
}
232+
syncChannel := make(chan interface{}, 10) // buffer up to 10 events
231233
if err := d.impl.StartSync(d.syncEvent); err != nil {
232234
d.outputError("start_sync", "Cannot START_SYNC: "+err.Error())
235+
close(syncChannel) // do not leak channel...
233236
return
234237
}
238+
d.syncChannel = syncChannel
235239
d.syncStarted = true
236240
d.outputOk("start_sync")
241+
go func() {
242+
for e := range syncChannel {
243+
d.output(e)
244+
}
245+
}()
237246
}
238247

239248
func (d *DiscoveryServer) stop() {
@@ -246,7 +255,11 @@ func (d *DiscoveryServer) stop() {
246255
return
247256
}
248257
d.started = false
249-
d.syncStarted = false
258+
if d.syncStarted {
259+
close(d.syncChannel)
260+
d.syncChannel = nil
261+
d.syncStarted = false
262+
}
250263
d.outputOk("stop")
251264
}
252265

@@ -255,10 +268,10 @@ func (d *DiscoveryServer) syncEvent(event string, port *Port) {
255268
EventType string `json:"eventType"`
256269
Port *Port `json:"port"`
257270
}
258-
d.output(&syncOutputJSON{
271+
d.syncChannel <- &syncOutputJSON{
259272
EventType: event,
260273
Port: port,
261-
})
274+
}
262275
}
263276

264277
type genericMessageJSON struct {

0 commit comments

Comments
 (0)