Round robin backends

This commit is contained in:
Alex 2021-12-07 16:37:22 +01:00
parent cd7e5ad034
commit ccb4e87658
No known key found for this signature in database
GPG key ID: EDABF9711E244EB1
3 changed files with 14 additions and 6 deletions

View file

@ -1,5 +1,5 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::{atomic::Ordering, Arc};
use anyhow::Result; use anyhow::Result;
use log::*; use log::*;
@ -108,10 +108,13 @@ async fn handle(
.as_ref() .as_ref()
.map(|x| x.len() as i32) .map(|x| x.len() as i32)
.unwrap_or(0)), .unwrap_or(0)),
ent.calls.load(Ordering::SeqCst),
) )
}); });
if let Some(proxy_to) = ent { if let Some(proxy_to) = ent {
proxy_to.calls.fetch_add(1, Ordering::SeqCst);
let to_addr = format!("http://{}", proxy_to.target_addr); let to_addr = format!("http://{}", proxy_to.target_addr);
info!("Proxying {} {} -> {}", host, path, to_addr); info!("Proxying {} {} -> {}", host, path, to_addr);

View file

@ -1,5 +1,5 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::{atomic, Arc};
use std::{cmp, time::Duration}; use std::{cmp, time::Duration};
use log::*; use log::*;
@ -12,9 +12,15 @@ use crate::consul::*;
#[derive(Debug)] #[derive(Debug)]
pub struct ProxyEntry { pub struct ProxyEntry {
pub target_addr: SocketAddr, pub target_addr: SocketAddr,
pub host: String, pub host: String,
pub path_prefix: Option<String>, pub path_prefix: Option<String>,
pub priority: u32, pub priority: u32,
// Counts the number of times this proxy server has been called to
// This implements a round-robin load balancer if there are multiple
// entries for the same host and same path prefix.
pub calls: atomic::AtomicU64,
} }
#[derive(Debug)] #[derive(Debug)]
@ -53,6 +59,7 @@ fn parse_tricot_tag(target_addr: SocketAddr, tag: &str) -> Option<ProxyEntry> {
host: host.to_string(), host: host.to_string(),
path_prefix, path_prefix,
priority, priority,
calls: atomic::AtomicU64::from(0),
}) })
} }

View file

@ -72,10 +72,8 @@ fn create_proxied_request<B>(
// If request does not have host header, add it from original URI authority // If request does not have host header, add it from original URI authority
if let Some(authority) = request.uri().authority() { if let Some(authority) = request.uri().authority() {
if let hyper::header::Entry::Vacant(entry) = builder if let hyper::header::Entry::Vacant(entry) =
.headers_mut() builder.headers_mut().unwrap().entry(host_header_name)
.unwrap()
.entry(host_header_name)
{ {
entry.insert(authority.as_str().parse()?); entry.insert(authority.as_str().parse()?);
} }