Skip to content

Commit

Permalink
Creating a Graph doesn't have to be async
Browse files Browse the repository at this point in the history
The first connection is done when the first query is run, no need for an async graph creation.

BREAKING CHANGE: The Graph::new and Graph::connect methods are no longer `async`, any `.await` after calling them must be removed.
  • Loading branch information
knutwalker committed Feb 12, 2025
1 parent 7561bbe commit 115bbc2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
1 change: 0 additions & 1 deletion lib/examples/concurrent_writes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ async fn main() {
.build()
.unwrap(),
)
.await
.unwrap();

stream::iter(1..=1337)
Expand Down
18 changes: 9 additions & 9 deletions lib/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Graph {
/// Connects to the database with configurations provided.
///
/// You can build a config using [`ConfigBuilder::default()`].
pub async fn connect(config: Config) -> Result<Self> {
pub fn connect(config: Config) -> Result<Self> {
#[cfg(feature = "unstable-bolt-protocol-impl-v2")]
{
let info = ConnectionInfo::new(
Expand All @@ -77,16 +77,16 @@ impl Graph {
)?;
if matches!(info.routing, Routing::Yes(_)) {
debug!("Routing enabled, creating a routed connection manager");
let pool = Routed(
RoutedConnectionManager::new(&config, Box::new(ClusterRoutingTableProvider))
.await?,
);
let pool = Routed(RoutedConnectionManager::new(
&config,
Box::new(ClusterRoutingTableProvider),
)?);
Ok(Graph {
config: config.into_live_config(),
pool,
})
} else {
let pool = Direct(create_pool(&config).await?);
let pool = Direct(create_pool(&config)?);
Ok(Graph {
config: config.into_live_config(),
pool,
Expand All @@ -95,7 +95,7 @@ impl Graph {
}
#[cfg(not(feature = "unstable-bolt-protocol-impl-v2"))]
{
let pool = Direct(create_pool(&config).await?);
let pool = Direct(create_pool(&config)?);
Ok(Graph {
config: config.into_live_config(),
pool,
Expand All @@ -104,7 +104,7 @@ impl Graph {
}

/// Connects to the database with default configurations
pub async fn new(
pub fn new(
uri: impl Into<String>,
user: impl Into<String>,
password: impl Into<String>,
Expand All @@ -114,7 +114,7 @@ impl Graph {
.user(user)
.password(password)
.build()?;
Self::connect(config).await
Self::connect(config)
}

/// Starts a new transaction on the configured database.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Manager for ConnectionManager {
}
}

pub async fn create_pool(config: &Config) -> Result<ConnectionPool> {
pub fn create_pool(config: &Config) -> Result<ConnectionPool> {
let mgr = ConnectionManager::new(
&config.uri,
&config.user,
Expand Down
5 changes: 2 additions & 3 deletions lib/src/routing/connection_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ async fn refresh_routing_table(
create_pool(&Config {
uri,
..config.clone()
})
.await?,
})?,
);
}
registry.connections.retain(|k, _| servers.contains(k));
Expand All @@ -103,7 +102,7 @@ async fn refresh_routing_table(
Ok(routing_table.ttl)
}

pub(crate) async fn start_background_updater(
pub(crate) fn start_background_updater(
config: &Config,
registry: Arc<ConnectionRegistry>,
provider: Arc<Box<dyn RoutingTableProvider>>,
Expand Down
7 changes: 2 additions & 5 deletions lib/src/routing/routed_connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ pub struct RoutedConnectionManager {
}

impl RoutedConnectionManager {
pub async fn new(
config: &Config,
provider: Box<dyn RoutingTableProvider>,
) -> Result<Self, Error> {
pub fn new(config: &Config, provider: Box<dyn RoutingTableProvider>) -> Result<Self, Error> {
let backoff = Arc::new(
ExponentialBackoffBuilder::new()
.with_initial_interval(Duration::from_millis(1))
Expand All @@ -40,7 +37,7 @@ impl RoutedConnectionManager {

let connection_registry = Arc::new(ConnectionRegistry::default());
let channel =
start_background_updater(config, connection_registry.clone(), provider.into()).await;
start_background_updater(config, connection_registry.clone(), provider.into());
Ok(RoutedConnectionManager {
load_balancing_strategy: Arc::new(RoundRobinStrategy::default()),
bookmarks: Arc::new(Mutex::new(vec![])),
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl Neo4jContainer {
.build()
.unwrap();

Graph::connect(config).await.unwrap()
Graph::connect(config).unwrap()
}
}

Expand Down

0 comments on commit 115bbc2

Please sign in to comment.