Skip to content

Commit

Permalink
[GraphQL] Set Connection Timeout (MystenLabs#16199)
Browse files Browse the repository at this point in the history
## Description

Use the request timeout configuration to also bound the time a
connection to the DB is held open for, to prevent long-lived connections
from lingering while the underlying request has been cleaned up.

## Test Plan

```
sui-graphql-rpc$ cargo nextest run
sui-graphql-e2e-tests$ cargo nextest run --features pg_integration
```
  • Loading branch information
amnn authored Feb 12, 2024
1 parent 266311d commit 46dab3c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/sui-graphql-rpc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const MAX_TYPE_ARGUMENT_WIDTH: u32 = 32;
const MAX_TYPE_NODES: u32 = 256;
const MAX_MOVE_VALUE_DEPTH: u32 = 128;

const DEFAULT_REQUEST_TIMEOUT_MS: u64 = 40_000;
pub(crate) const DEFAULT_REQUEST_TIMEOUT_MS: u64 = 40_000;

const DEFAULT_IDE_TITLE: &str = "Sui GraphQL IDE";

Expand Down
12 changes: 9 additions & 3 deletions crates/sui-graphql-rpc/src/context_data/db_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
config::DEFAULT_SERVER_DB_POOL_SIZE,
config::{DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_SERVER_DB_POOL_SIZE},
error::Error,
types::{address::Address, sui_address::SuiAddress, validator::Validator},
};
use std::collections::BTreeMap;
use std::{collections::BTreeMap, time::Duration};
use sui_indexer::{
apis::GovernanceReadApiV2, indexer_reader::IndexerReader, PgConnectionPoolConfig,
};
Expand All @@ -30,15 +30,21 @@ impl PgManager {

/// Create a new underlying reader, which is used by this type as well as other data providers.
pub(crate) fn reader(db_url: impl Into<String>) -> Result<IndexerReader, Error> {
Self::reader_with_config(db_url, DEFAULT_SERVER_DB_POOL_SIZE)
Self::reader_with_config(
db_url,
DEFAULT_SERVER_DB_POOL_SIZE,
DEFAULT_REQUEST_TIMEOUT_MS,
)
}

pub(crate) fn reader_with_config(
db_url: impl Into<String>,
pool_size: u32,
timeout_ms: u64,
) -> Result<IndexerReader, Error> {
let mut config = PgConnectionPoolConfig::default();
config.set_pool_size(pool_size);
config.set_connection_timeout(Duration::from_millis(timeout_ms));
IndexerReader::new_with_config(db_url, config)
.map_err(|e| Error::Internal(format!("Failed to create reader: {e}")))
}
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-graphql-rpc/src/server/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ impl ServerBuilder {
let reader = PgManager::reader_with_config(
config.connection.db_url.clone(),
config.connection.db_pool_size,
// Bound each connection in a request with the overall request timeout, to bound DB
// utilisation (in the worst case we will use 2x the request timeout time in DB wall
// time).
config.service.limits.request_timeout_ms,
)
.map_err(|e| Error::Internal(format!("Failed to create pg connection pool: {}", e)))?;

Expand Down

0 comments on commit 46dab3c

Please sign in to comment.