Skip to content

Commit

Permalink
Apply clippy suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Eloi DEMOLIS <eloi.demolis@clever-cloud.com>
  • Loading branch information
Wonshtrum committed Jun 5, 2023
1 parent dc78bc0 commit fe9097e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
14 changes: 6 additions & 8 deletions e2e/src/mock/async_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ pub struct BackendHandle<T> {
pub aggregator_rx: mpsc::Receiver<T>,
}

type RequestHandler<A> = Box<dyn Fn(&TcpStream, &str, A) -> A + Send + Sync>;

impl<A: Aggregator + Send + Sync + 'static> BackendHandle<A> {
pub fn spawn_detached_backend<S: Into<String>>(
name: S,
address: SocketAddr,
mut aggregator: A,
handler: Box<dyn Fn(&TcpStream, &String, A) -> A + Send + Sync>,
handler: RequestHandler<A>,
) -> Self {
let name = name.into();
let (stop_tx, mut stop_rx) = mpsc::channel::<()>(1);
Expand Down Expand Up @@ -97,9 +99,7 @@ impl BackendHandle<SimpleAggregator> {
/// This creates a callback that listens on a TcpStream
/// and returns HTTP OK responses with the given content in the body
/// it returns an updated aggregator
pub fn http_handler<S: Into<String>>(
content: S,
) -> Box<dyn Fn(&TcpStream, &String, SimpleAggregator) -> SimpleAggregator + Send + Sync> {
pub fn http_handler<S: Into<String>>(content: S) -> RequestHandler<SimpleAggregator> {
let content = content.into();
Box::new(move |mut stream, backend_name, mut aggregator| {
let mut buf = [0u8; BUFFER_SIZE];
Expand All @@ -125,10 +125,8 @@ impl BackendHandle<SimpleAggregator> {
/// This creates a callback that listens on a TcpStream
/// and returns the given content as raw bytes
/// it returns an updated aggregator
pub fn tcp_handler<S: Into<String>>(
content: String,
) -> Box<dyn Fn(&TcpStream, &String, SimpleAggregator) -> SimpleAggregator + Send + Sync> {
let content: String = content;
pub fn tcp_handler<S: Into<String>>(content: S) -> RequestHandler<SimpleAggregator> {
let content: String = content.into();
Box::new(move |mut stream, backend_name, mut aggregator| {
let mut buf = [0u8; BUFFER_SIZE];
match stream.read(&mut buf) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/load_balancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl LoadBalancingAlgorithm for Random {
})
.collect();

if let Ok(dist) = WeightedIndex::new(&weights) {
if let Ok(dist) = WeightedIndex::new(weights) {
let index = dist.sample(&mut rng);
backends.get(index).cloned()
} else {
Expand Down
29 changes: 12 additions & 17 deletions lib/src/protocol/kawa_h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,19 +1045,16 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
}

pub fn is_valid_backend_socket(&self) -> bool {
// if socket was not used in the last second, test it
if self
.backend_stop
.as_ref()
.map(|t| {
// if socket was last used in the last second, test it
match self.backend_stop.as_ref() {
Some(stop_instant) => {
let now = Instant::now();
let dur = now - *t;

dur > Duration::seconds(1)
})
.unwrap_or(true)
{
return self.test_backend_socket();
let dur = now - *stop_instant;
if dur > Duration::seconds(1) {
return self.test_backend_socket();
}
}
None => return self.test_backend_socket(),
}

true
Expand Down Expand Up @@ -1727,12 +1724,10 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
} else {
TimeoutStatus::Response
}
} else if self.keepalive_count > 0 {
TimeoutStatus::WaitingForNewRequest
} else {
if self.keepalive_count > 0 {
TimeoutStatus::WaitingForNewRequest
} else {
TimeoutStatus::Request
}
TimeoutStatus::Request
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/protocol/kawa_h1/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{fmt, str::from_utf8_unchecked};

use nom::{
bytes::{self, complete::take_while},
character::{complete::digit1, is_alphanumeric},
Expand All @@ -6,7 +8,6 @@ use nom::{
sequence::preceded,
Err, IResult,
};
use std::{fmt, str::from_utf8_unchecked};

pub fn compare_no_case(left: &[u8], right: &[u8]) -> bool {
if left.len() != right.len() {
Expand All @@ -21,6 +22,7 @@ pub fn compare_no_case(left: &[u8], right: &[u8]) -> bool {
_ => false,
})
}

#[derive(PartialEq, Eq, Debug, Clone)]
pub enum Method {
Get,
Expand Down

0 comments on commit fe9097e

Please sign in to comment.