Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 77 additions & 28 deletions core/main/src/firebolt/rpc_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// SPDX-License-Identifier: Apache-2.0
//

use crate::tokio::sync::mpsc::Sender;
use futures::StreamExt;
use jsonrpsee::{
core::server::{
Expand All @@ -33,8 +34,9 @@ use ripple_sdk::{
extn::extn_client_message::ExtnMessage,
log::{debug, error, info},
service::service_message::{
Id as ServiceMessageId, JsonRpcMessage as ServiceJsonRpcMessage,
JsonRpcSuccess as ServiceJsonRpcSuccess, ServiceMessage,
Id as ServiceMessageId, JsonRpcError, JsonRpcErrorDetails,
JsonRpcMessage as ServiceJsonRpcMessage, JsonRpcSuccess as ServiceJsonRpcSuccess,
ServiceMessage,
},
tokio,
tokio_tungstenite::tungstenite::Message,
Expand Down Expand Up @@ -203,7 +205,6 @@ async fn resolve_route(
if let Some(api_stats) = platform_state.metrics.get_api_stats(&request_id) {
msg.stats = Some(api_stats);
}

return Ok(msg);
}
error!("Invalid output from method sink");
Expand Down Expand Up @@ -285,34 +286,68 @@ impl RpcRouter {
serde_json::from_str::<serde_json::Value>(msg.jsonrpc_msg.clone().as_str())
.unwrap();

let result = json_rpc_response.get("result").cloned().unwrap_or_default();
let jsonrpc = serde_json::to_string(
&json_rpc_response
.get("jsonrpc")
.cloned()
.unwrap_or_default(),
)
.unwrap();
let id = ServiceMessageId::String(msg.request_id.clone());
// Treat presence of 'result' key (even if null) as success
if json_rpc_response.get("result").is_some() {
let result = json_rpc_response.get("result").cloned().unwrap();
let jsonrpc = serde_json::to_string(
&json_rpc_response
.get("jsonrpc")
.cloned()
.unwrap_or_default(),
)
.unwrap();
let id = ServiceMessageId::String(msg.request_id.clone());

let service_message = ServiceMessage {
message: ServiceJsonRpcMessage::Success(ServiceJsonRpcSuccess {
result,
jsonrpc,
id,
}),
context: Some(
serde_json::to_value(req.ctx.clone()).unwrap_or_default(),
),
};
send_response(&service_id, &sender, &service_message);
} else if let Some(error) = json_rpc_response.get("error") {
if !error.is_null() {
let jsonrpc = serde_json::to_string(
&json_rpc_response
.get("jsonrpc")
.cloned()
.unwrap_or_default(),
)
.unwrap();
let id = ServiceMessageId::String(msg.request_id.clone());
let details = JsonRpcErrorDetails {
code: -32600,
message: "Ripple Main does not support this request from Service"
.to_string(),
data: Some(error.clone()),
};

let service_message = ServiceMessage {
message: ServiceJsonRpcMessage::Success(ServiceJsonRpcSuccess {
result,
jsonrpc,
id,
}),
context: Some(serde_json::to_value(req.ctx.clone()).unwrap_or_default()),
};
let msg_str = serde_json::to_string(&service_message).unwrap();
let message = Message::Text(msg_str.clone());
debug!("Sending response to service {}: {:?}", service_id, message);
if let Err(err) = sender.try_send(message) {
let service_message = ServiceMessage {
message: ServiceJsonRpcMessage::Error(JsonRpcError {
error: details,
jsonrpc,
id,
}),
context: Some(
serde_json::to_value(req.ctx.clone()).unwrap_or_default(),
),
};
send_response(&service_id, &sender, &service_message);
} else {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about event response ? We are currently handling only result and error use cases. Event notification will not have either of these.

error!(
"Received unexpected response from service {:?}",
json_rpc_response
);
}
} else {
error!(
"Failed to send request to service {}: {:?}",
service_id, err
"Received unexpected response from service {:?}",
json_rpc_response
);
} else {
debug!("Successfully sent request to service: {}", service_id);
}
} else {
error!(
Expand All @@ -333,3 +368,17 @@ impl RpcRouter {
});
}
}

fn send_response(service_id: &str, sender: &Sender<Message>, service_message: &ServiceMessage) {
let msg_str = serde_json::to_string(&service_message).unwrap();
let message = Message::Text(msg_str.clone());
debug!("Sending response to service {}: {:?}", service_id, message);
if let Err(err) = sender.try_send(message) {
error!(
"Failed to send request to service {}: {:?}",
service_id, err
);
} else {
debug!("Successfully sent request to service: {}", service_id);
}
}
4 changes: 2 additions & 2 deletions core/sdk/src/utils/ws_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::time::Duration;

use futures::stream::{SplitSink, SplitStream};
use futures_util::StreamExt;
use log::{error, info};
use log::{error, info, warn};
use tokio::net::TcpStream;
use tokio_tungstenite::{client_async, tungstenite::Message, WebSocketStream};

Expand Down Expand Up @@ -226,7 +226,7 @@ impl WebSocketUtils {
}
}
index += 1;
error!(
warn!(
"new Websocket TCP Connection with {} failed with retry for last {} millisecs in {}",
url_path,
delay_duration.as_millis(),
Expand Down