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    enable_gzip: bool,
16}
17
18impl ConnectionManager {
19    pub async fn new(
20        pool_size: usize,
21        environment: &Environment,
22        domain: &str,
23        conn_options: &ConnectionOptions,
24    ) -> Result<Self, Error> {
25        // Support Private Service Connect (PSC) endpoints for Spanner.
26        //
27        // When `domain` is a full HTTPS URL (e.g. "https://spanner-nonprod.p.googleapis.com")
28        // we use it as both the TLS SNI hostname and the gRPC connection target, mirroring
29        // the Java SDK's `SpannerOptions.setHost("https://...")` behaviour.
30        //
31        // When `domain` is a plain hostname (default: "spanner.googleapis.com") the existing
32        // behaviour is preserved: the public AUDIENCE constant is used as the target.
33        let (sni_domain, audience): (String, &str) = if domain.starts_with("https://") {
34            let host = domain.trim_start_matches("https://").trim_end_matches('/');
35            (host.to_string(), domain)
36        } else {
37            (domain.to_string(), AUDIENCE)
38        };
39
40        Ok(ConnectionManager {
41            inner: GRPCConnectionManager::new(pool_size, sni_domain, audience, environment, conn_options).await?,
42            enable_gzip: false,
43        })
44    }
45
46    pub fn num(&self) -> usize {
47        self.inner.num()
48    }
49
50    pub(crate) fn with_gzip(mut self, enabled: bool) -> Self {
51        self.enable_gzip = enabled;
52        self
53    }
54
55    pub fn conn(&self) -> Client {
56        let conn = self.inner.conn();
57        Client::new(SpannerClient::new(conn)).with_gzip(self.enable_gzip)
58    }
59}