actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

Connecting to the database from an external network causes a spike in connections

Open tengfei-xy opened this issue 1 year ago • 1 comments

Your issue may already be reported! Please search on the Actix Web issue tracker before creating one.

Expected Behavior

The normal number of connections that should be maintained to mysql from any network ​

Current Behavior

Using the public IP from the mysql host to connect to this mysql, there are 40 connections, but from other networks, the number of connections will increase sharply. ​

Possible Solution

Is it related to the mysql library? I am using https://docs.rs/mysql/latest/mysql/

Steps to Reproduce (for bugs)

  1. Run this program on your home network
  2. Remotely access mysql and run show processlist. It is found that the connection surges.
  3. Close the program. The abnormal number of connections has been closed. Start the program in the server running mysql.
  4. Remotely go to mysql and run show processlist and find that the connection remains at 40
use actix_web::{web, web::Data, App, HttpRequest, HttpResponse, HttpServer, Responder};
extern crate mysql;
use mysql::{prelude::Queryable, *};
fn init_mysql() -> Pool {
    let username = "xxx";
    let password = "xxx";
    let host = "xxx";
    let database_name = "xxx";
    Pool::new(
        format!(
            "mysql://{}:{}@{}/{}",
            username, password, host, database_name
        )
        .as_str(),
    )
    .unwrap()
}
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .app_data(Data::new(MysqlData {
                mysql: Data::new(init_mysql()),
            }))
            .route("/api/v1/login", web::post().to(login))
            .route("/api/v1/sigup", web::post().to(sigup))
    })
    // .bind("127.0.0.1:16825")? // 监听所有IP和端口8000
    .bind("0.0.0.0:16825")? // 监听所有IP和端口8000
    .run()
    .await
}

Context

I started the web server. For requests, I want to connect to mysql through actix-web and process the requests. If this isn't best practice, what should it be?

Your Environment

  • Rust Version (I.e, output of rustc -V):rustc 1.74.1 (a28077b28 2023-12-04)
  • Actix Web Version:4.5.1
  • mysql: 24.0.0

tengfei-xy avatar Feb 16 '24 17:02 tengfei-xy

I have tested it. In the environment where the error occurred, I set up mysql. When the database and program are running on the same host, there will be no abnormal number of connections.

tengfei-xy avatar Feb 16 '24 19:02 tengfei-xy

This doesn't relate to Actix Web. Please ask the mysql crate maintainers about its behavior when it the Pool is dropped.

robjtede avatar Jun 16 '24 18:06 robjtede