Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tengfei-xy opened this issue Feb 16, 2024 · 1 comment

Comments

@tengfei-xy
Copy link

tengfei-xy commented Feb 16, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant