Skip to content

Commit

Permalink
perf(katana-rpc): spawn blocking tasks (#1456)
Browse files Browse the repository at this point in the history
Resolves #1448

Creates new crate under `katana`, `katana-tasks`, for managing spawning blocking tasks. RPC calls that mostly perform blocking tasks are now sent to their designated threadpools and won't block the async threads.

- `TokioTaskSpawner`: mainly for spawning blocking IO-bound tasks (ie reading from storage)
- `BlockingThreadPool`: mainly for spawning expensive CPU-bound tasks

Depends on #1455 because now the RPC requests (that used to block the thread before) have to wait for the blocking tasks to finish and thus may be idling for more than 2 seconds which will result in a connection timeout.

Doing `sozo migrate` on 2s timeout will failed when calling `/esimateFee` for estimating the World contract declare tx with error `connection closed before message completed`.

Raw error message from `sozo` against the new changes:
```console
Caused by:
    Failed to deploy world: Failed to migrate world: Migrator(Provider(Other(TransportError(Reqwest(reqwest::Error { kind: Request, url: Url { scheme: "http", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("localhost")), port: Some(5050), path: "/", query: None, fragment: None }, source: hyper::Error(IncompleteMessage) })))))
```
  • Loading branch information
kariy committed Jan 18, 2024
1 parent 2fbacf9 commit 0ef407a
Show file tree
Hide file tree
Showing 6 changed files with 660 additions and 352 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions crates/katana/core/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@ impl KatanaSequencer {
self.backend.chain_id
}

pub fn block_number(&self) -> BlockNumber {
BlockNumberProvider::latest_number(&self.backend.blockchain.provider()).unwrap()
pub fn block_number(&self) -> SequencerResult<BlockNumber> {
let num = BlockNumberProvider::latest_number(&self.backend.blockchain.provider())?;
Ok(num)
}

pub fn block_tx_count(&self, block_id: BlockIdOrTag) -> SequencerResult<Option<u64>> {
Expand Down Expand Up @@ -300,7 +301,7 @@ impl KatanaSequencer {
Ok(count)
}

pub async fn nonce_at(
pub fn nonce_at(
&self,
block_id: BlockIdOrTag,
contract_address: ContractAddress,
Expand Down Expand Up @@ -352,7 +353,7 @@ impl KatanaSequencer {
Ok(tx)
}

pub async fn events(
pub fn events(
&self,
from_block: BlockIdOrTag,
to_block: BlockIdOrTag,
Expand Down
1 change: 1 addition & 0 deletions crates/katana/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ katana-primitives = { path = "../primitives" }
katana-provider = { path = "../storage/provider" }
katana-rpc-types = { path = "rpc-types" }
katana-rpc-types-builder = { path = "rpc-types-builder" }
katana-tasks = { path = "../tasks" }

anyhow.workspace = true
cairo-lang-starknet = "2.3.1"
Expand Down
Loading

0 comments on commit 0ef407a

Please sign in to comment.