Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[ISSUE #966]🚀Support client consumer message-3🚀 #968

Merged
merged 1 commit into from
Sep 8, 2024
Merged

Conversation

mxsm
Copy link
Owner

@mxsm mxsm commented Sep 8, 2024

Which Issue(s) This PR Fixes(Closes)

Fixes #966

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • New Features

    • Introduced a new message listener for concurrent message handling in the consumer module.
    • Added support for asynchronous message processing and topic subscription management.
    • Implemented a new strategy for allocating message queues evenly among consumers.
  • Bug Fixes

    • Enhanced error handling by standardizing error types throughout the client.
  • Documentation

    • Updated documentation to reflect changes in error handling and consumer configuration.
  • Refactor

    • Improved the structure of consumer configuration and management for better maintainability.
  • Style

    • Renamed error variants for improved clarity and consistency across the codebase.

@mxsm
Copy link
Owner Author

mxsm commented Sep 8, 2024

🚀Thanks for your contribution🎉. CodeRabbit(AI) will review your code first🔥

Copy link
Contributor

coderabbitai bot commented Sep 8, 2024

Walkthrough

This pull request introduces significant enhancements to the RocketMQ client, focusing on the consumer module. Key changes include the implementation of a new concurrent message listener, improved error handling with updated error types, and the introduction of a rebalance strategy module. Additionally, the consumer configuration has been modified for better management, and several methods have been updated to support synchronous operations instead of asynchronous ones. Overall, these changes aim to improve the functionality and maintainability of the consumer implementation.

Changes

Files Change Summary
rocketmq-client/examples/quickstart/consumer.rs Added a new concurrent message listener and updated the main function for message processing.
rocketmq-client/src/base/validators.rs Replaced MQClientException with MQClientErr for error handling in validation methods.
rocketmq-client/src/consumer.rs Introduced a new public module for rebalance strategies.
rocketmq-client/src/consumer/allocate_message_queue_strategy.rs Changed return type of get_name method to &'static str.
rocketmq-client/src/consumer/consumer_impl/default_mq_push_consumer_impl.rs Modified consumer configuration management and introduced new methods for consumer management.
rocketmq-client/src/consumer/default_mq_push_consumer.rs Expanded ConsumerConfig and changed subscribe method to synchronous execution.
rocketmq-client/src/error.rs Renamed error variants for clarity and consistency.
rocketmq-client/src/consumer/rebalance_strategy.rs Added a function for validating consumer IDs within the queue context.
rocketmq-client/src/consumer/rebalance_strategy/allocate_message_queue_averagely.rs Implemented round-robin allocation strategy for message queues.

Sequence Diagram(s)

sequenceDiagram
    participant Consumer
    participant MessageQueue
    participant Listener
    Consumer->>MessageQueue: Subscribe to topic
    MessageQueue-->>Consumer: Acknowledge subscription
    Consumer->>Listener: Register message listener
    Listener->>MessageQueue: Fetch messages
    MessageQueue-->>Listener: Deliver messages
    Listener->>Consumer: Process messages concurrently
Loading

Assessment against linked issues

Objective Addressed Explanation
Support client consumer message-3 ( #966 )

Suggested labels

approved, auto merge

Poem

🐰 In the meadow, messages flow,
With listeners ready, they steal the show.
Errors renamed, clear as can be,
Rabbit hops in, with joy and glee!
Consumer's dance, a synchronized play,
In RocketMQ's world, we leap and sway! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Sep 8, 2024

Codecov Report

Attention: Patch coverage is 0% with 174 lines in your changes missing coverage. Please review.

Project coverage is 21.24%. Comparing base (c54bd64) to head (3e5a7c8).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...mer/consumer_impl/default_mq_push_consumer_impl.rs 0.00% 43 Missing ⚠️
...mq-client/src/consumer/default_mq_push_consumer.rs 0.00% 31 Missing ⚠️
...lance_strategy/allocate_message_queue_averagely.rs 0.00% 22 Missing ⚠️
...er/consumer_impl/re_balance/rebalance_push_impl.rs 0.00% 21 Missing ⚠️
rocketmq-client/src/consumer/rebalance_strategy.rs 0.00% 16 Missing ⚠️
rocketmq-client/src/base/validators.rs 0.00% 15 Missing ⚠️
...t/src/consumer/default_mq_push_consumer_builder.rs 0.00% 12 Missing ⚠️
rocketmq-client/src/error.rs 0.00% 8 Missing ⚠️
...producer/producer_impl/default_mq_producer_impl.rs 0.00% 3 Missing ⚠️
...onsumer/consumer_impl/re_balance/rebalance_impl.rs 0.00% 1 Missing ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #968      +/-   ##
==========================================
- Coverage   21.45%   21.24%   -0.22%     
==========================================
  Files         402      404       +2     
  Lines       31114    31410     +296     
==========================================
- Hits         6676     6673       -3     
- Misses      24438    24737     +299     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@SpaceXCN SpaceXCN added approved PR has approved and removed ready to review labels Sep 8, 2024
@mxsm mxsm merged commit 27debb7 into main Sep 8, 2024
16 of 18 checks passed
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Outside diff range, codebase verification and nitpick comments (3)
rocketmq-client/src/error.rs (1)

23-41: Update references to renamed error types.

The verification script found occurrences of old error names in the codebase, indicating that not all references have been updated. Please update the following instances to reflect the new error type names:

  • MQClientError::MQClientException in rocketmq-client/src/consumer/consumer_impl/default_mq_push_consumer_impl.rs
  • MQClientError::RemotingTooMuchRequestException in rocketmq-client/src/producer/producer_impl/default_mq_producer_impl.rs

Ensure all references to the renamed error types are updated to maintain consistency and avoid potential runtime errors.

Analysis chain

Approved renaming and addition of error types.

Renaming error variants for consistency and clarity, and adding IllegalArgumentError, enhances the error handling framework. Ensure that all references to these error types in the codebase are updated to reflect the new names.

Run the following script to verify the usage of renamed error types:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all references to error types are updated.

# Test: Search for old error names. Expect: No occurrences.
rg --type rust -A 5 $'MQClientException|RemotingTooMuchRequestException|MQBrokerException|RequestTimeoutException|OffsetNotFoundException|RemotingException'

Length of output: 2799

rocketmq-client/src/producer/default_mq_producer.rs (1)

485-485: Incomplete Update: Instances of MQClientException still exist.

The transition to the new error type MQClientErr is incomplete. Please update the following occurrences of MQClientException to ensure consistent error handling across the codebase:

  • rocketmq-client/src/producer/producer_impl/default_mq_producer_impl.rs
  • rocketmq-client/src/consumer/consumer_impl/default_mq_push_consumer_impl.rs
Analysis chain

Approved: Error handling updated to use new error type.

The change from MQClientException to MQClientErr in the error handling of the batch function aligns with the PR's objective to enhance error clarity. Ensure that all references to the old error type have been updated across the entire codebase.

Run the following script to verify the usage of the new error type:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all references to the old error type have been updated.

# Test: Search for the old error type. Expect: No occurrences.
rg --type rust "MQClientException"

Length of output: 416

rocketmq-client/src/producer/producer_impl/default_mq_producer_impl.rs (1)

245-245: Incomplete Refactoring: Old error types still present in the codebase.

The refactoring to replace old error types with new ones is incomplete. The following occurrences of old error types were found and need to be updated:

  • MQClientException and RemotingTooMuchRequestException in rocketmq-client/src/producer/producer_impl/default_mq_producer_impl.rs
  • MQClientException in rocketmq-client/src/consumer/consumer_impl/default_mq_push_consumer_impl.rs

Please update these instances to use the new error types MQClientErr, RequestTimeoutError, and RemotingTooMuchRequestError for consistency.

Analysis chain

Approved: Error handling updated to use new error types.

The changes in error handling in the sync_send_with_message_queue_timeout function align with the PR's objective to enhance error clarity. Ensure that all references to the old error types have been updated across the entire codebase.

Run the following script to verify the usage of the new error types:

Also applies to: 256-256, 319-319, 399-399, 415-415, 422-422

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all references to the old error types have been updated.

# Test: Search for the old error types. Expect: No occurrences.
rg --type rust "MQClientException|RemotingTooMuchRequestException|RequestTimeoutException"

Length of output: 624

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c54bd64 and 3e5a7c8.

Files selected for processing (19)
  • rocketmq-client/examples/quickstart/consumer.rs (2 hunks)
  • rocketmq-client/src/base/validators.rs (11 hunks)
  • rocketmq-client/src/consumer.rs (1 hunks)
  • rocketmq-client/src/consumer/allocate_message_queue_strategy.rs (1 hunks)
  • rocketmq-client/src/consumer/consumer_impl/default_mq_push_consumer_impl.rs (28 hunks)
  • rocketmq-client/src/consumer/consumer_impl/re_balance/rebalance_impl.rs (4 hunks)
  • rocketmq-client/src/consumer/consumer_impl/re_balance/rebalance_push_impl.rs (4 hunks)
  • rocketmq-client/src/consumer/default_mq_push_consumer.rs (9 hunks)
  • rocketmq-client/src/consumer/default_mq_push_consumer_builder.rs (5 hunks)
  • rocketmq-client/src/consumer/mq_push_consumer.rs (1 hunks)
  • rocketmq-client/src/consumer/rebalance_strategy.rs (1 hunks)
  • rocketmq-client/src/consumer/rebalance_strategy/allocate_message_queue_averagely.rs (1 hunks)
  • rocketmq-client/src/error.rs (1 hunks)
  • rocketmq-client/src/factory/mq_client_instance.rs (2 hunks)
  • rocketmq-client/src/implementation/mq_admin_impl.rs (2 hunks)
  • rocketmq-client/src/implementation/mq_client_api_impl.rs (5 hunks)
  • rocketmq-client/src/producer/default_mq_producer.rs (2 hunks)
  • rocketmq-client/src/producer/producer_impl/default_mq_producer_impl.rs (35 hunks)
  • rocketmq-client/src/producer/request_future_holder.rs (2 hunks)
Files skipped from review due to trivial changes (4)
  • rocketmq-client/src/consumer.rs
  • rocketmq-client/src/consumer/consumer_impl/re_balance/rebalance_impl.rs
  • rocketmq-client/src/implementation/mq_client_api_impl.rs
  • rocketmq-client/src/producer/request_future_holder.rs
Additional comments not posted (11)
rocketmq-client/src/consumer/allocate_message_queue_strategy.rs (1)

30-30: Approved change to get_name return type.

The change to return a &'static str from get_name enhances safety by ensuring the string's lifetime matches the program's. However, verify that all implementations of this trait return a static string.

Run the following script to verify the implementations of get_name:

Verification successful

Verified: All Implementations of get_name Return a Static String

The implementation of the get_name method found in rocketmq-client/src/consumer/rebalance_strategy/allocate_message_queue_averagely.rs returns a &'static str, which is correct. This confirms that the change to the return type enhances safety by ensuring the string's lifetime matches the program's. No other implementations were found, indicating compliance across the codebase.

  • rocketmq-client/src/consumer/rebalance_strategy/allocate_message_queue_averagely.rs: Lines 61-63
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all implementations of `get_name` return a static string.

# Test: Search for implementations of `get_name`. Expect: Only static strings returned.
ast-grep --lang rust --pattern $'trait AllocateMessageQueueStrategy {
  $$$
  get_name($_) {
    $$$
  }
  $$$  
}'

Length of output: 620


Script:

#!/bin/bash
# Description: Find all implementations of the `get_name` method in Rust files and check their return types.

# Search for implementations of `get_name` in Rust files.
ast-grep --lang rust --pattern 'fn get_name(&self) -> &'static str { $$$ }'

Length of output: 93


Script:

#!/bin/bash
# Description: Find all implementations of the `get_name` method in Rust files and check their return types.

# Search for implementations of `get_name` in Rust files.
ast-grep --lang rust --pattern $'fn get_name(&self) -> &\'static str { $$$ }'

Length of output: 402

rocketmq-client/src/consumer/rebalance_strategy.rs (1)

27-56: Approved implementation of check function.

The function check is well-implemented, using IllegalArgumentError effectively for error handling and efficiently checking membership with HashSet. Ensure that the error messages provided are clear and informative.

rocketmq-client/src/consumer/mq_push_consumer.rs (1)

92-92: Verify the rationale and impact of changing subscribe to synchronous.

The method subscribe has been changed from asynchronous to synchronous. This could have significant implications for the system's performance and responsiveness. It is crucial to verify that this change aligns with the overall system architecture and does not adversely affect the consumer's ability to process messages efficiently.

Please provide the rationale behind this change and assess its impact on the system's performance and scalability.

rocketmq-client/src/base/validators.rs (1)

26-26: Approve the standardization of error handling.

The replacement of MQClientException with MQClientErr across various validation methods helps standardize error handling within the project. This change should maintain or enhance the clarity and utility of error messages provided to the users. It is important to ensure that MQClientErr includes all necessary information for effective error resolution and does not omit any critical details that were previously available with MQClientException.

Also applies to: 38-38, 42-42, 49-49, 67-67, 77-77, 85-85, 92-92, 104-104, 120-120, 124-124, 134-134, 149-149, 159-159, 170-170, 182-182

rocketmq-client/src/consumer/consumer_impl/re_balance/rebalance_push_impl.rs (1)

42-44: Approve the enhancements in RebalancePushImpl.

The introduction of ArcRefCellWrapper and WeakCellWrapper in the RebalancePushImpl struct, along with the modifications to various methods, enhances the management of shared mutable state and references. These changes are likely to improve the flexibility and robustness of the rebalancing functionality. It is important to ensure that these changes are thoroughly tested, especially in concurrent environments, to avoid any potential issues with memory management or thread safety.

Also applies to: 48-56, 62-67, 70-70, 74-74, 84-85, 93-94, 97-109, 164-166, 170-170

rocketmq-client/src/consumer/default_mq_push_consumer_builder.rs (1)

36-36: Approved: Addition of topic_sub_expression field and its initialization.

The new field topic_sub_expression in DefaultMQPushConsumerBuilder is correctly initialized in the constructor. This change supports enhanced subscription management capabilities.

Also applies to: 76-76

rocketmq-client/src/consumer/default_mq_push_consumer.rs (2)

59-60: Approved: Addition of topic and sub_expression fields and their initialization.

The new fields topic and sub_expression in ConsumerConfig are correctly initialized in the constructor with default empty strings. This change supports enhanced subscription management capabilities.

Also applies to: 377-378


585-605: Approved with caution: Refactored subscribe method to use synchronous threading model.

The refactoring of the subscribe method to use a synchronous threading model simplifies the control flow but introduces complexity in thread management. Ensure thorough testing and monitoring to detect any potential issues with race conditions or deadlocks that could arise from this new model.

Consider adding comprehensive tests to verify the new threading model:

#[test]
fn test_subscribe_thread_safety() {
    // Implement test to check for race conditions or deadlocks
}
rocketmq-client/src/consumer/consumer_impl/default_mq_push_consumer_impl.rs (2)

76-76: Approved: Addition of client_instance field and its initialization.

The new field client_instance in DefaultMQPushConsumerImpl is correctly initialized in the constructor with None. This change supports enhanced client instance management capabilities.

Also applies to: 81-81, 111-111, 124-124


143-149: Approved: Addition of set_default_mqpush_consumer_impl method.

The method set_default_mqpush_consumer_impl enhances flexibility in dependency management by allowing a weak reference to be set for the consumer implementation. This approach is beneficial for managing the lifecycle of consumer instances and avoiding memory leaks.

rocketmq-client/src/factory/mq_client_instance.rs (1)

241-241: Updated Error Handling:

The error handling in the start method has been updated to use MQClientErr instead of MQClientException. This change is consistent with the PR's objectives to improve error handling clarity.

Run the following script to verify the integration of the new error type with other components:

Verification successful

Successful Integration of MQClientErr:

The MQClientErr error type has been successfully integrated across the codebase, replacing the previous MQClientException. This change is consistently applied in various modules, ensuring improved error handling clarity.

  • Locations:
    • rocketmq-client/src/error.rs: Definition of MQClientErr.
    • Various modules including producer, consumer, base, implementation, and factory.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the integration of `MQClientErr` with other components.

# Test: Search for the usage of `MQClientErr`. Expect: Consistent usage across the project.
rg --type rust -A 5 $'MQClientErr'

Length of output: 64180

Comment on lines +23 to +64
pub struct AllocateMessageQueueAveragely;

impl AllocateMessageQueueStrategy for AllocateMessageQueueAveragely {
fn allocate(
&self,
consumer_group: &str,
current_cid: &str,
mq_all: &[MessageQueue],
cid_all: &[String],
) -> Result<Vec<MessageQueue>> {
let mut result = Vec::new();
if !check(consumer_group, current_cid, mq_all, cid_all)? {
return Ok(result);
}

let index = cid_all
.iter()
.position(|cid| cid == current_cid)
.unwrap_or(0);
let mod_val = mq_all.len() % cid_all.len();
let average_size = if mq_all.len() <= cid_all.len() {
1
} else if mod_val > 0 && index < mod_val {
mq_all.len() / cid_all.len() + 1
} else {
mq_all.len() / cid_all.len()
};
let start_index = if mod_val > 0 && index < mod_val {
index * average_size
} else {
index * average_size + mod_val
};
let range = average_size.min(mq_all.len() - start_index);
for i in 0..range {
result.push(mq_all[start_index + i].clone());
}
Ok(result)
}
fn get_name(&self) -> &'static str {
"AVG"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Comprehensive Review of New Rebalance Strategy Implementation

The new file introduces a struct AllocateMessageQueueAveragely and implements the AllocateMessageQueueStrategy trait for it. Here are some observations and suggestions:

  1. Correctness and Logic:

    • The allocate function correctly checks if the consumer is eligible to receive message queues before proceeding with the allocation logic.
    • The use of unwrap_or(0) at line 41 is potentially risky if current_cid is not found in cid_all. Consider handling this case more gracefully to avoid unexpected behavior.
  2. Performance:

    • The calculation of index, mod_val, and average_size is efficient and handles different scenarios of queue sizes and consumer counts.
  3. Error Handling:

    • Proper use of Result and early returns on error conditions are good practices that are followed here.
  4. Readability and Maintainability:

    • The code is generally well-structured and readable. Comments explaining the logic of queue allocation could enhance maintainability, especially for new contributors.
  5. Best Practices:

    • Implementing get_name to return a static string is a good practice for identifying the strategy. However, consider making the strategy name more descriptive than "AVG".

Overall, the implementation is solid, but adding more descriptive comments and handling potential edge cases more robustly would be beneficial.

Comment on lines +51 to +63
pub struct MyMessageListener;

impl MessageListenerConcurrently for MyMessageListener {
fn consume_message(
&self,
msgs: Vec<MessageExt>,
_context: ConsumeConcurrentlyContext,
) -> Result<ConsumeConcurrentlyStatus> {
for msg in msgs {
println!("Receive message: {:?}", msg);
}
Ok(())
});*/
consumer.start().await?;

Ok(())
Ok(ConsumeConcurrentlyStatus::ConsumeSuccess)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Review of New Message Listener Implementation

The implementation of MyMessageListener in the example consumer file introduces concurrent message handling capabilities:

  1. Correctness and Logic:

    • The consume_message method processes each message and logs its content. This is a simple and effective demonstration of message processing.
    • The method returns a ConsumeConcurrentlyStatus::ConsumeSuccess which is appropriate for this example.
  2. Best Practices:

    • Using println! for logging in examples is acceptable, but for more robust applications, integrating a logging framework would be advisable.
  3. Error Handling:

    • The method handles the basic case well. However, adding error handling for potential issues during message processing could make the example more comprehensive.

Overall, the example is well-implemented for demonstration purposes but could benefit from more detailed error handling and the use of a logging framework for more serious applications.

@@ -83,7 +83,7 @@ impl MQAdminImpl {
));
}
}
Err(MQClientException(
Err(MQClientErr(
Copy link
Contributor

Choose a reason for hiding this comment

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

Review of Error Handling Update in MQAdminImpl

The update to use MQClientErr for error handling in the fetch_publish_message_queues method is a significant change:

  1. Correctness and Logic:

    • The new error type MQClientErr is used consistently to handle errors related to message queue retrieval. This standardizes error handling and improves the clarity of error messages.
  2. Best Practices:

    • The use of a formatted error message that includes the topic name helps in diagnosing issues more effectively.
  3. Potential Improvements:

    • Consider adding more specific error codes or categories to MQClientErr to further enhance error diagnostics and handling.

Overall, the change is a positive step towards better error management in the RocketMQ client. Further refinement of the error categorization could enhance its effectiveness.

Comment on lines +156 to +162
pub fn subscribe(
mut self,
topic: impl Into<String>,
sub_expression: impl Into<String>,
) -> Self {
self.topic_sub_expression.0 = Some(topic.into());
self.topic_sub_expression.1 = Some(sub_expression.into());
Copy link
Contributor

Choose a reason for hiding this comment

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

Approved: Refactored subscribe method with enhanced clarity.

The refactoring of the subscribe method to directly assign topic and sub_expression to topic_sub_expression simplifies the subscription setup process. However, consider adding error handling or validation to ensure that neither topic nor sub_expression are empty strings before assignment, which could prevent runtime issues.

Consider adding validation for topic and sub_expression:

if topic.into().is_empty() || sub_expression.into().is_empty() {
    return Err("Topic and subscription expression cannot be empty");
}

Comment on lines +614 to +632
pub async fn subscribe(&mut self, topic: &str, sub_expression: &str) -> Result<()> {
let subscription_data = FilterAPI::build_subscription_data(topic, sub_expression);
if let Err(e) = subscription_data {
return Err(MQClientError::MQClientErr(
-1,
format!("buildSubscriptionData exception, {}", e),
));
}
let subscription_data = subscription_data.unwrap();
self.rebalance_impl
.put_subscription_data(topic, subscription_data)
.await;
if let Some(ref mut client_instance) = self.client_instance {
client_instance
.send_heartbeat_to_all_broker_with_lock()
.await;
}
Ok(())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Approved: Addition of subscribe method with encapsulated subscription logic.

The new subscribe method in DefaultMQPushConsumerImpl simplifies the subscription process by encapsulating the logic for building subscription data and sending heartbeats to brokers. Consider adding error handling for the subscription data building process to ensure robustness in case of invalid parameters.

Consider adding error handling for building subscription data:

if let Err(e) = subscription_data {
    return Err(MQClientError::MQClientErr(
        -1,
        format!("Error building subscription data: {}", e),
    ));
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
approved PR has approved auto merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature🚀] Support client consumer message-3
2 participants