-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Allow substrate-based chains to define their own rpc middleware #9458
Comments
Would you be open to create a pr? |
Yes :) |
I think you can create one and then we can discuss what needs to be done. |
@librelois I'm late to the game here, but do you mind elaborating a bit more on what your use-case for this is? "improved logging" is a bit vague. Do you have a link to your fork I can look at? |
We use this fork to log the execution time of a request: https://github.com/purestake/substrate/tree/elois-custom-rpc-middleware We are still using an old version of substrate, but rather than updating our fork, we would prefer that the PR #9466 is finally merged, so we don't have to maintain a fork. |
There was work in that area merged quite recently, #9358. Maybe that is useful to you? |
@librelois isn't the linked pr from @dvdplm exactly doing what you want? |
@bkchr and @dvdplm , the change we are trying to introduce here is for multiple reason. The possibility to track the timing of an RPC is one of them, but we also want to change the logging mechanism and possibly tweak how we handle the rpc request. (For ex, we can't use the |
@dvdplm informed me yesterday that with jsonrpsee (the new jsonrpc backend) the possibility to define a Middleware is not available anymore afaiu. So, your pr would be removed again in the near future. @crystalin could you maybe tell us what exactly you need and we will help you to get this kind of information? 🙂 |
@bkchr I see. We will look into jsonrpsee. In addition to that, I wanted to experiment with a better request handler where the load would be spread across the RPC threads and not associated with the connection. Otherwise it leads to some request being computed very fast but being kept for a long time before being sent back if the thread is busy with another request. Even if the other RPC threads are totally free. I Those are the 2 main requirements I had for implementing the rpc middleware |
Now is a great time to be involved as the feature set isn't frozen and we are actively looking for input from the public. Any level of participation is very much appreciated (PRs, issues, docs, examples, ideas and comments). Pre-amble. I think that middleware is one of those features that look good on paper but end up being less useful in practice. The idea that "anyone" can add their own code to filter/modify/do whetever to requests is often false: the middleware author needs to know a fair amount of the internal details anyway. Conversely, in cases where a middleware is actually useful they are so useful that 99% users need it, in which case it could just as well be a feature. Now to the specifics here.
|
@dvdplm Thank you for replying. Even in our own parachain, the different roles of our nodes (normal rpc nodes, "tracing" rpc nodes) already makes us trying to customize the rpc handler to behave differently. Concerning the thread scheduling, I believe it can be improved in the library itself. On the jsonrpc one, each connection made to the server is associated to one of the given thread (We recently added the support to customize the number of threads), given if the thread is free or not. However this scenario makes it ineficient:
This is one of the multiple scenarios that are highly inefficient with this threading logic to associate with connection. Concerning jsonrpsee where can we start (ticket, PR, ??) to add ideas and maybe work ? |
Thanks @dvdplm :) I think a universal solution is better than specific solutions, and more in line with the philosophy of susbtrate. I agree that the API should be as minimal as possible to ease maintenance, but I think it's possible to create a middleware with the most flexible API possible, inspired by actix or warp API for example. I could be interested to work on this with you :) We can at least explore different possible APIs and see if we can make something minimal and easy to maintain. |
This should never be the case, AFAIK IO is non-blocking in |
Ideally I agree, but in the described scenario, It is the json formatter (which is part of the rpc thread) taking 10s to process all the data (there is a lot !) for tracing requests. There is also time spent in sending the data but I don't remember if it was the same thread It probably cost too much to put on a separate thread for 99% of the requests, but is worth for those tracing ones. |
@crystalin that's a relatively easy to solve problem without any need for a middleware. We were already contemplating adding support for blocking class in Sidenote: we don't pass JSON through |
jsonrpc-core
allows to define a custom middleware but substrate has already one and it doesn't seem possible to add a second one.For substrate-based projects to be able to customize, for example, the way RPC requests are logged, the substrate middleware would have to be generic in order to be able to handle an optional "sub-middleware".
We have this need at moonbeam, and are forced to maintain a fork of
sc-rpc-server
for this, this is not a sustainable solution, that's why I propose to contribute to substrate to allow this, I'm waiting for an agreement in principle before doing so :)The text was updated successfully, but these errors were encountered: