9

Sqlx reported an error when I used the script to loop the request

"pool timed out while waiting for an open connection"

this pool is below

Pool { size: 5, num_idle: 5, is_closed: false, options: PoolOptions { max_connections: 5, min_connections: 0, connect_timeout: 30s, max_lifetime: Some(1800s), idle_timeout: Some(600s), test_before_acquire: true } }

Here's my code

rust main.rs

#[derive(Serialize)]
struct JsonResp {
    id: i32
}

#[derive(Clone)]
struct AppState {
    pub pool: MySqlPool
}

#[async_std::main]
async fn main() -> tide::Result<()> {

    let pool = MySqlPoolOptions::new()
        .max_connections(5)
        .connect("mysql://root:123456@localhost:3306/blog").await?;

    println!("{:?}", pool);

    let app_state = AppState { pool: pool };

    let mut app = tide::with_state(app_state);
    app.at("/test").get(test);
    app.listen("127.0.0.1:8080").await?;
    Ok(())
}


async fn test(req: Request<(AppState)>) -> tide::Result<tide::Body> {

    println!("order_shoes");
    let app_state = req.state();
    let pool = &app_state.pool;
    println!("{:?}", pool);

    let (id, color) = sqlx::query_as::<_, (i32, String)>("select id, color from tags where id = 1").fetch_one(&mut pool.acquire().await?).await?;
    println!("{}", color);

    Ok(tide::Body::from_json( &JsonResp { id: id})?)
}

test script

ruby_send.rb

require 'net/http'
require 'uri'

loop do
  url = URI.parse("http://localhost:8080/test")
  response = Net::HTTP.get(url)
  p response
end

start.shell

for i in {1..20}; do
 nohup ruby ruby_send.rb > send.log &;
done

Run the start.shell and There was no response to the request

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
周永建
  • 113
  • 6

0 Answers0