Skip to content

Commit

Permalink
feat: Support SYNC_SPANNER_EMULATOR_HOST (#1061)
Browse files Browse the repository at this point in the history
Closes #915
  • Loading branch information
ethowitz authored Apr 30, 2021
1 parent 21da763 commit 322603a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/db/spanner/manager/deadpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct SpannerSessionManager {
test_transactions: bool,
max_lifespan: Option<u32>,
max_idle: Option<u32>,
emulator_host: Option<String>,
}

impl fmt::Debug for SpannerSessionManager {
Expand Down Expand Up @@ -56,6 +57,7 @@ impl SpannerSessionManager {
test_transactions,
max_lifespan: settings.database_pool_connection_lifespan,
max_idle: settings.database_pool_connection_max_idle,
emulator_host: settings.spanner_emulator_host.clone(),
})
}
}
Expand All @@ -68,6 +70,7 @@ impl Manager<SpannerSession, DbError> for SpannerSessionManager {
self.metrics.clone(),
&self.database_name,
self.test_transactions,
self.emulator_host.clone(),
)
.await?;
Ok(session)
Expand Down
22 changes: 15 additions & 7 deletions src/db/spanner/manager/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,26 @@ pub async fn create_spanner_session(
mut metrics: Metrics,
database_name: &str,
use_test_transactions: bool,
emulator_host: Option<String>,
) -> Result<SpannerSession, DbError> {
// XXX: issue732: Could google_default_credentials (or
// ChannelBuilder::secure_connect) block?!
let chan = block(move || -> Result<grpcio::Channel, grpcio::Error> {
metrics.start_timer("storage.pool.grpc_auth", None);
// Requires
// GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
let creds = ChannelCredentials::google_default_credentials()?;
Ok(ChannelBuilder::new(env)
.max_send_message_len(100 << 20)
.max_receive_message_len(100 << 20)
.secure_connect(SPANNER_ADDRESS, creds))
if let Some(spanner_emulator_address) = emulator_host {
Ok(ChannelBuilder::new(env)
.max_send_message_len(100 << 20)
.max_receive_message_len(100 << 20)
.connect(&spanner_emulator_address))
} else {
// Requires
// GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
let creds = ChannelCredentials::google_default_credentials()?;
Ok(ChannelBuilder::new(env)
.max_send_message_len(100 << 20)
.max_receive_message_len(100 << 20)
.secure_connect(SPANNER_ADDRESS, creds))
}
})
.await
.map_err(|e| match e {
Expand Down
3 changes: 3 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub struct Settings {

pub enable_quota: bool,
pub enforce_quota: bool,

pub spanner_emulator_host: Option<String>,
}

impl Default for Settings {
Expand Down Expand Up @@ -109,6 +111,7 @@ impl Default for Settings {
human_logs: false,
enable_quota: false,
enforce_quota: false,
spanner_emulator_host: None,
}
}
}
Expand Down

0 comments on commit 322603a

Please sign in to comment.