Skip to content

Commit

Permalink
Leaked db pointers to satisfy static lifetime demand by tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
sparky8251 committed Jan 24, 2024
1 parent 085fe79 commit ae6facb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::services::matrix::listener::MatrixListener;
use crate::services::matrix::responder::MatrixResponder;
use crate::services::webhook::listener::WebhookListener;
use anyhow::Context;
use native_db::DatabaseBuilder;
use native_db::{Database, DatabaseBuilder};
use std::env;
use std::path::PathBuf;
use tokio::signal::unix::{signal, SignalKind};
Expand All @@ -23,7 +23,7 @@ pub async fn init() -> anyhow::Result<()> {
Err(_) => ["database.nativedb"].iter().collect::<PathBuf>(),
};

let mut builder = DatabaseBuilder::new();
let mut builder = Box::new(DatabaseBuilder::new());
//load models
builder
.define::<AccessToken>()
Expand All @@ -34,14 +34,17 @@ pub async fn init() -> anyhow::Result<()> {
builder
.define::<CorrectionTimeCooldown>()
.context("Unable to load correction time cooldown database model")?;
let static_builder: &'static DatabaseBuilder = Box::leak(builder);
//open db
let db = builder
.create(&path)
.with_context(|| format!("Unable to create/open db {}", &path.display()))?;

let db = Box::new(
static_builder
.create(&path)
.with_context(|| format!("Unable to create/open db {}", &path.display()))?,
);
let static_db: &'static Database = Box::leak(db);
// fetch access_token

let r = db
let r = static_db
.r_transaction()
.context("Unable to get read transaction from db")?;
let access_token = match r
Expand All @@ -68,7 +71,7 @@ pub async fn init() -> anyhow::Result<()> {
.await?;

// Save returned session
let rw = db
let rw = static_db
.rw_transaction()
.context("Unable to get read transaction from db")?;
trace!("Session retrieved, saving session data...");
Expand All @@ -94,7 +97,7 @@ pub async fn init() -> anyhow::Result<()> {
let webhook_tx = matrix_tx.clone();

// Create thread structures
let mut matrix_listener = MatrixListener::new(&config, matrix_tx, &db)?;
let mut matrix_listener = MatrixListener::new(&config, matrix_tx, &static_db)?;
let mut matrix_responder = MatrixResponder::new(matrix_rx)?;
let webhook_listener = WebhookListener::new(&config, webhook_tx);

Expand Down
2 changes: 1 addition & 1 deletion src/services/matrix/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'a> MatrixListener<'a> {
pub fn new(
config: &Config,
send: Sender<MatrixMessage>,
storage: &Database<'a>,
storage: &'a Database<'a>,
) -> anyhow::Result<MatrixListener<'a>> {
let config = MatrixListenerConfig::new(config);
let api_client = reqwest::Client::new();
Expand Down

0 comments on commit ae6facb

Please sign in to comment.