Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(torii): max conns to tasks & dont use sqlite acquire #2911

Merged
merged 2 commits into from
Jan 15, 2025
Merged
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
7 changes: 5 additions & 2 deletions crates/torii/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ impl Runner {
options = options.journal_mode(SqliteJournalMode::Wal);
options = options.synchronous(SqliteSynchronous::Normal);

let pool =
SqlitePoolOptions::new().min_connections(1).connect_with(options.clone()).await?;
let pool = SqlitePoolOptions::new()
.min_connections(1)
.max_connections(self.args.indexing.max_concurrent_tasks as u32)
.connect_with(options.clone())
.await?;

let readonly_options = options.read_only(true);
let readonly_pool = SqlitePoolOptions::new()
Expand Down
13 changes: 2 additions & 11 deletions crates/torii/sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use dojo_types::schema::{Struct, Ty};
use dojo_world::config::WorldMetadata;
use dojo_world::contracts::abigen::model::Layout;
use dojo_world::contracts::naming::compute_selector_from_names;
use sqlx::pool::PoolConnection;
use sqlx::{Pool, Sqlite};
use starknet::core::types::{Event, Felt, InvokeTransaction, Transaction};
use starknet_crypto::poseidon_hash_many;
Expand Down Expand Up @@ -173,18 +172,17 @@ impl Sql {
}

pub async fn cursors(&self) -> Result<Cursors> {
let mut conn: PoolConnection<Sqlite> = self.pool.acquire().await?;
let cursors = sqlx::query_as::<_, (String, String)>(
"SELECT contract_address, last_pending_block_contract_tx FROM contracts WHERE \
last_pending_block_contract_tx IS NOT NULL",
)
.fetch_all(&mut *conn)
.fetch_all(&self.pool)
.await?;

let (head, last_pending_block_tx) = sqlx::query_as::<_, (Option<i64>, Option<String>)>(
"SELECT head, last_pending_block_tx FROM contracts WHERE 1=1",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curiosity, why this 1=1 here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Acquire means you need to handle closing the connection yourself etc.. passing the pool directly will manage acquiring and cleaning it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Curiosity, why this 1=1 here?

idk

)
.fetch_one(&mut *conn)
.fetch_one(&self.pool)
.await?;

let head = head.map(|h| h.try_into().expect("doesn't fit in u64"));
Expand Down Expand Up @@ -514,13 +512,6 @@ impl Sql {
self.model_cache.model(&selector).await.map_err(|e| e.into())
}

pub async fn entities(&self, model: String) -> Result<Vec<Vec<Felt>>> {
let query = sqlx::query_as::<_, (i32, String, String)>("SELECT * FROM ?").bind(model);
let mut conn: PoolConnection<Sqlite> = self.pool.acquire().await?;
let mut rows = query.fetch_all(&mut *conn).await?;
Ok(rows.drain(..).map(|row| serde_json::from_str(&row.2).unwrap()).collect())
}

pub fn store_transaction(
&mut self,
transaction: &Transaction,
Expand Down
Loading