Skip to content

refactor(kafka): optimize Producer async processing with type constants#7539

Open
huangdijia wants to merge 8 commits intohyperf:masterfrom
huangdijia:refactor/kafka-producer-async-processing
Open

refactor(kafka): optimize Producer async processing with type constants#7539
huangdijia wants to merge 8 commits intohyperf:masterfrom
huangdijia:refactor/kafka-producer-async-processing

Conversation

@huangdijia
Copy link
Copy Markdown
Member

Summary

This PR refactors the Kafka Producer async processing system to improve code readability, maintainability, and efficiency.

Changes Made

  • Added type constants: Introduced SINGLE = 1 and BATCH = 2 constants for operation type identification
  • Simplified channel message structure: Replaced closure-based channel messages with structured arrays [type, args, promise]
  • Improved operation dispatch: Used match expression for cleaner type-based operation dispatch instead of closures
  • Streamlined error handling: Simplified promise management and error handling in the processing loop
  • Code optimization: Removed redundant variables and simplified logic flow

Benefits

  • Better readability: The code is now easier to understand and follow
  • Improved maintainability: Structured approach makes future modifications easier
  • Performance optimization: Reduced closure overhead and memory usage
  • Type safety: Clear operation type identification with constants

Backward Compatibility

This is an internal refactoring that maintains the same public API and functionality. No breaking changes are introduced.

Test Plan

  • Verify that single message sending still works correctly
  • Verify that batch message sending still works correctly
  • Verify that error handling works as expected
  • Verify that promise resolution/rejection works correctly
  • Run existing Kafka Producer tests to ensure no regressions

- Add SINGLE and BATCH constants for operation type identification
- Replace closure-based channel messages with structured arrays [type, args, promise]
- Use match expression for cleaner type-based operation dispatch
- Simplify error handling and promise management in loop
- Improve code readability and maintainability

This refactoring maintains the same functionality while making the code
more efficient and easier to understand.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the Kafka Producer's async processing implementation to use type constants and structured array messages instead of closures for improved readability and maintainability.

Key Changes

  • Introduced SINGLE = 1 and BATCH = 2 constants for operation type identification
  • Replaced closure-based channel messages with structured arrays [type, args, promise]
  • Implemented match expression for cleaner operation dispatch based on type constants

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

huangdijia and others added 6 commits September 13, 2025 15:09
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Replaces direct access to $this->chan with a local $chan variable and uses the nullsafe operator to prevent errors if the channel is null. This improves code safety and consistency when handling the channel in Producer methods.
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.

2 participants