[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)) => {
|
||||
|
||||
Reference in New Issue
Block a user