apache_dubbo/triple/transport/connector/
mod.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 */
17
18pub mod http_connector;
19
20use hyper::Uri;
21use tower::make::MakeConnection;
22use tower_service::Service;
23
24use super::io::BoxIO;
25
26pub struct Connector<C> {
27    inner: C,
28}
29
30impl<C> Connector<C> {
31    pub fn new(inner: C) -> Connector<C> {
32        Self { inner }
33    }
34}
35
36impl<C> Service<Uri> for Connector<C>
37where
38    C: MakeConnection<Uri>,
39    C::Connection: Unpin + Send + 'static,
40    C::Future: Send + 'static,
41    crate::Error: From<C::Error> + Send + 'static,
42{
43    type Response = BoxIO;
44
45    type Error = crate::Error;
46
47    type Future = crate::BoxFuture<Self::Response, Self::Error>;
48
49    fn poll_ready(
50        &mut self,
51        cx: &mut std::task::Context<'_>,
52    ) -> std::task::Poll<Result<(), Self::Error>> {
53        MakeConnection::poll_ready(&mut self.inner, cx).map_err(Into::into)
54    }
55
56    fn call(&mut self, uri: Uri) -> Self::Future {
57        let conn = self.inner.make_connection(uri);
58
59        Box::pin(async move {
60            let io = conn.await?;
61            Ok(BoxIO::new(io))
62        })
63    }
64}