Files
pgcat/src/main.rs

124 lines
3.6 KiB
Rust
Raw Normal View History

2022-02-05 13:52:16 -08:00
//
// Copyright 2022 Lev Kokotov <lev@levthe.dev>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
extern crate async_trait;
extern crate bb8;
2022-02-03 13:35:40 -08:00
extern crate bytes;
2022-02-03 15:17:04 -08:00
extern crate md5;
2022-02-08 09:25:59 -08:00
extern crate serde;
extern crate serde_derive;
2022-02-03 13:35:40 -08:00
extern crate tokio;
2022-02-08 09:25:59 -08:00
extern crate toml;
2022-02-03 13:35:40 -08:00
use tokio::net::TcpListener;
2022-02-03 13:35:40 -08:00
2022-02-04 09:28:52 -08:00
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
2022-02-03 15:17:04 -08:00
mod client;
2022-02-05 10:02:13 -08:00
mod config;
2022-02-03 13:35:40 -08:00
mod errors;
mod messages;
2022-02-03 16:25:05 -08:00
mod pool;
mod server;
2022-02-05 19:43:48 -08:00
mod sharding;
2022-02-03 13:35:40 -08:00
2022-02-05 10:02:13 -08:00
// Support for query cancellation: this maps our process_ids and
// secret keys to the backend's.
use config::{Address, User};
2022-02-05 18:20:53 -08:00
use pool::{ClientServerMap, ConnectionPool};
2022-02-05 13:15:53 -08:00
/// Main!
2022-02-03 13:35:40 -08:00
#[tokio::main]
async fn main() {
2022-02-05 10:02:13 -08:00
println!("> Welcome to PgCat! Meow.");
2022-02-03 13:35:40 -08:00
2022-02-08 09:25:59 -08:00
let config = match config::parse("pgcat.toml").await {
Ok(config) => config,
Err(err) => {
return;
}
};
let addr = format!("{}:{}", config.general.host, config.general.port);
let listener = match TcpListener::bind(&addr).await {
2022-02-03 13:35:40 -08:00
Ok(sock) => sock,
Err(err) => {
println!("> Error: {:?}", err);
return;
}
};
2022-02-05 10:02:13 -08:00
println!("> Running on {}", addr);
2022-02-08 09:25:59 -08:00
// Tracks which client is connected to which server for query cancellation.
2022-02-04 16:01:35 -08:00
let client_server_map: ClientServerMap = Arc::new(Mutex::new(HashMap::new()));
2022-02-05 10:02:13 -08:00
2022-02-08 09:25:59 -08:00
println!("> Pool size: {}", config.general.pool_size);
println!("> Pool mode: {}", config.general.pool_mode);
println!("> Ban time: {}s", config.general.ban_time);
println!(
"> Healthcheck timeout: {}ms",
config.general.healthcheck_timeout
);
2022-02-05 10:02:13 -08:00
2022-02-08 09:25:59 -08:00
let pool = ConnectionPool::from_config(config.clone(), client_server_map.clone()).await;
let transaction_mode = config.general.pool_mode == "transaction";
2022-02-05 13:15:53 -08:00
2022-02-08 09:25:59 -08:00
println!("> Waiting for clients...");
2022-02-03 16:25:05 -08:00
2022-02-03 13:35:40 -08:00
loop {
2022-02-05 13:15:53 -08:00
let pool = pool.clone();
2022-02-04 16:01:35 -08:00
let client_server_map = client_server_map.clone();
2022-02-03 16:25:05 -08:00
2022-02-03 13:54:07 -08:00
let (socket, addr) = match listener.accept().await {
Ok((socket, addr)) => (socket, addr),
2022-02-03 13:35:40 -08:00
Err(err) => {
println!("> Listener: {:?}", err);
continue;
}
};
// Client goes to another thread, bye.
tokio::task::spawn(async move {
2022-02-08 09:25:59 -08:00
println!(
">> Client {:?} connected, transaction pooling: {}",
addr, transaction_mode
);
2022-02-08 09:25:59 -08:00
match client::Client::startup(socket, client_server_map, transaction_mode).await {
2022-02-03 13:54:07 -08:00
Ok(mut client) => {
2022-02-03 15:17:04 -08:00
println!(">> Client {:?} authenticated successfully!", addr);
2022-02-05 13:15:53 -08:00
match client.handle(pool).await {
2022-02-03 15:17:04 -08:00
Ok(()) => {
println!(">> Client {:?} disconnected.", addr);
}
Err(err) => {
println!(">> Client disconnected with error: {:?}", err);
2022-02-04 16:01:35 -08:00
client.release();
2022-02-03 15:17:04 -08:00
}
}
}
2022-02-03 13:35:40 -08:00
Err(err) => {
println!(">> Error: {:?}", err);
}
};
});
}
}