Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit dce8f19

Browse files
committed
fix buffer flush and signal logic
1 parent 53274a7 commit dce8f19

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Diff for: go/inst/instance_dao.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -2485,9 +2485,14 @@ var forceFlushInstanceWriteBuffer = make(chan bool)
24852485

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

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

0 commit comments

Comments
 (0)