rocketmq_remoting/remoting.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use std::sync::Arc;
18
19use rocketmq_rust::WeakArcMut;
20
21use crate::base::response_future::ResponseFuture;
22use crate::protocol::remoting_command::RemotingCommand;
23use crate::runtime::RPCHook;
24
25/// `RemotingService` trait defines the core functionalities for a remoting service.
26///
27/// This trait outlines the essential operations for starting, shutting down, and managing RPC hooks
28/// within a remoting service. Implementors of this trait are expected to provide concrete
29/// implementations for these operations, facilitating the management of remote procedure calls.
30///
31/// # Requirements
32/// Implementors must be `Send` to ensure thread safety, allowing instances to be transferred
33/// across thread boundaries.
34#[allow(async_fn_in_trait)]
35pub trait RemotingService: Send {
36 /// Asynchronously starts the remoting service.
37 ///
38 /// This function should initialize and start the service, making it ready to handle incoming
39 /// or outgoing remote procedure calls. The exact implementation details, such as opening
40 /// network connections or preparing internal state, are left to the implementor.
41 async fn start(&self, this: WeakArcMut<Self>);
42
43 /// Shuts down the remoting service.
44 ///
45 /// This function is responsible for gracefully shutting down the service. It should ensure
46 /// that all resources are released, and any ongoing operations are completed or aborted
47 /// appropriately before the service stops.
48 fn shutdown(&mut self);
49
50 /// Registers an RPC hook.
51 ///
52 /// This function allows for the registration of an RPC hook, which can be used to intercept
53 /// and modify the behavior of remote procedure calls. Hooks can be used for logging,
54 /// monitoring, or modifying the requests or responses of RPCs.
55 ///
56 /// # Arguments
57 /// * `hook` - An implementation of the `RPCHook` trait that will be registered.
58 fn register_rpc_hook(&mut self, hook: Arc<Box<dyn RPCHook>>);
59
60 /// Clears all registered RPC hooks.
61 ///
62 /// This function removes all previously registered RPC hooks, returning the service to its
63 /// default state without any hooks. This can be useful for cleanup or when changing the
64 /// configuration of the service.
65 fn clear_rpc_hook(&mut self);
66}
67pub trait InvokeCallback {
68 fn operation_complete(&self, response_future: ResponseFuture);
69 fn operation_succeed(&self, response: RemotingCommand);
70 fn operation_fail(&self, throwable: Box<dyn std::error::Error>);
71}