feat(connection): add connection attempt cancellation
This commit is contained in:
16 files changed
+681
-51
No files matched your search
@@ -13,12 +13,14 @@ use crate::state::WebState;
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ConnectRequest {
|
||||
pub config: ConnectionConfig,
|
||||
pub client_attempt: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DisconnectRequest {
|
||||
pub connection_id: String,
|
||||
pub client_attempt: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -67,7 +69,7 @@ pub async fn connect_db(
|
||||
let config = body.config;
|
||||
let app = &state.app;
|
||||
let connection_id = config.id.clone();
|
||||
let attempt = app.begin_connection_attempt(&connection_id).await;
|
||||
let attempt = app.begin_connection_attempt_with_client_attempt(&connection_id, body.client_attempt).await;
|
||||
|
||||
app.remove_connection_pools_detached(&connection_id).await;
|
||||
app.reset_connection_transport_for_config(&connection_id, &config).await;
|
||||
@@ -102,7 +104,15 @@ pub async fn disconnect_db(
|
||||
) -> Result<Json<()>, AppError> {
|
||||
let app = &state.app;
|
||||
|
||||
app.supersede_connection_attempt(&body.connection_id).await;
|
||||
let should_disconnect = if let Some(client_attempt) = body.client_attempt {
|
||||
app.supersede_connection_attempt_if_client_attempt(&body.connection_id, client_attempt).await
|
||||
} else {
|
||||
app.supersede_connection_attempt(&body.connection_id).await;
|
||||
true
|
||||
};
|
||||
if !should_disconnect {
|
||||
return Ok(Json(()));
|
||||
}
|
||||
app.remove_connection_pools_detached(&body.connection_id).await;
|
||||
app.nacos_registry.drop_connection(&body.connection_id).await;
|
||||
#[cfg(feature = "mq-admin")]
|
||||
@@ -419,7 +429,9 @@ mod tests {
|
||||
let first = state.app.mq_registry.get_or_build(&initial).await.unwrap();
|
||||
|
||||
let updated = mq_config("mq-conn", &spawn_pulsar_clusters_server().await);
|
||||
let result = connect_db(State(state.clone()), Json(ConnectRequest { config: updated.clone() })).await;
|
||||
let result =
|
||||
connect_db(State(state.clone()), Json(ConnectRequest { config: updated.clone(), client_attempt: None }))
|
||||
.await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
let second = state.app.mq_registry.get_or_build(&updated).await.unwrap();
|
||||
@@ -519,8 +531,11 @@ mod tests {
|
||||
connections.insert("conn2".to_string(), PoolKind::Sqlite(conn2_pool));
|
||||
}
|
||||
|
||||
let result =
|
||||
disconnect_db(State(state.clone()), Json(DisconnectRequest { connection_id: "conn".to_string() })).await;
|
||||
let result = disconnect_db(
|
||||
State(state.clone()),
|
||||
Json(DisconnectRequest { connection_id: "conn".to_string(), client_attempt: None }),
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
let connections = state.app.connections.read().await;
|
||||
@@ -530,6 +545,39 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn disconnect_db_ignores_stale_client_attempt_cancel() {
|
||||
let (state, dir) = test_web_state().await;
|
||||
let conn_path = dir.join("conn.db");
|
||||
std::fs::File::create(&conn_path).unwrap();
|
||||
let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap();
|
||||
state.app.begin_connection_attempt_with_client_attempt("conn", Some(1)).await;
|
||||
let current_attempt = state.app.begin_connection_attempt_with_client_attempt("conn", Some(2)).await;
|
||||
state.app.connections.write().await.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
|
||||
|
||||
let result = disconnect_db(
|
||||
State(state.clone()),
|
||||
Json(DisconnectRequest { connection_id: "conn".to_string(), client_attempt: Some(1) }),
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
assert!(state.app.connections.read().await.contains_key("conn"));
|
||||
assert!(state.app.ensure_current_connection_attempt("conn", Some(current_attempt)).await.is_ok());
|
||||
|
||||
let result = disconnect_db(
|
||||
State(state.clone()),
|
||||
Json(DisconnectRequest { connection_id: "conn".to_string(), client_attempt: Some(2) }),
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
assert!(!state.app.connections.read().await.contains_key("conn"));
|
||||
assert!(state.app.ensure_current_connection_attempt("conn", Some(current_attempt)).await.is_err());
|
||||
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn disconnect_db_keeps_connection_config_for_reconnect() {
|
||||
let (state, dir) = test_web_state().await;
|
||||
@@ -546,8 +594,11 @@ mod tests {
|
||||
configs.insert("conn".to_string(), sqlite_config("conn", &conn_path.to_string_lossy()));
|
||||
}
|
||||
|
||||
let result =
|
||||
disconnect_db(State(state.clone()), Json(DisconnectRequest { connection_id: "conn".to_string() })).await;
|
||||
let result = disconnect_db(
|
||||
State(state.clone()),
|
||||
Json(DisconnectRequest { connection_id: "conn".to_string(), client_attempt: None }),
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
let configs = state.app.configs.read().await;
|
||||
@@ -565,8 +616,11 @@ mod tests {
|
||||
state.app.connections.write().await.insert(config.id.clone(), PoolKind::MessageQueue);
|
||||
let first = state.app.mq_registry.get_or_build(&config).await.unwrap();
|
||||
|
||||
let result =
|
||||
disconnect_db(State(state.clone()), Json(DisconnectRequest { connection_id: config.id.clone() })).await;
|
||||
let result = disconnect_db(
|
||||
State(state.clone()),
|
||||
Json(DisconnectRequest { connection_id: config.id.clone(), client_attempt: None }),
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
assert!(!state.app.connections.read().await.contains_key(&config.id));
|
||||
@@ -588,8 +642,11 @@ mod tests {
|
||||
configs.insert(draft_id.to_string(), sqlite_config(draft_id, &conn_path.to_string_lossy()));
|
||||
}
|
||||
|
||||
let result =
|
||||
disconnect_db(State(state.clone()), Json(DisconnectRequest { connection_id: draft_id.to_string() })).await;
|
||||
let result = disconnect_db(
|
||||
State(state.clone()),
|
||||
Json(DisconnectRequest { connection_id: draft_id.to_string(), client_attempt: None }),
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
let configs = state.app.configs.read().await;
|
||||
|
||||
Reference in new issue
Block a user