mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-29 22:38:36 +08:00
Performance optimization v2
This commit is contained in:
@@ -25,10 +25,10 @@ pub async fn for_consul(url: String, token: Option<String>, conf: &ServiceMappin
|
|||||||
for subsets in endpoints {
|
for subsets in endpoints {
|
||||||
// let addr = subsets.tagged_addresses.get("lan_ipv4").unwrap().address.clone();
|
// let addr = subsets.tagged_addresses.get("lan_ipv4").unwrap().address.clone();
|
||||||
// let prt = subsets.tagged_addresses.get("lan_ipv4").unwrap().port.clone();
|
// let prt = subsets.tagged_addresses.get("lan_ipv4").unwrap().port.clone();
|
||||||
let addr = subsets.tagged_addresses.get("lan_ipv4").unwrap().address.clone().parse().unwrap();
|
let addr = subsets.tagged_addresses.get("lan_ipv4").unwrap().address.clone();
|
||||||
let prt = subsets.tagged_addresses.get("lan_ipv4").unwrap().port.clone();
|
let prt = subsets.tagged_addresses.get("lan_ipv4").unwrap().port.clone();
|
||||||
let to_add = Arc::from(InnerMap {
|
let to_add = Arc::from(InnerMap {
|
||||||
address: addr,
|
address: Arc::from(&*addr),
|
||||||
port: prt,
|
port: prt,
|
||||||
is_ssl: false,
|
is_ssl: false,
|
||||||
is_http2: false,
|
is_http2: false,
|
||||||
@@ -61,7 +61,7 @@ pub async fn for_kuber(url: &str, token: &str, conf: &ServiceMapping) -> Option<
|
|||||||
for addr in addresses {
|
for addr in addresses {
|
||||||
for port in &ports {
|
for port in &ports {
|
||||||
let to_add = Arc::from(InnerMap {
|
let to_add = Arc::from(InnerMap {
|
||||||
address: addr.ip.parse().unwrap(),
|
address: Arc::from(addr.ip.clone()),
|
||||||
port: port.port.clone(),
|
port: port.port.clone(),
|
||||||
is_ssl: false,
|
is_ssl: false,
|
||||||
is_http2: false,
|
is_http2: false,
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ async fn populate_file_upstreams(config: &mut Configuration, parsed: &Config) {
|
|||||||
if let Some((ip, port_str)) = server.split_once(':') {
|
if let Some((ip, port_str)) = server.split_once(':') {
|
||||||
if let Ok(port) = port_str.parse::<u16>() {
|
if let Ok(port) = port_str.parse::<u16>() {
|
||||||
server_list.push(Arc::from(InnerMap {
|
server_list.push(Arc::from(InnerMap {
|
||||||
address: ip.trim().parse().unwrap(),
|
address: Arc::from(ip),
|
||||||
port,
|
port,
|
||||||
is_ssl: true,
|
is_ssl: true,
|
||||||
is_http2: false,
|
is_http2: false,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::IpAddr;
|
|
||||||
use std::sync::atomic::AtomicUsize;
|
use std::sync::atomic::AtomicUsize;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@@ -10,6 +9,14 @@ pub type UpstreamsDashMap = DashMap<Arc<str>, DashMap<Arc<str>, (Vec<Arc<InnerMa
|
|||||||
pub type UpstreamsIdMap = DashMap<Arc<str>, Arc<InnerMap>>;
|
pub type UpstreamsIdMap = DashMap<Arc<str>, Arc<InnerMap>>;
|
||||||
pub type Headers = DashMap<Arc<str>, DashMap<Arc<str>, Vec<(Arc<str>, Arc<str>)>>>;
|
pub type Headers = DashMap<Arc<str>, DashMap<Arc<str>, Vec<(Arc<str>, Arc<str>)>>>;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
pub struct Extraparams {
|
||||||
|
pub sticky_sessions: bool,
|
||||||
|
pub to_https: Option<bool>,
|
||||||
|
pub authentication: DashMap<Arc<str>, Vec<Arc<str>>>,
|
||||||
|
pub rate_limit: Option<isize>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||||
pub struct ServiceMapping {
|
pub struct ServiceMapping {
|
||||||
pub upstream: String,
|
pub upstream: String,
|
||||||
@@ -21,13 +28,6 @@ pub struct ServiceMapping {
|
|||||||
pub server_headers: Option<Vec<String>>,
|
pub server_headers: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
|
||||||
pub struct Extraparams {
|
|
||||||
pub sticky_sessions: bool,
|
|
||||||
pub to_https: Option<bool>,
|
|
||||||
pub authentication: DashMap<Arc<str>, Vec<Arc<str>>>,
|
|
||||||
pub rate_limit: Option<isize>,
|
|
||||||
}
|
|
||||||
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
|
||||||
pub struct Kubernetes {
|
pub struct Kubernetes {
|
||||||
pub servers: Option<Vec<String>>,
|
pub servers: Option<Vec<String>>,
|
||||||
@@ -114,9 +114,9 @@ pub struct AppConfig {
|
|||||||
pub rungroup: Option<String>,
|
pub rungroup: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct InnerMap {
|
pub struct InnerMap {
|
||||||
pub address: IpAddr,
|
pub address: Arc<str>,
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub is_ssl: bool,
|
pub is_ssl: bool,
|
||||||
pub is_http2: bool,
|
pub is_http2: bool,
|
||||||
@@ -129,7 +129,8 @@ pub struct InnerMap {
|
|||||||
impl InnerMap {
|
impl InnerMap {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
address: "127.0.0.1".parse().unwrap(),
|
// address: "127.0.0.1".parse().unwrap(),
|
||||||
|
address: Arc::from("127.0.0.1"),
|
||||||
port: Default::default(),
|
port: Default::default(),
|
||||||
is_ssl: Default::default(),
|
is_ssl: Default::default(),
|
||||||
is_http2: Default::default(),
|
is_http2: Default::default(),
|
||||||
@@ -140,9 +141,18 @@ impl InnerMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||||
pub struct UpstreamSnapshot {
|
pub struct InnerMapForJson {
|
||||||
pub backends: Vec<InnerMap>,
|
pub address: String,
|
||||||
|
pub port: u16,
|
||||||
|
pub is_ssl: bool,
|
||||||
|
pub is_http2: bool,
|
||||||
|
pub to_https: bool,
|
||||||
|
pub rate_limit: Option<isize>,
|
||||||
|
pub healthcheck: Option<bool>,
|
||||||
|
}
|
||||||
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||||
|
pub struct UpstreamSnapshotForJson {
|
||||||
|
pub backends: Vec<InnerMapForJson>,
|
||||||
pub requests: usize,
|
pub requests: usize,
|
||||||
}
|
}
|
||||||
// pub type UpstreamsSnapshot = HashMap<String, HashMap<String, UpstreamSnapshot>>;
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::utils::structs::{InnerMap, UpstreamSnapshot, UpstreamsDashMap, UpstreamsIdMap};
|
use crate::utils::structs::{InnerMap, InnerMapForJson, UpstreamSnapshotForJson, UpstreamsDashMap, UpstreamsIdMap};
|
||||||
use crate::utils::tls;
|
use crate::utils::tls;
|
||||||
use crate::utils::tls::CertificateConfig;
|
use crate::utils::tls::CertificateConfig;
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
@@ -161,7 +161,7 @@ pub fn clone_idmap_into(original: &UpstreamsDashMap, cloned: &UpstreamsIdMap) {
|
|||||||
let hex_hash = base16ct::lower::encode_string(&hash);
|
let hex_hash = base16ct::lower::encode_string(&hash);
|
||||||
let hh = hex_hash[0..50].to_string();
|
let hh = hex_hash[0..50].to_string();
|
||||||
let to_add = InnerMap {
|
let to_add = InnerMap {
|
||||||
address: "127.0.0.1".parse().unwrap(),
|
address: Arc::from("127.0.0.1"),
|
||||||
port: 0,
|
port: 0,
|
||||||
is_ssl: false,
|
is_ssl: false,
|
||||||
is_http2: false,
|
is_http2: false,
|
||||||
@@ -283,8 +283,19 @@ pub fn upstreams_to_json(upstreams: &UpstreamsDashMap) -> serde_json::Result<Str
|
|||||||
|
|
||||||
inner_map.insert(
|
inner_map.insert(
|
||||||
inner_entry.key().to_string(),
|
inner_entry.key().to_string(),
|
||||||
UpstreamSnapshot {
|
UpstreamSnapshotForJson {
|
||||||
backends: backends.iter().map(|a| (**a).clone()).collect(),
|
backends: backends
|
||||||
|
.iter()
|
||||||
|
.map(|a| InnerMapForJson {
|
||||||
|
address: a.address.to_string(),
|
||||||
|
port: a.port,
|
||||||
|
is_ssl: a.is_ssl,
|
||||||
|
is_http2: a.is_http2,
|
||||||
|
to_https: a.to_https,
|
||||||
|
rate_limit: a.rate_limit,
|
||||||
|
healthcheck: a.healthcheck,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
requests: counter.load(Ordering::Relaxed),
|
requests: counter.load(Ordering::Relaxed),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -323,7 +334,7 @@ pub fn upstreams_liveness_json(configured: &UpstreamsDashMap, current: &Upstream
|
|||||||
false
|
false
|
||||||
};
|
};
|
||||||
json!({
|
json!({
|
||||||
"address": backend.address,
|
"address": &*backend.address,
|
||||||
"port": backend.port,
|
"port": backend.port,
|
||||||
"alive": alive
|
"alive": alive
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ impl ProxyHttp for LB {
|
|||||||
match ctx.hostname.as_ref() {
|
match ctx.hostname.as_ref() {
|
||||||
Some(hostname) => match ctx.upstream_peer.as_ref() {
|
Some(hostname) => match ctx.upstream_peer.as_ref() {
|
||||||
Some(innermap) => {
|
Some(innermap) => {
|
||||||
let mut peer = Box::new(HttpPeer::new((innermap.address, innermap.port), innermap.is_ssl, String::new()));
|
let mut peer = Box::new(HttpPeer::new((&*innermap.address, innermap.port), innermap.is_ssl, String::new()));
|
||||||
if innermap.is_http2 {
|
if innermap.is_http2 {
|
||||||
peer.options.alpn = ALPN::H2;
|
peer.options.alpn = ALPN::H2;
|
||||||
}
|
}
|
||||||
@@ -182,7 +182,7 @@ impl ProxyHttp for LB {
|
|||||||
upstream_request.insert_header("Host", hostname.as_ref())?;
|
upstream_request.insert_header("Host", hostname.as_ref())?;
|
||||||
}
|
}
|
||||||
if let Some(peer) = ctx.upstream_peer.as_ref() {
|
if let Some(peer) = ctx.upstream_peer.as_ref() {
|
||||||
upstream_request.insert_header("X-Forwarded-For", peer.address.to_string())?;
|
upstream_request.insert_header("X-Forwarded-For", &*peer.address)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(headers) = self.get_header(ctx.hostname.as_ref().unwrap_or(&Arc::from("localhost")), session.req_header().uri.path()) {
|
if let Some(headers) = self.get_header(ctx.hostname.as_ref().unwrap_or(&Arc::from("localhost")), session.req_header().uri.path()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user