tcvectordb 0.1.9

Rust SDK for Tencent Cloud VectorDB
Documentation
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...");
                                
                                // ๅฐ่ฏ•ๅŸบๆœฌ็š„HTTP่ฏทๆฑ‚
                                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())
}