Skip to content

Commit

Permalink
fix: Fix Rust clippy warnings (#458)
Browse files Browse the repository at this point in the history
* Fix clippy warnings

Signed-off-by: Li Zhanhui <[email protected]>

* Add doc for Producer

Signed-off-by: Li Zhanhui <[email protected]>

---------

Signed-off-by: Li Zhanhui <[email protected]>
  • Loading branch information
lizhanhui committed Apr 4, 2023
1 parent 33c069f commit 90231bd
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
7 changes: 4 additions & 3 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -23,6 +23,7 @@ edition = "2021"
tokio = { version = "1", features = ["full"] }
tokio-rustls = {version = "0.24.0", features = ["default", "dangerous_configuration"] }
async-trait = "0.1.68"
lazy_static = "1.4"
tonic = {version = "0.9.0", features = ["tls", "default", "channel", "tls-roots"]}
prost = "0.11.8"
prost-types = "0.11.8"
Expand Down Expand Up @@ -50,4 +51,4 @@ tonic-build = "0.9.0"

[dev-dependencies]
wiremock-grpc = "0.0.3-alpha2"
futures = "0.3"
futures = "0.3"
20 changes: 16 additions & 4 deletions rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ use crate::pb::{
};
use crate::session::{RPCClient, Session, SessionManager};

pub trait Foo {}

#[derive(Debug)]
pub(crate) struct Client {
logger: Logger,
option: ClientOption,
Expand All @@ -39,7 +38,9 @@ pub(crate) struct Client {
endpoints: Endpoints,
}

static CLIENT_ID_SEQUENCE: AtomicUsize = AtomicUsize::new(0);
lazy_static::lazy_static! {
static ref CLIENT_ID_SEQUENCE: AtomicUsize = AtomicUsize::new(0);
}

impl Client {
const OPERATION_CLIENT_NEW: &'static str = "client.new";
Expand All @@ -50,7 +51,7 @@ impl Client {
let id = Self::generate_client_id();
let endpoints = Endpoints::from_access_url(option.access_url().to_string())
.map_err(|e| e.with_operation(Self::OPERATION_CLIENT_NEW))?;
let session_manager = SessionManager::new(&logger, id.clone(), &option);
let session_manager = SessionManager::new(logger, id.clone(), &option);
Ok(Client {
logger: logger.new(o!("component" => "client")),
option,
Expand Down Expand Up @@ -250,12 +251,23 @@ impl Client {

#[cfg(test)]
mod tests {
use std::sync::atomic::Ordering;

use crate::client::Client;
use crate::conf::ClientOption;
use crate::log::terminal_logger;
use crate::pb::{Code, MessageQueue, QueryRouteResponse, Resource, Status};
use crate::session;

use super::CLIENT_ID_SEQUENCE;

#[test]
fn test_client_id_sequence() {
let v1 = CLIENT_ID_SEQUENCE.fetch_add(1, Ordering::Relaxed);
let v2 = CLIENT_ID_SEQUENCE.fetch_add(1, Ordering::Relaxed);
assert!(v2 > v1, "Client ID sequence should be increasing");
}

#[tokio::test]
async fn client_query_route() {
let logger = terminal_logger();
Expand Down
7 changes: 6 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ mod log;

mod client;
mod model;

#[allow(clippy::all)]
#[path = "pb/apache.rocketmq.v2.rs"]
mod pb;
mod session;

mod producer;
pub(crate) mod producer;

// Export structs that are part of crate API.
pub use producer::Producer;
1 change: 1 addition & 0 deletions rust/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub(crate) enum RouteStatus {
Found(Arc<Route>),
}

#[derive(Debug)]
pub(crate) struct Endpoints {
access_url: String,
scheme: AddressScheme,
Expand Down
13 changes: 11 additions & 2 deletions rust/src/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use slog::{info, Logger};

//! Publish messages of various types to brokers.
//!
//! `Producer` is a thin wrapper of internal `Client` struct that shoulders the actual workloads.
//! Most of its methods take shared reference so that application developers may use it at will.

use crate::client::Client;
use crate::conf::{ClientOption, ProducerOption};
use crate::error::ClientError;
use crate::log;
use crate::pb::{Message, SendResultEntry};
use slog::{info, Logger};

struct Producer {
/// `Producer` is the core struct, to which application developers should turn, when publishing messages to brokers.
///
/// `Producer` is `Send` and `Sync` by design, so that developers may get started easily.
#[derive(Debug)]
pub struct Producer {
option: ProducerOption,
logger: Logger,
client: Client,
Expand Down
8 changes: 4 additions & 4 deletions rust/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Session {
client_id: String,
option: &ClientOption,
) -> Result<Self, ClientError> {
let peer = endpoints.access_url().clone();
let peer = endpoints.access_url().to_owned();
debug!(logger, "creating session, peer={}", peer);

let mut channel_endpoints = Vec::new();
Expand All @@ -78,7 +78,7 @@ impl Session {
"No endpoint available.".to_string(),
Self::OPERATION_CREATE,
)
.with_context("peer", peer));
.with_context("peer", peer.clone()));
}

let channel = if channel_endpoints.len() == 1 {
Expand All @@ -89,7 +89,7 @@ impl Session {
Self::OPERATION_CREATE,
)
.set_source(e)
.with_context("peer", peer)
.with_context("peer", peer.clone())
})?
} else {
Channel::balance_list(channel_endpoints.into_iter())
Expand All @@ -104,7 +104,7 @@ impl Session {
);

Ok(Session {
logger: logger.new(o!("component" => "session", "peer" => peer.to_owned())),
logger: logger.new(o!("component" => "session", "peer" => peer.clone())),
client_id,
stub,
})
Expand Down

0 comments on commit 90231bd

Please sign in to comment.