evilrobot-01 opened a new pull request, #2456:
URL: https://github.com/apache/iggy/pull/2456

   When using a producer with background mode and no messages are being sent, 
CPU usage spikes due to a busy-loop in the `Shard` implementation. This 
one-line fix ensures `last_flush` is always updated after a linger timeout, 
preventing the tight loop.
   
   Full disclosure: bug diagnosed by Claude, along with supporting integration 
test.
   
   ## Problem
   
   In `sdk/src/clients/producer_sharding.rs`, `last_flush` is only updated when 
the buffer is non-empty:
   
   ```rust
   _ = tokio::time::sleep_until(deadline) => {
       if !buffer.is_empty() {
           Self::flush_buffer(...).await;
           last_flush = tokio::time::Instant::now();  // Only updated here
       }
   }
   ```
   
   When the buffer is empty:
   1. `last_flush` stays at its previous value
   2. `deadline = last_flush + linger_time` computes to an instant already in 
the past
   3. `sleep_until(past_instant)` returns immediately
   4. Tight loop consumes 100%+ CPU per shard
   
   ## Fix
   
   Move `last_flush` update outside the `if` block:
   
   ```rust
   _ = tokio::time::sleep_until(deadline) => {
       if !buffer.is_empty() {
           Self::flush_buffer(...).await;
       }
       last_flush = tokio::time::Instant::now();  // Always update
   }
   ```
   
   ## PR Checks
   - [x] Code Formatting
   - [x] Code Linting
   - [ ] Unit Testing: seems overly difficult to test based on fix, open to 
suggestions.
   - [x] Integration Testing: `background_linger_time_respected_after_idle` 
added, failing when fix reverted.
   - [x] Build Integrity
   - [x] Project Structure: N/A
   - [x] Check unused dependencies: N/A
   - [x] Sort dependencies: N/A
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to