use tcvectordb::{VectorDBClient, enums::ReadConsistency};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("๐ Debug Connection - VectorDB Rust SDK");
let server_url = std::env::var("VECTORDB_URL")
.unwrap_or_else(|_| {
println!("โ ๏ธ VECTORDB_URL not set, using default: http://localhost:8100");
"http://localhost:8100".to_string()
});
let username = std::env::var("VECTORDB_USERNAME")
.unwrap_or_else(|_| {
println!("โ ๏ธ VECTORDB_USERNAME not set, using default: root");
"root".to_string()
});
let api_key = std::env::var("VECTORDB_API_KEY")
.unwrap_or_else(|_| {
eprintln!("โ Error: VECTORDB_API_KEY environment variable is required!");
eprintln!("Please set it with: export VECTORDB_API_KEY=your-api-key");
std::process::exit(1);
});
println!("๐ง Configuration:");
println!(" Server: {}", server_url);
println!(" Username: {}", username);
println!(" API Key: {}***", &api_key[..std::cmp::min(8, api_key.len())]);
let timeout_values = vec![10, 30, 60];
for timeout in timeout_values {
println!("\n๐ Trying with timeout: {} seconds", timeout);
match VectorDBClient::new(
&server_url,
&username,
&api_key,
ReadConsistency::EventualConsistency,
timeout,
) {
Ok(client) => {
println!("โ
Client created successfully");
match client.list_databases().await {
Ok(databases) => {
println!("๐ Connection successful with timeout {}s!", timeout);
println!("๐ Found {} database(s):", databases.len());
for (i, db) in databases.iter().enumerate() {
println!(" {}. {}", i + 1, db.name());
}
return Ok(());
}
Err(e) => {
println!("โ Connection failed with timeout {}s: {}", timeout, e);
if let Some(diagnosis) = e.diagnose_http_error() {
println!("๐ Diagnosis:\n{}", diagnosis);
}
if let tcvectordb::error::VectorDBError::HttpError(ref http_err) = e {
if http_err.is_timeout() {
println!(" โ This is a timeout error, trying longer timeout...");
continue;
} else if http_err.is_connect() {
println!(" โ This is a connection error, server might be unreachable");
break;
} else if http_err.is_request() {
println!(" โ This is a request error, checking connection details...");
println!("๐งช Testing basic HTTP connectivity...");
match test_basic_http(&server_url).await {
Ok(status) => println!(" โ
Basic HTTP test successful: {}", status),
Err(e) => println!(" โ Basic HTTP test failed: {}", e),
}
break;
}
}
}
}
}
Err(e) => {
println!("โ Failed to create client: {}", e);
break;
}
}
}
println!("\n๐ ๏ธ Troubleshooting suggestions:");
println!("1. Check if the server is running:");
println!(" curl -v {}/database/list", server_url);
println!("2. Try with different timeout values");
println!("3. Check network connectivity:");
println!(" ping sg-vdb-99wnoegp.sql.tencentcdb.com");
println!("4. Verify the server supports HTTP/1.1");
println!("5. Check if there are any proxy settings interfering");
Err("All connection attempts failed".into())
}
async fn test_basic_http(server_url: &str) -> Result<u16, Box<dyn std::error::Error>> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
.build()?;
let response = client
.get(format!("{}/database/list", server_url))
.send()
.await?;
Ok(response.status().as_u16())
}