kq: 支持 kafka.Writer BatchTimeout/BatchBytes 配置 #97
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
kq: 支持 kafka.Writer BatchTimeout/BatchBytes 配置
涉及文件:
Greptile Overview
Updated On: 2025-11-10 06:45:25 UTC
Greptile Summary
This PR enhances the Kafka queue (
kq) package by adding support for two critical Kafka writer batch configuration options:BatchTimeoutandBatchBytes. The changes introduceWithBatchTimeout()andWithBatchBytes()configuration functions that allow users to fine-tune Kafka message batching behavior for performance optimization.The implementation follows the existing functional options pattern used throughout the codebase. Two new fields are added to the
pushOptionsstruct (batchTimeoutandbatchBytes), and theNewPusher()function applies these configurations to the underlyingkafka.Writerwith proper zero-value validation. This allows users to control both the maximum time to wait before sending a batch (BatchTimeout) and the maximum batch size in bytes (BatchBytes), which are essential parameters for balancing throughput and latency in Kafka message production.The changes are well-integrated with the existing architecture and maintain backward compatibility by using zero-value checks before applying the new configurations.
Important Files Changed
Confidence score: 4/5
Sequence Diagram
sequenceDiagram participant User participant Pusher participant KafkaWriter participant ChunkExecutor User->>+Pusher: NewPusher(addrs, topic, WithBatchTimeout(), WithBatchBytes()) Pusher->>Pusher: "Create kafka.Writer with addresses and topic" Pusher->>Pusher: "Apply batch timeout and batch bytes options" Pusher->>+ChunkExecutor: "Create ChunkExecutor (if not sync mode)" ChunkExecutor-->>-Pusher: "Return executor" Pusher-->>-User: "Return Pusher instance" User->>+Pusher: Push(ctx, value) Pusher->>Pusher: "Generate timestamp key" Pusher->>+Pusher: PushWithKey(ctx, key, value) Pusher->>Pusher: "Create kafka.Message with key and value" Pusher->>Pusher: "Inject trace context into message" alt executor exists (async mode) Pusher->>+ChunkExecutor: Add(message, messageSize) ChunkExecutor-->>-Pusher: "Return error or nil" else sync mode Pusher->>+KafkaWriter: WriteMessages(ctx, message) KafkaWriter-->>-Pusher: "Return error or nil" end Pusher-->>-Pusher: "Return result" Pusher-->>-User: "Return error or nil" Note over ChunkExecutor, KafkaWriter: "ChunkExecutor batches messages and flushes to KafkaWriter" ChunkExecutor->>+KafkaWriter: WriteMessages(ctx, batchedMessages) KafkaWriter-->>-ChunkExecutor: "Return error or nil" User->>+Pusher: Close() alt executor exists Pusher->>+ChunkExecutor: Flush() ChunkExecutor-->>-Pusher: "Complete pending writes" end Pusher->>+KafkaWriter: Close() KafkaWriter-->>-Pusher: "Return error or nil" Pusher-->>-User: "Return error or nil"