Skip to content

Commit

Permalink
chore: cargo clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ilbertt committed Mar 29, 2024
1 parent 5fe37ff commit 339beb4
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/canister-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,5 @@ pub async fn ws_get_messages(
.await
.expect("Failed to read HTTP response");

Decode!(&res, CanisterOutputCertifiedMessages).map_err(|e| IcError::Candid(e))
Decode!(&res, CanisterOutputCertifiedMessages).map_err(IcError::Candid)
}
8 changes: 7 additions & 1 deletion src/gateway-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ pub struct GatewayState {
inner: Arc<GatewayStateInner>,
}

impl Default for GatewayState {
fn default() -> Self {
Self::new()
}
}

impl GatewayState {
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -199,7 +205,7 @@ impl GatewayState {
///
/// This function shall be called only if it is guaranteed that the canister entry exists in the gateway state.
pub fn remove_failed_canister(&self, canister_id: CanisterPrincipal) {
if let None = self.inner.data.remove(&canister_id) {
if self.inner.data.remove(&canister_id).is_none() {
unreachable!("failed canister not found in gateway state");
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/ic-websocket-gateway/src/canister_poller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,11 @@ impl CanisterPoller {
PollingStatus::MessagesPolled(certified_canister_output) => {
let relay_messages_span =
span!(parent: &Span::current(), Level::TRACE, "Relay Canister Messages");
let end_of_queue_reached = {
match certified_canister_output.is_end_of_queue {
Some(is_end_of_queue_reached) => is_end_of_queue_reached,
// if 'is_end_of_queue' is None, the CDK version is < 0.3.1 and does not have such a field
// in this case, assume that the queue is fully drained and therefore will be polled again
// after waiting for 'polling_interval_ms'
None => true,
}
};
// if 'is_end_of_queue' is None, the CDK version is < 0.3.1 and does not have such a field
// in this case, assume that the queue is fully drained and therefore will be polled again
// after waiting for 'polling_interval_ms'
let end_of_queue_reached =
certified_canister_output.is_end_of_queue.unwrap_or(true);
self.update_nonce(&certified_canister_output)?;
// relaying of messages cannot be done in a separate task for each polling iteration
// as they might interleave and break the correct ordering of messages
Expand Down Expand Up @@ -233,7 +229,7 @@ impl CanisterPoller {
trace!("Start relaying message",);
(canister_to_client_message, Span::current())
});
relay_message(canister_message, &client_channel_tx)
relay_message(canister_message, client_channel_tx)
.instrument(canister_message_span)
.await;
relayed_messages_count += 1;
Expand Down Expand Up @@ -340,7 +336,7 @@ async fn relay_message(
}
}

pub fn get_nonce_from_message(key: &String) -> Result<u64, String> {
pub fn get_nonce_from_message(key: &str) -> Result<u64, String> {
if let Some(message_nonce_str) = key.split('_').last() {
let message_nonce = message_nonce_str
.parse()
Expand Down
12 changes: 6 additions & 6 deletions src/ic-websocket-gateway/src/client_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ impl<S: AsyncRead + AsyncWrite + Unpin> ClientSession<S> {
// replace the field with the canister_id received in the first envelope
// this shall not be updated anymore
// if canister_id is already set in the struct, we return an error as inspect_ic_ws_open_message shall only be called once
if !self.canister_id.replace(canister_id.clone()).is_none()
|| !self.client_key.replace(client_key.clone()).is_none()
if self.canister_id.replace(canister_id).is_some()
|| self.client_key.replace(client_key.clone()).is_some()
{
// if the canister_id or client_key field was already set,
// it means that the client sent the WS open message twice,
Expand All @@ -303,10 +303,10 @@ impl<S: AsyncRead + AsyncWrite + Unpin> ClientSession<S> {
// in case of other errors, we report them and terminate the connection handler task
Err(e) => {
self.close_ws_session().await?;
return Err(IcWsError::IcWsProtocol(format!(
Err(IcWsError::IcWsProtocol(format!(
"IC WS setup failed. Error: {:?}",
e
)));
)))
},
}
}
Expand Down Expand Up @@ -361,7 +361,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin> ClientSession<S> {
let canister_id = self.canister_id.expect("must be set");

// relay the envelope to the IC
self.relay_envelope_to_canister(serialized_envelope, canister_id.clone())
self.relay_envelope_to_canister(serialized_envelope, canister_id)
.await
.map_err(|e| IcWsError::IcWsProtocol(e.to_string()))?;

Expand All @@ -385,7 +385,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin> ClientSession<S> {
self.agent
.update_signed(canister_id, serialized_envelope)
.await?;
return Ok(());
Ok(())
}

async fn handle_open_transition(
Expand Down
2 changes: 1 addition & 1 deletion src/ic-websocket-gateway/src/client_session_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl ClientSessionHandler {
// call ws_close so that the client is removed from the canister
if let Err(e) = ws_close(
&self.agent,
&canister_id,
canister_id,
CanisterWsCloseArguments { client_key },
)
.await
Expand Down
4 changes: 2 additions & 2 deletions src/ic-websocket-gateway/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ impl Manager {
// creates a concurrent hashmap with capacity of 32 divided in shards so that each entry can be accessed concurrently without locking the whole state
let state: GatewayState = GatewayState::new();

return Self {
Self {
agent,
address: gateway_address,
state,
};
}
}

pub fn get_agent_principal(&self) -> Principal {
Expand Down
6 changes: 1 addition & 5 deletions src/ic-websocket-gateway/src/ws_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,7 @@ impl WsListener {
let polling_interval_ms = self.polling_interval_ms;
// spawn a session handler task for each incoming client connection

let start = self
.clients_connection_time
.get(&client_id)
.unwrap()
.clone();
let start = *self.clients_connection_time.get(&client_id).unwrap();

tokio::spawn(
async move {
Expand Down
12 changes: 5 additions & 7 deletions tests/src/test_canister_rs/src/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ pub struct AppMessage {

impl AppMessage {
fn candid_serialize(&self) -> Vec<u8> {
encode_one(&self).unwrap()
encode_one(self).unwrap()
}
}

pub fn on_open(args: OnOpenCallbackArgs) {
// add client to the list of connected clients
CLIENTS_CONNECTED.with(|clients_connected| {
clients_connected
.borrow_mut()
.insert(args.client_principal.clone());
clients_connected.borrow_mut().insert(args.client_principal);

print(format!(
"[on_open] # clients connected: {}",
Expand All @@ -46,16 +44,16 @@ pub fn on_message(args: OnMessageCallbackArgs) {
text: app_msg.clone().text + " ping",
timestamp: time(),
};
print(format!("[on_message] Received message"));
print("[on_message] Received message");
send_app_message(args.client_principal, new_msg)
}

fn send_app_message(client_key: ClientPrincipal, msg: AppMessage) {
print(format!("Sending message"));
print("Sending message");
if let Err(e) = send(client_key, msg.candid_serialize()) {
print(format!("Could not send message: {}", e));
}
print(format!("Message sent"));
print("Message sent");
}

pub fn on_close(args: OnCloseCallbackArgs) {
Expand Down
3 changes: 2 additions & 1 deletion tests/src/test_canister_rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ fn ws_get_messages(args: CanisterWsGetMessagesArguments) -> CanisterWsGetMessage
ic_websocket_cdk::ws_get_messages(args)
}

//// Debug/tests methods
// Debug/tests methods

// send a message to the client, usually called by the canister itself
#[update]
fn send(client_key: ClientPrincipal, msg_bytes: Vec<u8>) -> CanisterSendResult {
Expand Down

0 comments on commit 339beb4

Please sign in to comment.