Skip to main content

rocketmq_client_rust/hook/
check_forbidden_hook.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::hook::check_forbidden_context::CheckForbiddenContext;
16
17/// Hook for checking forbidden message sending operations.
18///
19/// This trait allows custom validation logic to be executed before messages are sent.
20/// Implementations can inspect the message context and reject operations that violate
21/// business rules or security policies.
22///
23/// All implementations must be `Send + Sync` to support concurrent message sending
24/// in multi-threaded producer environments.
25///
26/// # Examples
27///
28/// ```ignore
29/// use rocketmq_client::CheckForbiddenContext;
30/// use rocketmq_client::CheckForbiddenHook;
31/// use crate::ClientResult;
32///
33/// struct RegionValidator;
34///
35/// impl CheckForbiddenHook for RegionValidator {
36///     fn hook_name(&self) -> &'static str {
37///         "RegionValidator"
38///     }
39///
40///     fn check_forbidden(&self, context: &CheckForbiddenContext) -> ClientResult<()> {
41///         if let Some(broker_addr) = &context.broker_addr {
42///             if broker_addr.contains("forbidden-region") {
43///                 return Err("Cannot send to forbidden region".into());
44///             }
45///         }
46///         Ok(())
47///     }
48/// }
49/// ```
50pub trait CheckForbiddenHook: Send + Sync {
51    /// Returns the unique name of this hook.
52    ///
53    /// The name is used for logging and diagnostic purposes.
54    fn hook_name(&self) -> &'static str;
55
56    /// Validates whether a message send operation should be allowed.
57    ///
58    /// # Errors
59    ///
60    /// Returns an error if the operation should be rejected based on the context.
61    fn check_forbidden(&self, context: &CheckForbiddenContext<'_>) -> crate::ClientResult<()>;
62}