Skip to main content

couchbase_core/memdx/
dispatcher.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * 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 *
17 */
18
19use std::sync::Arc;
20
21use crate::memdx::client::ResponseContext;
22use crate::memdx::connection::ConnectionType;
23use crate::memdx::error::Result;
24use crate::memdx::packet::{RequestPacket, ResponsePacket};
25use crate::memdx::pendingop::ClientPendingOp;
26use crate::orphan_reporter::OrphanContext;
27use async_trait::async_trait;
28use futures::future::BoxFuture;
29use tokio::sync::oneshot;
30
31pub type UnsolicitedPacketHandler =
32    Arc<dyn Fn(ResponsePacket) -> BoxFuture<'static, ()> + Send + Sync>;
33pub type OrphanResponseHandler = Arc<dyn Fn(ResponsePacket, OrphanContext) + Send + Sync>;
34pub type OnReadLoopCloseHandler = oneshot::Sender<()>;
35
36pub struct DispatcherOptions {
37    pub unsolicited_packet_handler: UnsolicitedPacketHandler,
38    pub orphan_handler: Option<OrphanResponseHandler>,
39    pub on_read_close_tx: OnReadLoopCloseHandler,
40    pub disable_decompression: bool,
41    pub id: String,
42}
43
44#[async_trait]
45pub trait Dispatcher: Send + Sync {
46    fn new(conn: ConnectionType, opts: DispatcherOptions) -> Self;
47    async fn dispatch<'a>(
48        &self,
49        packet: RequestPacket<'a>,
50        is_persistent: bool,
51        response_context: Option<ResponseContext>,
52    ) -> Result<ClientPendingOp>;
53    async fn close(&self) -> Result<()>;
54}