Skip to content

Fix CPU usage during shutdown by adding sleep to queue drain loop

Description

This PR adds a small sleep (100ms) to the queue drain loop during application shutdown to significantly reduce CPU usage while waiting for the processor queue to empty.

Problem

Previously, the application used a busy-wait loop when waiting for the processor queue to empty during shutdown:

for processor.QueueSize() > 0 {
}

This caused unnecessarily high CPU utilization as the loop continuously checked the queue size without any pause.

Solution

Added a small sleep in the loop to reduce CPU load:

for processor.QueueSize() > 0 {
    time.Sleep(100 * time.Millisecond)
}

Tradeoffs

  • Significantly reduced CPU usage during shutdown
  • Potential delay of up to 100ms in shutdown completion

I guess this minor potential delay is negligible compared to the benefits of improved system resource utilization during the shutdown process.

Merge request reports

Loading