Skip to content

Commit

Permalink
apply clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Apr 29, 2024
1 parent f84cdeb commit 3816cbf
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 29 deletions.
1 change: 0 additions & 1 deletion bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ paw = "^1.0.0"
serde = { version = "^1.0.197", features = ["derive"] }
serde_json = "^1.0.116"
prost = "^0.12.4"
# time = "^0.3.36"
tempfile = "^3.10.1"
termion = "^3.0.0"
thiserror = "^1.0.58"
Expand Down
2 changes: 1 addition & 1 deletion lib/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ impl HttpProxy {
.listeners
.values()
.find(|listener| listener.borrow().address == address)
.ok_or(ProxyError::NoListenerFound(address.clone()))?;
.ok_or(ProxyError::NoListenerFound(address))?;

let mut owned = listener.borrow_mut();

Expand Down
2 changes: 1 addition & 1 deletion lib/src/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ impl HttpsProxy {
.listeners
.values()
.find(|listener| listener.borrow().address == address)
.ok_or(ProxyError::NoListenerFound(address.clone()))?;
.ok_or(ProxyError::NoListenerFound(address))?;

let mut owned = listener.borrow_mut();

Expand Down
2 changes: 1 addition & 1 deletion lib/src/protocol/kawa_h1/answers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Template {
.collect::<Vec<_>>();
let answer = answer
.replace("\r\n", "\n")
.replace("\n", "\r\n")
.replace('\n', "\r\n")
.into_bytes();

let len = answer.len();
Expand Down
11 changes: 4 additions & 7 deletions lib/src/protocol/kawa_h1/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ fn hex_dump(buffer: &[u8], window: usize, start: usize, end: usize) -> String {
for c in slice {
let _ = write!(result, " {c:02x}");
}
result.push_str(
&std::iter::repeat(' ')
.take((window + start - end) * 3 + 4)
.collect::<String>(),
);
result.push_str(&" ".repeat((window + start - end) * 3 + 4));
result.push_str(&String::from_utf8_lossy(slice).escape_debug().to_string());
} else {
let slice1 = &buffer[start..start + window - 1];
Expand Down Expand Up @@ -112,10 +108,11 @@ Invalid:
pub fn diagnostic_413_507(parsing_phase: ParsingPhase) -> String {
match parsing_phase {
kawa::ParsingPhase::StatusLine => {
format!("Request line is too long. Note that an URL should not exceed 2083 characters.")
"Request line is too long. Note that an URL should not exceed 2083 characters."
.to_string()
}
kawa::ParsingPhase::Headers | kawa::ParsingPhase::Cookies { .. } => {
format!("Headers are too long. All headers should fit in a single buffer.")
"Headers are too long. All headers should fit in a single buffer.".to_string()
}
phase => format!("Unexpected parsing phase: {phase:?}"),
}
Expand Down
10 changes: 5 additions & 5 deletions lib/src/protocol/kawa_h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
}
return format!("{method}");
}
return format!("");
String::new()
}

fn cluster_id_from_request(
Expand Down Expand Up @@ -1845,7 +1845,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> SessionState
// we do not have a complete answer
TimeoutStatus::Request => {
self.set_answer(DefaultAnswer::Answer408 {
duration: self.container_frontend_timeout.duration_fmt(),
duration: self.container_frontend_timeout.to_string(),
});
self.writable(metrics)
}
Expand All @@ -1854,7 +1854,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> SessionState
// this case is ambiguous, as it is the frontend timeout that triggers while we were waiting for response
// the timeout responsibility should have switched before
self.set_answer(DefaultAnswer::Answer504 {
duration: self.container_backend_timeout.duration_fmt(),
duration: self.container_backend_timeout.to_string(),
});
self.writable(metrics)
}
Expand All @@ -1875,13 +1875,13 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> SessionState
"got backend timeout while waiting for a request, this should not happen"
);
self.set_answer(DefaultAnswer::Answer504 {
duration: self.container_backend_timeout.duration_fmt(),
duration: self.container_backend_timeout.to_string(),
});
self.writable(metrics)
}
TimeoutStatus::WaitingForResponse => {
self.set_answer(DefaultAnswer::Answer504 {
duration: self.container_backend_timeout.duration_fmt(),
duration: self.container_backend_timeout.to_string(),
});
self.writable(metrics)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ impl Server {
}
}

fn reset_loop_time_and_get_timeout(&mut self) -> Option<std::time::Duration> {
fn reset_loop_time_and_get_timeout(&mut self) -> Option<Duration> {
let now = Instant::now();
time!("event_loop_time", (now - self.loop_start).as_millis());

Expand All @@ -632,7 +632,7 @@ impl Server {
};

self.loop_start = now;
timeout.and_then(|t| std::time::Duration::try_from(t).ok())
timeout
}

/// Returns true if hardstop
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ impl TcpListener {

registry
.register(&mut listener, self.token, Interest::READABLE)
.map_err(|io_err| ProxyError::RegisterListener(io_err))?;
.map_err(ProxyError::RegisterListener)?;

self.listener = Some(listener);
self.active = true;
Expand Down
22 changes: 12 additions & 10 deletions lib/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//! code imported from mio-extras
//! License: MIT or Apache 2.0
use std::{
cmp, iter,
cmp,
fmt::Display,
iter,
time::{Duration, Instant},
u64, usize,
};
Expand Down Expand Up @@ -146,11 +148,6 @@ impl TimeoutContainer {
self.duration
}

/// format timeout duration
pub fn duration_fmt(&self) -> String {
format!("{:?}", self.duration)
}

pub fn cancel(&mut self) -> bool {
match self.timeout.take() {
None => {
Expand Down Expand Up @@ -187,10 +184,16 @@ impl TimeoutContainer {
}
}

impl Display for TimeoutContainer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.duration)
}
}

impl std::ops::Drop for TimeoutContainer {
fn drop(&mut self) {
if self.cancel() {
debug!("Cancel a dangling timeout that haven't be handled in session lifecycle, token ({:?}), duration {}", self.token, self.duration_fmt());
debug!("Cancel a dangling timeout that haven't be handled in session lifecycle, token ({:?}), duration {}", self.token, self);
}
}
}
Expand Down Expand Up @@ -475,9 +478,8 @@ impl<T> Timer<T> {
}

pub fn next_poll_date(&self) -> Option<Instant> {
self.next_tick().map(|tick| {
self.start + Duration::from_millis(self.tick_ms.saturating_mul(tick) as u64)
})
self.next_tick()
.map(|tick| self.start + Duration::from_millis(self.tick_ms.saturating_mul(tick)))
}

fn slot_for(&self, tick: Tick) -> usize {
Expand Down

0 comments on commit 3816cbf

Please sign in to comment.