From c4fecbf2ea597ce2c2e249ef7b0f5c220a5fdbad Mon Sep 17 00:00:00 2001 From: Anthony Griffon Date: Sun, 7 Jan 2024 18:39:17 +0100 Subject: [PATCH] misc: clippy Signed-off-by: Anthony Griffon --- .../application/server/cmd/client/set_info.rs | 4 +- app/roster/src/application/server/cmd/get.rs | 1 - .../src/application/server/cmd/parse.rs | 4 +- app/roster/src/application/server/cmd/ping.rs | 3 +- app/roster/src/application/server/cmd/set.rs | 14 ++----- .../src/application/server/cmd/unknown.rs | 2 +- .../src/application/server/connection.rs | 4 +- app/roster/src/application/server/context.rs | 2 - app/roster/src/application/server/frame.rs | 14 +++---- app/roster/src/application/server/handle.rs | 4 +- app/roster/src/application/server/mod.rs | 8 +--- app/roster/src/domain/storage/mod.rs | 42 ++----------------- .../src/infrastructure/instruments/mod.rs | 1 + app/roster/src/main.rs | 2 +- 14 files changed, 26 insertions(+), 79 deletions(-) diff --git a/app/roster/src/application/server/cmd/client/set_info.rs b/app/roster/src/application/server/cmd/client/set_info.rs index cee42b2..fa8b9ee 100644 --- a/app/roster/src/application/server/cmd/client/set_info.rs +++ b/app/roster/src/application/server/cmd/client/set_info.rs @@ -27,7 +27,9 @@ use crate::application::server::frame::Frame; /// Note that these attributes are not cleared by the RESET command. #[derive(Debug, Default)] pub struct ClientSetInfo { + #[allow(dead_code)] lib_name: Option, + #[allow(dead_code)] lib_version: Option, } @@ -98,7 +100,7 @@ impl ClientSetInfo { pub(crate) async fn apply( self, dst: &mut WriteConnection, - ctx: Context, + _ctx: Context, ) -> anyhow::Result<()> { monoio::time::sleep(Duration::from_secs(3)).await; /* diff --git a/app/roster/src/application/server/cmd/get.rs b/app/roster/src/application/server/cmd/get.rs index c97e739..fc95c39 100644 --- a/app/roster/src/application/server/cmd/get.rs +++ b/app/roster/src/application/server/cmd/get.rs @@ -1,5 +1,4 @@ use bytestring::ByteString; -use tracing::info; use super::parse::Parse; use super::CommandExecution; diff --git a/app/roster/src/application/server/cmd/parse.rs b/app/roster/src/application/server/cmd/parse.rs index c0ccb71..9e050dd 100644 --- a/app/roster/src/application/server/cmd/parse.rs +++ b/app/roster/src/application/server/cmd/parse.rs @@ -1,6 +1,6 @@ use std::{str, vec}; -use bytes::{Buf, Bytes}; +use bytes::Bytes; use bytestring::ByteString; use crate::application::server::frame::Frame; @@ -94,7 +94,7 @@ impl Parse { // // Although errors are stored as strings and could be represented as // raw bytes, they are considered separate types. - Frame::Simple(s) => Ok(Bytes::from(s.into_bytes())), + Frame::Simple(s) => Ok(s.into_bytes()), Frame::Bulk(data) => Ok(data), frame => Err(format!( "protocol error; expected simple frame or bulk frame, got {:?}", diff --git a/app/roster/src/application/server/cmd/ping.rs b/app/roster/src/application/server/cmd/ping.rs index f54072f..f6e029f 100644 --- a/app/roster/src/application/server/cmd/ping.rs +++ b/app/roster/src/application/server/cmd/ping.rs @@ -1,6 +1,5 @@ use bytes::Bytes; use bytestring::ByteString; -use tracing::info; use super::parse::{Parse, ParseError}; use super::CommandExecution; @@ -58,7 +57,7 @@ impl CommandExecution for Ping { async fn apply( self, dst: &mut WriteConnection, - ctx: Context, + _ctx: Context, ) -> anyhow::Result<()> { let response = match self.msg { None => Frame::Simple(ByteString::from_static("PONG")), diff --git a/app/roster/src/application/server/cmd/set.rs b/app/roster/src/application/server/cmd/set.rs index d7859dc..4e14e88 100644 --- a/app/roster/src/application/server/cmd/set.rs +++ b/app/roster/src/application/server/cmd/set.rs @@ -2,8 +2,6 @@ use std::time::Duration; use bytes::Bytes; use bytestring::ByteString; -use monoio::time::Instant; -use tracing::info; use super::parse::{Parse, ParseError}; use super::CommandExecution; @@ -53,7 +51,7 @@ pub struct Set { expire: Option, } -const OK_STR: ByteString = ByteString::from_static("OK"); +static OK_STR: ByteString = ByteString::from_static("OK"); impl Set { /// Parse a `Set` instance from a received frame. @@ -125,13 +123,7 @@ impl CommandExecution for Set { dst: &mut WriteConnection, ctx: Context, ) -> anyhow::Result<()> { - /* - let expired = match self.expire { - Some(dur) => Some(ctx.now() + dur.into()), - None => None, - }; - */ - let expired = None; + let expired = self.expire.map(|dur| ctx.now() + dur.into()); // let now = Instant::now(); let response = match ctx @@ -139,7 +131,7 @@ impl CommandExecution for Set { .set_async(self.key, self.value, SetOptions { expired }) .await { - Ok(_) => Frame::Simple(OK_STR), + Ok(_) => Frame::Simple(OK_STR.clone()), Err(_) => Frame::Null, }; // let elapsed = now.elapsed(); diff --git a/app/roster/src/application/server/cmd/unknown.rs b/app/roster/src/application/server/cmd/unknown.rs index 9db1e75..b9e0dc8 100644 --- a/app/roster/src/application/server/cmd/unknown.rs +++ b/app/roster/src/application/server/cmd/unknown.rs @@ -26,7 +26,7 @@ impl CommandExecution for Unknown { async fn apply( self, dst: &mut WriteConnection, - ctx: Context, + _ctx: Context, ) -> anyhow::Result<()> { let response = Frame::Error(ByteString::from(format!( "ERR unknown command '{}'", diff --git a/app/roster/src/application/server/connection.rs b/app/roster/src/application/server/connection.rs index 6b649c4..092e8e8 100644 --- a/app/roster/src/application/server/connection.rs +++ b/app/roster/src/application/server/connection.rs @@ -8,7 +8,7 @@ use monoio::io::{ }; use monoio::net::TcpStream; -use super::frame::{self, Frame}; +use super::frame::Frame; /// Send and receive `Frame` values from a remote peer. /// @@ -205,7 +205,7 @@ impl WriteConnection { } Frame::Integer(val) => { self.stream_w.write(&[b':']).await.0?; - self.write_decimal(*val as u64).await?; + self.write_decimal(*val).await?; } Frame::Null => { self.stream_w.write(b"$-1\r\n").await.0?; diff --git a/app/roster/src/application/server/context.rs b/app/roster/src/application/server/context.rs index 925ec03..77f81aa 100644 --- a/app/roster/src/application/server/context.rs +++ b/app/roster/src/application/server/context.rs @@ -1,6 +1,4 @@ use std::cell::Cell; -use std::rc::Rc; -use std::sync::Arc; use coarsetime::Instant; diff --git a/app/roster/src/application/server/frame.rs b/app/roster/src/application/server/frame.rs index 76b9862..d8baaa7 100644 --- a/app/roster/src/application/server/frame.rs +++ b/app/roster/src/application/server/frame.rs @@ -2,14 +2,12 @@ //! parsing frames from a byte array. use std::convert::TryInto; -use std::fmt; use std::io::Cursor; use std::num::TryFromIntError; use std::string::FromUtf8Error; use bytes::{Buf, Bytes, BytesMut}; use bytestring::ByteString; -use monoio::buf::{IoBuf, Slice}; /// A frame in the Redis protocol. #[derive(Clone, Debug)] @@ -103,7 +101,7 @@ impl Frame { if b'-' == peek_u8(src)? { let line = get_line(src)?; - if &line != b"-1".as_slice() { + if line != b"-1".as_slice() { return Err( "protocol error; invalid frame format".into() ); @@ -220,7 +218,7 @@ fn get_decimal_mut(src: &mut Cursor<&BytesMut>) -> Result { /// Find a line #[inline] -fn get_line<'a>(src: &mut Cursor) -> Result { +fn get_line(src: &mut Cursor) -> Result { // Scan the bytes directly let start = src.position() as usize; // Scan to the second to last byte @@ -240,9 +238,7 @@ fn get_line<'a>(src: &mut Cursor) -> Result { } #[inline] -fn get_line_mut_no_return<'a>( - src: &mut Cursor<&BytesMut>, -) -> Result<(), Error> { +fn get_line_mut_no_return(src: &mut Cursor<&BytesMut>) -> Result<(), Error> { // Scan the bytes directly let start = src.position() as usize; // Scan to the second to last byte @@ -260,8 +256,8 @@ fn get_line_mut_no_return<'a>( } #[inline] -fn get_line_mut<'a>( - src: &'a mut Cursor<&BytesMut>, +fn get_line_mut( + src: &mut Cursor<&BytesMut>, ) -> Result, Error> { // Scan the bytes directly let start = src.position() as usize; diff --git a/app/roster/src/application/server/handle.rs b/app/roster/src/application/server/handle.rs index be2ae86..9c2d560 100644 --- a/app/roster/src/application/server/handle.rs +++ b/app/roster/src/application/server/handle.rs @@ -61,10 +61,10 @@ impl Handler { monoio::select! { r = accepting_frames_handle => { - return r; + r } r = answer_in_order_handle => { - return r; + r } } } diff --git a/app/roster/src/application/server/mod.rs b/app/roster/src/application/server/mod.rs index cad75e7..280c8f2 100644 --- a/app/roster/src/application/server/mod.rs +++ b/app/roster/src/application/server/mod.rs @@ -1,16 +1,11 @@ //! The whole redis server implementation is here. use std::net::SocketAddr; -use std::os::fd::AsRawFd; -use std::rc::Rc; use std::sync::atomic::AtomicU16; use std::sync::Arc; use std::thread::JoinHandle; -use std::time::Duration; use derive_builder::Builder; use monoio::net::{ListenerConfig, TcpListener}; -use thread_priority::{set_current_thread_priority, ThreadPriorityValue}; -use tracing::{error, info}; mod connection; mod context; @@ -28,6 +23,7 @@ use crate::domain; #[builder(pattern = "owned", setter(into, strip_option))] pub struct ServerConfig { bind_addr: SocketAddr, + #[allow(dead_code)] connections_limit: Arc, } @@ -94,7 +90,7 @@ impl ServerConfig { let (connection, r) = WriteConnection::new(conn, 4 * 1024); - let mut handler = Handler { + let handler = Handler { connection, connection_r: r, }; diff --git a/app/roster/src/domain/storage/mod.rs b/app/roster/src/domain/storage/mod.rs index 8c1d02e..26cbb87 100644 --- a/app/roster/src/domain/storage/mod.rs +++ b/app/roster/src/domain/storage/mod.rs @@ -2,13 +2,12 @@ use std::hash::BuildHasherDefault; use std::rc::Rc; -use std::sync::atomic::{AtomicU16, AtomicU32}; -use std::time::SystemTime; +use std::sync::atomic::AtomicU32; use bytes::Bytes; use bytestring::ByteString; use coarsetime::Instant; -use rustc_hash::{FxHashMap, FxHasher}; +use rustc_hash::FxHasher; use scc::HashMap; // We disallow Send just to be sure @@ -23,7 +22,7 @@ pub struct StorageValue { /// Storage #[derive(Default, Debug, Clone)] pub struct Storage { - db: Rc>, + db: Rc>>, count: Rc, } @@ -43,8 +42,6 @@ impl Storage { 4096, Default::default(), )), - // db: HashMap>, count: Rc::new(AtomicU32::new(0)), } } @@ -86,39 +83,6 @@ impl Storage { } } - /// Set a key - pub fn set( - &self, - key: ByteString, - val: Bytes, - opt: SetOptions, - ) -> Result, (String, StorageValue)> { - let val = StorageValue { - expired: opt.expired, - val, - }; - - let old = self - .count - .fetch_add(1, std::sync::atomic::Ordering::Relaxed); - // Simulate some eviction mechanisme when we have too many keys - if old > 400_000 { - // dbg!("remove"); - // TODO: If the RC is for the DB instead, we could have a spawn from - // monoio for this task instead, it would save us some - // time for the p99.9 - self.db.retain(|_, _| false); - self.count.swap(0, std::sync::atomic::Ordering::Relaxed); - } - - if let Err((key, val)) = self.db.insert(key, val) { - let old = self.db.update(&key, |_, _| val); - Ok(old) - } else { - Ok(None) - } - } - /// Get a key /// /// Return None if it doesn't exist diff --git a/app/roster/src/infrastructure/instruments/mod.rs b/app/roster/src/infrastructure/instruments/mod.rs index e48f469..8ca3e31 100644 --- a/app/roster/src/infrastructure/instruments/mod.rs +++ b/app/roster/src/infrastructure/instruments/mod.rs @@ -7,6 +7,7 @@ use tracing_subscriber::{self}; #[must_use] pub struct Instruments {} +#[allow(dead_code)] impl Instruments { /// Create a new `Instruments` stack and register it globally. pub fn new() -> anyhow::Result { diff --git a/app/roster/src/main.rs b/app/roster/src/main.rs index d98b9cf..510aa64 100644 --- a/app/roster/src/main.rs +++ b/app/roster/src/main.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use application::server::ServerConfigBuilder; use infrastructure::config::Cfg; -use infrastructure::instruments::Instruments; +// use infrastructure::instruments::Instruments; fn main() -> anyhow::Result<()> { // Initialize config