Building a database connection pool
In this section, we will create a database connection pool. A database connection pool is a limited number of database connections. When our application needs a database connection, it will take the connection from the pool and place it back into the pool when the application no longer needs the connection. If there are no connections left in the pool, the application will wait until there is a connection available, as seen in the following diagram:
Figure 6.7 – Database connection pool with a limit of three connections
Before we refactor our database connection, we need to install the following dependency in our Cargo.toml
file:
lazy_static = "1.4.0"
We then define our imports with the src/database.rs
file with the following code:
use actix_web::dev::Payload; use actix_web::error::ErrorServiceUnavailable; use actix_web::{Error, FromRequest, HttpRequest}; use futures::future::{Ready, ok, err...