Skip to content

Commit

Permalink
Remove redundant clones
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Jun 30, 2021
1 parent 64714bf commit 4786a92
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
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()))
}

0 comments on commit 4786a92

Please sign in to comment.