Files
pgcat/src/main.rs

118 lines
3.2 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-03 13:35:40 -08:00
extern crate tokio;
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-05 19:43:48 -08:00
let addr = "0.0.0.0:6432";
2022-02-05 10:02:13 -08:00
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(),
2022-02-06 11:13:12 -08:00
port: "5433".to_string(),
2022-02-05 10:02:13 -08:00
},
];
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-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-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);
}
};
});
}
}