Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant clones #307

Merged
merged 2 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl Server {
args.log,
"Packet Received";
"from" => recv_addr,
"contents" => debug::bytes_to_string(packet.to_vec()),
"contents" => debug::bytes_to_string(&packet),
);

let endpoints = match args.cluster_manager.read().get_all_endpoints() {
Expand All @@ -333,7 +333,7 @@ impl Server {
let filter_manager_guard = args.filter_manager.read();
filter_manager_guard.get_filter_chain()
};
let result = filter_chain.read(ReadContext::new(endpoints, recv_addr, packet.to_vec()));
let result = filter_chain.read(ReadContext::new(endpoints, recv_addr, packet));

if let Some(response) = result {
for endpoint in response.endpoints.iter() {
Expand Down Expand Up @@ -460,13 +460,10 @@ impl Server {
log,
"Sending packet back to origin";
"origin" => packet.dest(),
"contents" => debug::bytes_to_string(packet.contents().clone()),
"contents" => debug::bytes_to_string(packet.contents()),
);

if let Err(err) = socket
.send_to(packet.contents().as_slice(), &packet.dest())
.await
{
if let Err(err) = socket.send_to(packet.contents(), &packet.dest()).await {
error!(log, "Error sending packet"; "dest" => %packet.dest(), "error" => %err);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/proxy/sessions/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl Session {

trace!(log, "Received packet"; "from" => from,
"endpoint_addr" => &endpoint.address,
"contents" => debug::bytes_to_string(packet.to_vec()));
"contents" => debug::bytes_to_string(&packet));

if let Err(err) = Session::do_update_expiration(expiration, ttl) {
warn!(log, "Error updating session expiration"; "error" => %err)
Expand Down Expand Up @@ -256,7 +256,7 @@ impl Session {
pub async fn send(&self, buf: &[u8]) -> Result<Option<usize>> {
trace!(self.log, "Sending packet";
"dest_address" => &self.dest.address,
"contents" => debug::bytes_to_string(buf.to_vec()));
"contents" => debug::bytes_to_string(buf));

self.do_send(buf)
.await
Expand Down
5 changes: 3 additions & 2 deletions src/utils/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

/// Attempt to convert a packet into a string, if it is one, otherwise return some human
/// readable details about the packet.
pub(crate) fn bytes_to_string(bytes: Vec<u8>) -> String {
String::from_utf8(bytes.to_vec())
pub(crate) fn bytes_to_string(bytes: &[u8]) -> String {
std::str::from_utf8(bytes)
.map(str::to_string)
.unwrap_or_else(|_| format!("<raw bytes :: len: {}>", bytes.len()))
}