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.
|
|
|
|
|
//
|
2022-02-03 17:06:19 -08:00
|
|
|
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-03 13:35:40 -08:00
|
|
|
extern crate tokio;
|
|
|
|
|
|
2022-02-03 17:06:19 -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;
|
2022-02-03 17:06:19 -08:00
|
|
|
mod server;
|
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-05 10:02:13 -08:00
|
|
|
let addr = "0.0.0.0:5433";
|
|
|
|
|
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-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-05 13:15:53 -08:00
|
|
|
// Replica pool.
|
2022-02-05 10:02:13 -08:00
|
|
|
let addresses = vec![
|
|
|
|
|
Address {
|
|
|
|
|
host: "127.0.0.1".to_string(),
|
|
|
|
|
port: "5432".to_string(),
|
|
|
|
|
},
|
|
|
|
|
Address {
|
|
|
|
|
host: "localhost".to_string(),
|
|
|
|
|
port: "5432".to_string(),
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let user = User {
|
|
|
|
|
name: "lev".to_string(),
|
|
|
|
|
password: "lev".to_string(),
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-05 13:15:53 -08:00
|
|
|
let database = "lev";
|
|
|
|
|
|
2022-02-05 18:20:53 -08:00
|
|
|
let pool = ConnectionPool::new(addresses, user, database, client_server_map.clone()).await;
|
2022-02-05 13:15:53 -08:00
|
|
|
// We are round-robining, so ideally the replicas will be equally loaded.
|
|
|
|
|
// Therefore, we are allocating number of replicas * pool size of connections.
|
|
|
|
|
// However, if a replica dies, the remaining replicas will share the burden,
|
|
|
|
|
// also equally.
|
|
|
|
|
//
|
|
|
|
|
// Note that failover in this case could bring down the remaining replicas, so
|
|
|
|
|
// in certain situations, e.g. when replicas are running hot already, failover
|
|
|
|
|
// is not at all desirable!!
|
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-03 15:17:04 -08:00
|
|
|
println!(">> Client {:?} connected.", addr);
|
2022-02-03 17:06:19 -08:00
|
|
|
|
2022-02-04 16:01:35 -08:00
|
|
|
match client::Client::startup(socket, client_server_map).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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|