google_cloud_gax/
conn.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;

use http::header::AUTHORIZATION;
use http::{HeaderValue, Request};
use tonic::body::BoxBody;
use tonic::transport::{Channel as TonicChannel, ClientTlsConfig, Endpoint};
use tonic::{Code, Status};
use tower::filter::{AsyncFilter, AsyncFilterLayer, AsyncPredicate};
use tower::util::Either;
use tower::{BoxError, ServiceBuilder};

use google_cloud_token::{TokenSource, TokenSourceProvider};

pub type Channel = Either<AsyncFilter<TonicChannel, AsyncAuthInterceptor>, TonicChannel>;

#[derive(Clone, Debug)]
pub struct AsyncAuthInterceptor {
    token_source: Arc<dyn TokenSource>,
}

impl AsyncAuthInterceptor {
    fn new(token_source: Arc<dyn TokenSource>) -> Self {
        Self { token_source }
    }
}

impl AsyncPredicate<Request<BoxBody>> for AsyncAuthInterceptor {
    type Future = Pin<Box<dyn Future<Output = Result<Self::Request, BoxError>> + Send>>;
    type Request = Request<BoxBody>;

    fn check(&mut self, request: Request<BoxBody>) -> Self::Future {
        let ts = self.token_source.clone();
        Box::pin(async move {
            let token = ts
                .token()
                .await
                .map_err(|e| Status::new(Code::Unauthenticated, format!("token error: {e:?}")))?;
            let token_header = HeaderValue::from_str(token.as_str())
                .map_err(|e| Status::new(Code::Unauthenticated, format!("token error: {e:?}")))?;
            let (mut parts, body) = request.into_parts();
            parts.headers.insert(AUTHORIZATION, token_header);
            Ok(Request::from_parts(parts, body))
        })
    }
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Auth(#[from] Box<dyn std::error::Error + Send + Sync>),

    #[error("tonic error : {0}")]
    TonicTransport(#[from] tonic::transport::Error),

    #[error("invalid emulator host: {0}")]
    InvalidEmulatorHOST(String),
}

#[derive(Debug)]
pub enum Environment {
    Emulator(String),
    GoogleCloud(Box<dyn TokenSourceProvider>),
}

#[derive(Debug)]
struct AtomicRing<T>
where
    T: Clone + Debug,
{
    index: AtomicUsize,
    values: Vec<T>,
}

impl<T> AtomicRing<T>
where
    T: Clone + Debug,
{
    fn next(&self) -> T {
        let current = self.index.fetch_add(1, Ordering::SeqCst);
        //clone() reuses http/2 connection
        self.values[current % self.values.len()].clone()
    }
}

#[derive(Debug, Clone, Default)]
pub struct ConnectionOptions {
    pub timeout: Option<Duration>,
    pub connect_timeout: Option<Duration>,
}

impl ConnectionOptions {
    fn apply(&self, mut endpoint: Endpoint) -> Endpoint {
        endpoint = match self.timeout {
            Some(t) => endpoint.timeout(t),
            None => endpoint,
        };
        endpoint = match self.connect_timeout {
            Some(t) => endpoint.connect_timeout(t),
            None => endpoint,
        };
        endpoint
    }
}

#[derive(Debug)]
pub struct ConnectionManager {
    inner: AtomicRing<Channel>,
}

impl<'a> ConnectionManager {
    pub async fn new(
        pool_size: usize,
        domain_name: impl Into<String>,
        audience: &'static str,
        environment: &Environment,
        conn_options: &'a ConnectionOptions,
    ) -> Result<Self, Error> {
        let conns = match environment {
            Environment::GoogleCloud(ts_provider) => {
                Self::create_connections(pool_size, domain_name, audience, ts_provider.as_ref(), conn_options).await?
            }
            Environment::Emulator(host) => Self::create_emulator_connections(host, conn_options).await?,
        };
        Ok(Self {
            inner: AtomicRing {
                index: AtomicUsize::new(0),
                values: conns,
            },
        })
    }

    async fn create_connections(
        pool_size: usize,
        domain_name: impl Into<String>,
        audience: &'static str,
        ts_provider: &dyn TokenSourceProvider,
        conn_options: &'a ConnectionOptions,
    ) -> Result<Vec<Channel>, Error> {
        let tls_config = ClientTlsConfig::new().with_webpki_roots().domain_name(domain_name);
        let mut conns = Vec::with_capacity(pool_size);

        let ts = ts_provider.token_source();

        for _i_ in 0..pool_size {
            let endpoint = TonicChannel::from_static(audience).tls_config(tls_config.clone())?;
            let endpoint = conn_options.apply(endpoint);

            let con = Self::connect(endpoint).await?;
            // use GCP token per call
            let auth_layer = Some(AsyncFilterLayer::new(AsyncAuthInterceptor::new(Arc::clone(&ts))));
            let auth_con = ServiceBuilder::new().option_layer(auth_layer).service(con);
            conns.push(auth_con);
        }
        Ok(conns)
    }

    async fn create_emulator_connections(
        host: &str,
        conn_options: &'a ConnectionOptions,
    ) -> Result<Vec<Channel>, Error> {
        let mut conns = Vec::with_capacity(1);
        let endpoint = TonicChannel::from_shared(format!("http://{host}").into_bytes())
            .map_err(|_| Error::InvalidEmulatorHOST(host.to_string()))?;
        let endpoint = conn_options.apply(endpoint);

        let con = Self::connect(endpoint).await?;
        conns.push(
            ServiceBuilder::new()
                .option_layer::<AsyncFilterLayer<AsyncAuthInterceptor>>(None)
                .service(con),
        );
        Ok(conns)
    }

    async fn connect(endpoint: Endpoint) -> Result<TonicChannel, tonic::transport::Error> {
        let channel = endpoint.connect().await?;
        Ok(channel)
    }

    pub fn num(&self) -> usize {
        self.inner.values.len()
    }

    pub fn conn(&self) -> Channel {
        self.inner.next()
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashSet;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use crate::conn::AtomicRing;

    #[test]
    fn test_atomic_ring() {
        let cm = AtomicRing::<&str> {
            index: AtomicUsize::new(usize::MAX - 1),
            values: vec!["a", "b", "c", "d"],
        };
        let mut values = HashSet::new();
        assert_eq!(usize::MAX - 1, cm.index.load(Ordering::SeqCst));
        assert!(values.insert(cm.next()));
        assert_eq!(usize::MAX, cm.index.load(Ordering::SeqCst));
        assert!(values.insert(cm.next()));
        assert_eq!(0, cm.index.load(Ordering::SeqCst));
        assert!(values.insert(cm.next()));
        assert_eq!(1, cm.index.load(Ordering::SeqCst));
        assert!(values.insert(cm.next()));
        assert_eq!(2, cm.index.load(Ordering::SeqCst));
        assert!(!values.insert(cm.next()));
        assert_eq!(3, cm.index.load(Ordering::SeqCst));
    }
}