some optimization using rustc-hash, parking_lot and tikv-jemallocator

This commit is contained in:
Jun Kurihara 2022-06-28 16:55:07 -04:00
commit 3e850e8410
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
5 changed files with 27 additions and 25 deletions

View file

@ -1,13 +1,14 @@
use crate::log::*;
use parking_lot::Mutex;
use rand::Rng;
use rustc_hash::FxHashMap as HashMap;
use std::{
collections::HashMap,
fs::File,
io::{self, BufReader, Cursor, Read},
path::PathBuf,
sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex,
Arc,
},
};
use tokio_rustls::rustls::{Certificate, PrivateKey, ServerConfig};
@ -88,11 +89,9 @@ impl Upstream {
impl Backend {
pub fn get_tls_server_config(&self) -> Option<ServerConfig> {
let lock = self.server_config.lock();
if let Ok(opt) = lock {
let opt_clone = opt.clone();
if let Some(sc) = opt_clone {
return Some(sc);
}
let opt_clone = lock.clone();
if let Some(sc) = opt_clone {
return Some(sc);
}
None
}
@ -183,11 +182,8 @@ impl Backend {
})?;
server_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
if let Ok(mut config_store) = self.server_config.lock() {
*config_store = Some(server_config);
} else {
error!("Some thing wrong to write into mutex")
}
let mut config_store = self.server_config.lock();
*config_store = Some(server_config);
// server_config;
Ok(())

View file

@ -1,8 +1,9 @@
use super::toml::{ConfigToml, ReverseProxyOption};
use crate::{backend::*, constants::*, error::*, globals::*, log::*};
use clap::Arg;
use parking_lot::Mutex;
use rustc_hash::FxHashMap as HashMap;
use std::net::SocketAddr;
use std::{collections::HashMap, sync::Mutex};
// #[cfg(feature = "tls")]
use std::path::PathBuf;
@ -147,7 +148,7 @@ pub fn parse_opts(globals: &mut Globals, backends: &mut Backends) -> Result<()>
}
fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> Result<ReverseProxy> {
let mut upstream: HashMap<String, Upstream> = HashMap::new();
let mut upstream: HashMap<String, Upstream> = HashMap::default();
let mut default_upstream: Option<Upstream> = None;
rp_settings.iter().for_each(|rpo| {
let elem = Upstream {

View file

@ -1,6 +1,7 @@
use crate::error::*;
use rustc_hash::FxHashMap as HashMap;
use serde::Deserialize;
use std::{collections::HashMap, fs};
use std::fs;
#[derive(Deserialize, Debug, Default)]
pub struct ConfigToml {

View file

@ -1,5 +1,6 @@
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
mod backend;
mod config;
@ -21,7 +22,8 @@ use crate::{
use futures::future::select_all;
use hyper::Client;
// use hyper_trust_dns::TrustDnsResolver;
use std::{collections::HashMap, io::Write, sync::Arc};
use rustc_hash::FxHashMap as HashMap;
use std::{io::Write, sync::Arc};
use tokio::time::Duration;
fn main() {
@ -63,7 +65,7 @@ fn main() {
let mut backends = Backends {
default_app: None,
apps: HashMap::<String, Backend>::new(),
apps: HashMap::<String, Backend>::default(),
};
let _ = parse_opts(&mut globals, &mut backends).expect("Invalid configuration");