Skip to content

Conversation

@SmartDever02
Copy link

Summary

This PR adds a new example (BatchWriteOptimizationExample) that demonstrates the batch write optimization pattern for streaming gRPC calls. This example shows that the pull/batch write feature is already implemented and available through the isReady() and setOnReadyHandler() API.

Motivation

Several older issues requested support for batch writes to optimize flushing and reduce buffer allocations when sending many small messages. This example demonstrates that this functionality has been available for years through the manual flow control API.

This example confirms that #454 can be closed, open issues makes visitors confusing.

What This Example Demonstrates

  1. Batch Writing Pattern: Shows how to write multiple messages in a tight loop while isReady() returns true, which naturally batches messages before flushing

  2. Backpressure Handling: Demonstrates how isReady() becomes false when backpressure occurs, and how setOnReadyHandler() is called when the transport is ready again

  3. Performance Metrics: Includes counters that show:

    • Total messages written
    • Number of batches (flushes)
    • Average messages per batch
  4. Both Client and Server: Shows the pattern on both client-side and server-side streaming

Key Code Pattern

requestStream.setOnReadyHandler(() -> {
    // Write multiple messages in a batch while isReady() is true
    while (requestStream.isReady() && hasMoreData()) {
        requestStream.onNext(nextMessage());  // Batched!
    }
    // When isReady() becomes false, this handler will be called again
});

Files Changed

✅ examples/src/main/java/io/grpc/examples/manualflowcontrol/BatchWriteOptimizationExample.java - New example with detailed comments
✅ examples/src/main/java/io/grpc/examples/manualflowcontrol/README.md - Updated with new section

Contribution by Gittensor, learn more at https://gittensor.io/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant