mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-21 08:04:26 +01:00
rs: Run hooks, methods and notification handlers in tokio tasks
Changelog-Changed: cln-plugin: Hooks, notifications and RPC methods now run asynchronously allowing for re-entrant handlers
This commit is contained in:
committed by
ShahanaFarooqui
parent
db3707f957
commit
f69da84256
@@ -11,8 +11,8 @@ use std::str::FromStr;
|
|||||||
use std::{io, str};
|
use std::{io, str};
|
||||||
use tokio_util::codec::{Decoder, Encoder};
|
use tokio_util::codec::{Decoder, Encoder};
|
||||||
|
|
||||||
use crate::messages::{Notification, Request};
|
|
||||||
use crate::messages::JsonRpc;
|
use crate::messages::JsonRpc;
|
||||||
|
use crate::messages::{Notification, Request};
|
||||||
|
|
||||||
/// A simple codec that parses messages separated by two successive
|
/// A simple codec that parses messages separated by two successive
|
||||||
/// `\n` newlines.
|
/// `\n` newlines.
|
||||||
|
|||||||
@@ -507,7 +507,7 @@ where
|
|||||||
|
|
||||||
impl<S> PluginDriver<S>
|
impl<S> PluginDriver<S>
|
||||||
where
|
where
|
||||||
S: Send + Clone,
|
S: Send + Clone + Sync,
|
||||||
{
|
{
|
||||||
/// Run the plugin until we get a shutdown command.
|
/// Run the plugin until we get a shutdown command.
|
||||||
async fn run<I, O>(
|
async fn run<I, O>(
|
||||||
@@ -554,14 +554,32 @@ where
|
|||||||
Some(Ok(msg)) => {
|
Some(Ok(msg)) => {
|
||||||
trace!("Received a message: {:?}", msg);
|
trace!("Received a message: {:?}", msg);
|
||||||
match msg {
|
match msg {
|
||||||
messages::JsonRpc::Request(id, p) => {
|
messages::JsonRpc::Request(_id, _p) => {
|
||||||
PluginDriver::<S>::dispatch_request(id, p, plugin).await
|
todo!("This is unreachable until we start filling in messages:Request. Until then the custom dispatcher below is used exclusively.");
|
||||||
}
|
}
|
||||||
messages::JsonRpc::Notification(n) => {
|
messages::JsonRpc::Notification(_n) => {
|
||||||
self.dispatch_notification(n, plugin).await
|
todo!("As soon as we define the full structure of the messages::Notification we'll get here. Until then the custom dispatcher below is used.")
|
||||||
}
|
}
|
||||||
messages::JsonRpc::CustomRequest(id, p) => {
|
messages::JsonRpc::CustomRequest(id, request) => {
|
||||||
match self.dispatch_custom_request(id.clone(), p, plugin).await {
|
trace!("Dispatching custom method {:?}", request);
|
||||||
|
let method = request
|
||||||
|
.get("method")
|
||||||
|
.context("Missing 'method' in request")?
|
||||||
|
.as_str()
|
||||||
|
.context("'method' is not a string")?;
|
||||||
|
let callback = self.rpcmethods.get(method).with_context(|| {
|
||||||
|
anyhow!("No handler for method '{}' registered", method)
|
||||||
|
})?;
|
||||||
|
let params = request
|
||||||
|
.get("params")
|
||||||
|
.context("Missing 'params' field in request")?
|
||||||
|
.clone();
|
||||||
|
|
||||||
|
let plugin = plugin.clone();
|
||||||
|
let call = callback(plugin.clone(), params);
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
match call.await {
|
||||||
Ok(v) => plugin
|
Ok(v) => plugin
|
||||||
.sender
|
.sender
|
||||||
.send(json!({
|
.send(json!({
|
||||||
@@ -570,7 +588,7 @@ where
|
|||||||
"result": v
|
"result": v
|
||||||
}))
|
}))
|
||||||
.await
|
.await
|
||||||
.context("returning custom result"),
|
.context("returning custom response"),
|
||||||
Err(e) => plugin
|
Err(e) => plugin
|
||||||
.sender
|
.sender
|
||||||
.send(json!({
|
.send(json!({
|
||||||
@@ -581,9 +599,29 @@ where
|
|||||||
.await
|
.await
|
||||||
.context("returning custom error"),
|
.context("returning custom error"),
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
messages::JsonRpc::CustomNotification(n) => {
|
messages::JsonRpc::CustomNotification(request) => {
|
||||||
self.dispatch_custom_notification(n, plugin).await
|
trace!("Dispatching custom notification {:?}", request);
|
||||||
|
let method = request
|
||||||
|
.get("method")
|
||||||
|
.context("Missing 'method' in request")?
|
||||||
|
.as_str()
|
||||||
|
.context("'method' is not a string")?;
|
||||||
|
let callback = self.subscriptions.get(method).with_context(|| {
|
||||||
|
anyhow!("No handler for notification '{}' registered", method)
|
||||||
|
})?;
|
||||||
|
let params = request
|
||||||
|
.get("params")
|
||||||
|
.context("Missing 'params' field in request")?
|
||||||
|
.clone();
|
||||||
|
|
||||||
|
let plugin = plugin.clone();
|
||||||
|
let call = callback(plugin.clone(), params);
|
||||||
|
|
||||||
|
tokio::spawn(async move { call.await.unwrap() });
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -591,85 +629,6 @@ where
|
|||||||
None => Err(anyhow!("Error reading from master")),
|
None => Err(anyhow!("Error reading from master")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn dispatch_request(
|
|
||||||
_id: serde_json::Value,
|
|
||||||
_request: messages::Request,
|
|
||||||
_plugin: &Plugin<S>,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
todo!("This is unreachable until we start filling in messages:Request. Until then the custom dispatcher below is used exclusively.")
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn dispatch_notification(
|
|
||||||
&self,
|
|
||||||
_notification: messages::Notification,
|
|
||||||
_plugin: &Plugin<S>,
|
|
||||||
) -> Result<(), Error>
|
|
||||||
where
|
|
||||||
S: Send + Clone,
|
|
||||||
{
|
|
||||||
todo!("As soon as we define the full structure of the messages::Notification we'll get here. Until then the custom dispatcher below is used.")
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn dispatch_custom_request(
|
|
||||||
&self,
|
|
||||||
_id: serde_json::Value,
|
|
||||||
request: serde_json::Value,
|
|
||||||
plugin: &Plugin<S>,
|
|
||||||
) -> Result<serde_json::Value, Error> {
|
|
||||||
let method = request
|
|
||||||
.get("method")
|
|
||||||
.context("Missing 'method' in request")?
|
|
||||||
.as_str()
|
|
||||||
.context("'method' is not a string")?;
|
|
||||||
|
|
||||||
let params = request
|
|
||||||
.get("params")
|
|
||||||
.context("Missing 'params' field in request")?;
|
|
||||||
let callback = self
|
|
||||||
.rpcmethods
|
|
||||||
.get(method)
|
|
||||||
.with_context(|| anyhow!("No handler for method '{}' registered", method))?;
|
|
||||||
|
|
||||||
trace!(
|
|
||||||
"Dispatching custom request: method={}, params={}",
|
|
||||||
method,
|
|
||||||
params
|
|
||||||
);
|
|
||||||
callback(plugin.clone(), params.clone()).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn dispatch_custom_notification(
|
|
||||||
&self,
|
|
||||||
notification: serde_json::Value,
|
|
||||||
plugin: &Plugin<S>,
|
|
||||||
) -> Result<(), Error>
|
|
||||||
where
|
|
||||||
S: Send + Clone,
|
|
||||||
{
|
|
||||||
trace!("Dispatching custom notification {:?}", notification);
|
|
||||||
let method = notification
|
|
||||||
.get("method")
|
|
||||||
.context("Missing 'method' in notification")?
|
|
||||||
.as_str()
|
|
||||||
.context("'method' is not a string")?;
|
|
||||||
let params = notification
|
|
||||||
.get("params")
|
|
||||||
.context("Missing 'params' field in notification")?;
|
|
||||||
let callback = self
|
|
||||||
.subscriptions
|
|
||||||
.get(method)
|
|
||||||
.with_context(|| anyhow!("No handler for method '{}' registered", method))?;
|
|
||||||
trace!(
|
|
||||||
"Dispatching custom request: method={}, params={}",
|
|
||||||
method,
|
|
||||||
params
|
|
||||||
);
|
|
||||||
if let Err(e) = callback(plugin.clone(), params.clone()).await {
|
|
||||||
log::error!("Error in notification handler '{}': {}", method, e);
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> Plugin<S>
|
impl<S> Plugin<S>
|
||||||
|
|||||||
@@ -36,8 +36,6 @@ impl Value {
|
|||||||
/// return the integer value.
|
/// return the integer value.
|
||||||
pub fn is_i64(&self) -> bool {
|
pub fn is_i64(&self) -> bool {
|
||||||
self.as_i64().is_some()
|
self.as_i64().is_some()
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If the `Value` is an integer, represent it as i64. Returns
|
/// If the `Value` is an integer, represent it as i64. Returns
|
||||||
|
|||||||
@@ -249,10 +249,6 @@ def test_grpc_wrong_auth(node_factory):
|
|||||||
stub.Getinfo(nodepb.GetinfoRequest())
|
stub.Getinfo(nodepb.GetinfoRequest())
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.xfail(
|
|
||||||
reason="Times out because we can't call the RPC method while currently holding on to HTLCs",
|
|
||||||
strict=True,
|
|
||||||
)
|
|
||||||
def test_cln_plugin_reentrant(node_factory, executor):
|
def test_cln_plugin_reentrant(node_factory, executor):
|
||||||
"""Ensure that we continue processing events while already handling.
|
"""Ensure that we continue processing events while already handling.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user