Skip to main content

gcloud_spanner/apiv1/
conn_pool.rs

1use google_cloud_gax::conn::{ConnectionManager as GRPCConnectionManager, ConnectionOptions, Environment, Error};
2use google_cloud_googleapis::spanner::v1::spanner_client::SpannerClient;
3
4use crate::apiv1::spanner_client::Client;
5
6pub const AUDIENCE: &str = "https://spanner.googleapis.com/";
7pub const SPANNER: &str = "spanner.googleapis.com";
8pub const SCOPES: [&str; 2] = [
9    "https://www.googleapis.com/auth/cloud-platform",
10    "https://www.googleapis.com/auth/spanner.data",
11];
12
13pub struct ConnectionManager {
14    inner: GRPCConnectionManager,
15}
16
17impl ConnectionManager {
18    pub async fn new(
19        pool_size: usize,
20        environment: &Environment,
21        domain: &str,
22        conn_options: &ConnectionOptions,
23    ) -> Result<Self, Error> {
24        // Support Private Service Connect (PSC) endpoints for Spanner.
25        //
26        // When `domain` is a full HTTPS URL (e.g. "https://spanner-nonprod.p.googleapis.com")
27        // we use it as both the TLS SNI hostname and the gRPC connection target, mirroring
28        // the Java SDK's `SpannerOptions.setHost("https://...")` behaviour.
29        //
30        // When `domain` is a plain hostname (default: "spanner.googleapis.com") the existing
31        // behaviour is preserved: the public AUDIENCE constant is used as the target.
32        let (sni_domain, audience): (String, &str) = if domain.starts_with("https://") {
33            let host = domain.trim_start_matches("https://").trim_end_matches('/');
34            (host.to_string(), domain)
35        } else {
36            (domain.to_string(), AUDIENCE)
37        };
38
39        Ok(ConnectionManager {
40            inner: GRPCConnectionManager::new(pool_size, sni_domain, audience, environment, conn_options).await?,
41        })
42    }
43
44    pub fn num(&self) -> usize {
45        self.inner.num()
46    }
47
48    pub fn conn(&self) -> Client {
49        let conn = self.inner.conn();
50        Client::new(SpannerClient::new(conn))
51    }
52}