Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release the read lock while creating connections inrefresh_connections #191

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 47 additions & 43 deletions redis/src/cluster_async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,51 +1303,55 @@ where
check_existing_conn: bool,
) {
info!("Started refreshing connections to {:?}", addresses);
let connections_container = inner.conn_lock.read().await;
let cluster_params = &inner.cluster_params;
let subscriptions_by_address = &inner.subscriptions_by_address;
let glide_connection_optons = &inner.glide_connection_options;
let mut tasks = FuturesUnordered::new();
let inner = inner.clone();

stream::iter(addresses.into_iter())
.fold(
&*connections_container,
|connections_container, address| async move {
let node_option = if check_existing_conn {
connections_container.remove_node(&address)
} else {
None
};
for address in addresses.into_iter() {
let inner = inner.clone();

// override subscriptions for this connection
let mut cluster_params = cluster_params.clone();
let subs_guard = subscriptions_by_address.read().await;
cluster_params.pubsub_subscriptions = subs_guard.get(&address).cloned();
drop(subs_guard);
let node = get_or_create_conn(
&address,
node_option,
&cluster_params,
conn_type,
glide_connection_optons.clone(),
)
.await;
match node {
Ok(node) => {
connections_container
.replace_or_add_connection_for_address(address, node);
}
Err(err) => {
warn!(
"Failed to refresh connection for node {}. Error: `{:?}`",
address, err
);
}
}
connections_container
},
)
.await;
info!("refresh connections completed");
tasks.push(async move {
let node_option = if check_existing_conn {
let connections_container = inner.conn_lock.read().await;
connections_container.remove_node(&address)
} else {
None
};

// Override subscriptions for this connection
let mut cluster_params = inner.cluster_params.clone();
let subs_guard = inner.subscriptions_by_address.read().await;
cluster_params.pubsub_subscriptions = subs_guard.get(&address).cloned();
drop(subs_guard);

let node = get_or_create_conn(
&address,
node_option,
&cluster_params,
conn_type,
inner.glide_connection_options.clone(),
)
.await;

(address, node)
});
}

// Poll connection tasks as soon as each one finishes
while let Some(result) = tasks.next().await {
match result {
(address, Ok(node)) => {
let connections_container = inner.conn_lock.read().await;
connections_container.replace_or_add_connection_for_address(address, node);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should expose API for inner for this function (replace_or_add_connection_for_address) and avoid exposing the lock here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}
(address, Err(err)) => {
warn!(
"Failed to refresh connection for node {}. Error: `{:?}`",
address, err
);
}
}
}
debug!("refresh connections completed");
}

async fn aggregate_results(
Expand Down