Use spawn_blocking for dry-run requests #761
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fixes: #752
Moves dry_run to a tokio::spawn_blocking call
Since the executor is mostly blocking code, issuing too many dry-run requests could impact more critical functions of the node (such as block production) if executed directly on the main worker pool.
Limits the number of concurrent dry_run requests by the CPU count
This is because in the worst case we will be CPU bound. While some txs may be state-access heavy and I/O bound, other txs could be CPU bound by using lots of crypto ops like hashing and ecrecover. To ensure we don't grind the system to a halt with hundreds of CPU intensive blocking threads, semaphore permits are used to rate limit the number of dry_run requests.
Removes async from the executor
Previously the executor methods were marked as async, even though there were no async calls. This posed a risk when calling the executor within spawn_blocking, as spawn_blocking is a sync context and must use
block_on
to call any futures. Usingblock_on
is risky because it can easily lead to deadlocks when using async code. While block_on likely would've been safe to use in this one case, it could be the source of difficult-to-debug issues if more asynchronous code is added to the executor in the future.