Crates.io | tidb_pool |
lib.rs | tidb_pool |
version | 0.1.2 |
source | src |
created_at | 2024-09-16 22:50:33.857547 |
updated_at | 2024-09-16 23:41:51.405984 |
description | A Rust crate that creates a MySQL connection pool for TiDB using a TOML configuration file. |
homepage | https://github.com/your-username/tidb_pool |
repository | https://github.com/your-username/tidb_pool |
max_upload_size | |
id | 1376965 |
size | 29,307 |
tidb-pool
tidb-pool
is a Rust crate that simplifies the creation of a MySQL connection pool for TiDB using a configuration file in TOML format. It provides a flexible, efficient, and configurable way to connect to TiDB, leveraging connection pooling for optimal performance in both lazy and immediate connection modes. This crate is built on top of the popular sqlx
library for managing database connections in an asynchronous environment.
.toml
file.Add the following to your Cargo.toml
:
[dependencies]
tidb-pool = "0.1.0"
Then, include the crate in your project:
use tidb_pool::build_pool_from_config;
This crate is designed to work with a TOML configuration file that specifies the connection details and pool options. Here's how you can use it to create a connection pool for TiDB:
Create a file config.toml
with the necessary configuration settings for connecting to your TiDB database:
[tidb]
host = "127.0.0.1"
port = 4000
username = "root"
password = "secret"
databaseName = "test_db"
[tidb.pool_options]
maxConnections = 10
minConnections = 5
acquireTimeout = 30
idleTimeout = 300
maxLifetime = 3600
isLazy = true
# Optional: Uncomment to use SSL
# ssl_ca = "/path/to/ca-cert.pem"
Now, you can load the configuration and build the connection pool using tidb-pool
:
use tidb_pool::build_pool_from_config;
use sqlx::MySqlPool;
use std::fs;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
// Load the configuration from the TOML file
let config_content = fs::read_to_string("config.toml").expect("Failed to read config file");
let config: TiDBConfig = toml::from_str(&config_content).expect("Invalid TOML configuration");
// Build the connection pool
let pool: MySqlPool = build_pool_from_config(config).await?;
// Use the pool (e.g., execute queries)
Ok(())
}
Here are the available fields in the TOML configuration:
TiDB Section:
host
: Hostname or IP address of the TiDB server.port
: Port number for the TiDB server (defaults to 4000).username
: Username for authentication.password
: Password for authentication.databaseName
: Name of the TiDB database to connect to.ssl_ca
: (Optional) Path to the CA certificate for SSL verification.Pool Options Section:
maxConnections
: Maximum number of connections in the pool.minConnections
: Minimum number of connections maintained in the pool.acquireTimeout
: Timeout (in seconds) for acquiring a connection from the pool.idleTimeout
: Timeout (in seconds) for closing idle connections.maxLifetime
: Maximum lifetime (in seconds) for connections in the pool.isLazy
: Whether to lazily initialize connections (true
) or establish them immediately (false
).The isLazy
field in the configuration controls whether connections are established lazily or immediately:
isLazy = true
): Connections are only created when they are actually requested.isLazy = false
): Connections are established as soon as the pool is created.[tidb]
host = "127.0.0.1"
port = 4000
username = "admin"
password = "mypassword"
databaseName = "example_db"
[tidb.pool_options]
maxConnections = 15
minConnections = 5
acquireTimeout = 30
idleTimeout = 300
maxLifetime = 1800
isLazy = false
# Optional: Uncomment if you use SSL
# ssl_ca = "/path/to/ca-cert.pem"
use tidb_pool::{build_pool_from_config, TiDBConfig};
use std::fs;
use sqlx::MySqlPool;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
// Load the TiDB configuration from a TOML file
let config_content = fs::read_to_string("config.toml").expect("Failed to read config file");
let config: TiDBConfig = toml::from_str(&config_content).expect("Invalid TOML format");
// Build the connection pool using the configuration
let pool: MySqlPool = build_pool_from_config(config).await?;
// Now you can use `pool` to execute queries, transactions, etc.
Ok(())
}
The build_pool_from_config
function returns a Result<MySqlPool, sqlx::Error>
. If there is an error in creating the pool, it logs the issue and returns the error, allowing the caller to handle it gracefully.
This project is licensed under the MIT License.