Skip to content

Commit

Permalink
fix: remove sockaddrs field from Target
Browse files Browse the repository at this point in the history
- remove the `sockaddrs` field from `Target`.
- resolve the `sockaddrs` only in `try_connect_over_tcp`, the only place
  it's required.
- do not resolved `sockaddrs` on `build_with_stream`.
  • Loading branch information
oleonardolima committed Jun 19, 2024
1 parent 717e9fe commit fcf3da2
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions client/transport/src/ws/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl WsTransportClientBuilder {
&self,
uri: Url,
) -> Result<(Sender<Compat<EitherStream>>, Receiver<Compat<EitherStream>>), WsHandshakeError> {
let mut target: Target = uri.try_into()?;
let mut target: Target = uri.clone().try_into()?;
let mut err = None;

// Only build TLS connector if `wss` in URL.
Expand All @@ -340,7 +340,8 @@ impl WsTransportClientBuilder {
tracing::debug!(target: LOG_TARGET, "Connecting to target: {:?}", target);

// The sockaddrs might get reused if the server replies with a relative URI.
let sockaddrs = std::mem::take(&mut target.sockaddrs);
let sockaddrs = uri.socket_addrs(|| None).map_err(WsHandshakeError::ResolutionFailed)?;

for sockaddr in &sockaddrs {
#[cfg(feature = "tls")]
let tcp_stream = match connect(*sockaddr, self.connection_timeout, &target.host, connector.as_ref(), self.tcp_no_delay)
Expand Down Expand Up @@ -407,7 +408,6 @@ impl WsTransportClientBuilder {
}
};
}
target.sockaddrs = sockaddrs;
break;
}

Expand Down Expand Up @@ -550,8 +550,6 @@ impl From<soketto::connection::Error> for WsError {
/// Represents a verified remote WebSocket address.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Target {
/// Socket addresses resolved the host name.
sockaddrs: Vec<SocketAddr>,
/// The host name (domain or IP address).
host: String,
/// The Host request header specifies the host and port number of the server to which the request is being sent.
Expand Down Expand Up @@ -600,8 +598,7 @@ impl TryFrom<url::Url> for Target {

let host_header = if let Some(port) = url.port() { format!("{host}:{port}") } else { host.to_string() };

let sockaddrs = url.socket_addrs(|| None).map_err(WsHandshakeError::ResolutionFailed)?;
Ok(Self { sockaddrs, host, host_header, _mode, path_and_query: path_and_query.to_string(), basic_auth })
Ok(Self { host, host_header, _mode, path_and_query: path_and_query.to_string(), basic_auth })
}
}

Expand Down

0 comments on commit fcf3da2

Please sign in to comment.