Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
fix buffer flush and signal logic
Browse files Browse the repository at this point in the history
  • Loading branch information
luisyonaldo committed Aug 19, 2019
1 parent 53274a7 commit dce8f19
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions go/inst/instance_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -2485,9 +2485,14 @@ var forceFlushInstanceWriteBuffer = make(chan bool)

func enqueueInstanceWrite(instance *Instance, instanceWasActuallyFound bool, lastError error) {
if len(instanceWriteBuffer) == config.Config.InstanceWriteBufferSize {
// Signal the "flushing" gorouting that there's work.
// Signal the "flushing" goroutine that there's work.
// We prefer doing all bulk flushes from one goroutine.
forceFlushInstanceWriteBuffer <- true
// Non blocking send to avoid blocking goroutines on sending a flush,
// if the "flushing" goroutine is not able read is because a flushing is ongoing.
select {
case forceFlushInstanceWriteBuffer <- true:
default:
}
}
instanceWriteBuffer <- instanceUpdateObject{instance, instanceWasActuallyFound, lastError}
}
Expand All @@ -2512,7 +2517,11 @@ func flushInstanceWriteBuffer() {
writeBufferLatency.Stop("wait")
writeBufferLatency.Start("flush")

for i := 0; len(instanceWriteBuffer) > 0; i++ {
// There are `DiscoveryMaxConcurrency` many goroutines trying to enqueue an instance into the buffer
// when one instance is flushed from the buffer then one discovery goroutine is ready to enqueue a new instance
// this is why we want to flush all instances in the buffer till a max of `InstanceWriteBufferSize`.
// Otherwise we can flush way more instances than what's expected.
for i := 0; i < config.Config.InstanceWriteBufferSize && len(instanceWriteBuffer) > 0; i++ {
upd := <-instanceWriteBuffer
if upd.instanceWasActuallyFound && upd.lastError == nil {
lastseen = append(lastseen, upd.instance)
Expand Down

0 comments on commit dce8f19

Please sign in to comment.