[core] Unix upstreams: unix:/path parses strictly into a socket upstream; edge proxies and health-checks it in plain HTTP over the socket, Caddy dials unix/<path>
This commit is contained in:
@@ -39,6 +39,36 @@ pub fn peer(addr: SocketAddr, u: &UpstreamRt) -> HttpPeer {
|
||||
p
|
||||
}
|
||||
|
||||
/// The peer of a `unix:` upstream (plain HTTP over the socket).
|
||||
#[cfg(unix)]
|
||||
pub fn unix_peer(path: &str) -> Result<HttpPeer> {
|
||||
let mut p = HttpPeer::new_uds(path, false, String::new())?;
|
||||
p.options.connection_timeout = Some(DIAL_TIMEOUT);
|
||||
Ok(p)
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
pub fn unix_peer(path: &str) -> Result<HttpPeer> {
|
||||
Error::e_explain(
|
||||
ErrorType::SocketError,
|
||||
format!("{path}: Unix sockets need a Unix system"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Every peer to try for `u`, in order: its socket, or each address its name resolves to.
|
||||
pub async fn peers(u: &UpstreamRt) -> Result<Vec<HttpPeer>> {
|
||||
if let Some(path) = &u.unix {
|
||||
return Ok(vec![unix_peer(path)?]);
|
||||
}
|
||||
let addrs = u.resolve().await.map_err(|e| {
|
||||
Error::explain(
|
||||
ErrorType::ConnectNoRoute,
|
||||
format!("cannot resolve {}: {e}", u.authority),
|
||||
)
|
||||
})?;
|
||||
Ok(addrs.into_iter().map(|a| peer(a, u)).collect())
|
||||
}
|
||||
|
||||
/// Send `req` without a body; read the whole response, at most `max_body` bytes of body,
|
||||
/// all within `timeout`.
|
||||
pub async fn fetch(
|
||||
@@ -64,16 +94,9 @@ async fn exchange(
|
||||
max_body: usize,
|
||||
timeout: Duration,
|
||||
) -> Result<Fetched> {
|
||||
let addrs = u.resolve().await.map_err(|e| {
|
||||
Error::explain(
|
||||
ErrorType::ConnectNoRoute,
|
||||
format!("cannot resolve {}: {e}", u.authority),
|
||||
)
|
||||
})?;
|
||||
let mut last = None;
|
||||
let mut session = None;
|
||||
for addr in addrs {
|
||||
let mut p = peer(addr, u);
|
||||
for mut p in peers(u).await? {
|
||||
p.options.total_connection_timeout = Some(timeout);
|
||||
match connector.get_http_session(&p).await {
|
||||
Ok((s, _reused)) => {
|
||||
|
||||
@@ -122,6 +122,9 @@ async fn round(host: &Arc<HostRt>, path: &str, connector: &Arc<Connector>, watch
|
||||
let (h, p, c) = (host.clone(), path.to_owned(), connector.clone());
|
||||
let probe_host = if watch {
|
||||
host.cfg.hostname.clone()
|
||||
} else if host.upstreams[i].unix.is_some() {
|
||||
// A socket has no authority to name; the probe asks as a local client would.
|
||||
"localhost".to_owned()
|
||||
} else {
|
||||
host.upstreams[i].authority.clone()
|
||||
};
|
||||
|
||||
@@ -306,6 +306,11 @@ impl ProxyHttp for EdgeProxy {
|
||||
Error::explain(ErrorType::HTTPStatus(503), "no healthy upstream")
|
||||
})?;
|
||||
let up = &host.upstreams[i];
|
||||
if let Some(path) = &up.unix {
|
||||
// A socket: one peer, nothing to resolve and no other address to try.
|
||||
log::debug!(target: "ianitor::proxy", "{}: upstream {}", host.cfg.hostname, up.authority);
|
||||
return Ok(Box::new(fetch::unix_peer(path)?));
|
||||
}
|
||||
ctx.addrs = match up.resolve().await {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
|
||||
@@ -85,6 +85,8 @@ pub struct UpstreamRt {
|
||||
pub authority: String,
|
||||
pub tls: bool,
|
||||
pub sni: String,
|
||||
/// A `unix:` upstream's socket path (plain HTTP); `host` and `port` are then unused.
|
||||
pub unix: Option<String>,
|
||||
pub healthy: AtomicBool,
|
||||
pub health: HealthRecord,
|
||||
}
|
||||
@@ -205,6 +207,21 @@ fn upstream(u: &EdgeUpstream) -> Result<UpstreamRt, EdgeError> {
|
||||
address: u.address.clone(),
|
||||
reason: reason.to_owned(),
|
||||
};
|
||||
if let Some(path) = u.address.strip_prefix("unix:") {
|
||||
if !path.starts_with('/') || u.tls {
|
||||
return Err(err("expected unix:/absolute/path, plain HTTP"));
|
||||
}
|
||||
return Ok(UpstreamRt {
|
||||
host: String::new(),
|
||||
port: 0,
|
||||
authority: u.address.clone(),
|
||||
tls: false,
|
||||
sni: String::new(),
|
||||
unix: Some(path.to_owned()),
|
||||
healthy: AtomicBool::new(true),
|
||||
health: HealthRecord::default(),
|
||||
});
|
||||
}
|
||||
let (host, port) = u
|
||||
.address
|
||||
.rsplit_once(':')
|
||||
@@ -223,6 +240,7 @@ fn upstream(u: &EdgeUpstream) -> Result<UpstreamRt, EdgeError> {
|
||||
authority: u.address.clone(),
|
||||
tls: u.tls,
|
||||
sni: u.sni.clone(),
|
||||
unix: None,
|
||||
healthy: AtomicBool::new(true),
|
||||
health: HealthRecord::default(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user