Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit 821fb27

Browse files
committed
Eliminate untracked singleton
1 parent 7c07ce5 commit 821fb27

File tree

4 files changed

+29
-36
lines changed

4 files changed

+29
-36
lines changed

server/bleep/src/db.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
1-
use std::{fs, path::Path};
1+
use std::{fs, path::Path, sync::Arc};
22

3-
use anyhow::{anyhow, Context, Result};
4-
use once_cell::sync::OnceCell;
3+
use anyhow::{Context, Result};
54
use sqlx::SqlitePool;
65
use tracing::{debug, warn};
76

87
use crate::Configuration;
98

10-
static POOL: OnceCell<SqlitePool> = OnceCell::new();
9+
pub type SqlDb = Arc<SqlitePool>;
1110

12-
pub async fn init(config: &Configuration) -> Result<()> {
11+
pub async fn init(config: &Configuration) -> Result<SqlitePool> {
1312
fs::create_dir_all(&config.data_dir)?;
1413
let data_dir = config.data_dir.to_string_lossy();
1514

16-
let pool = match connect(&data_dir).await {
17-
Ok(pool) => pool,
15+
match connect(&data_dir).await {
16+
Ok(pool) => Ok(pool),
1817
Err(e) => {
1918
warn!(
2019
?e,
2120
"encountered DB error while migrating, recreating database..."
2221
);
2322
reset(&data_dir)?;
24-
connect(&data_dir)
23+
Ok(connect(&data_dir)
2524
.await
26-
.context("failed to recreate database")?
25+
.context("failed to recreate database")?)
2726
}
28-
};
29-
30-
POOL.set(pool)
31-
.map_err(|_| anyhow!("database was already initialized!"))?;
32-
33-
Ok(())
27+
}
3428
}
3529

3630
async fn connect(data_dir: &str) -> Result<SqlitePool> {
@@ -46,8 +40,3 @@ fn reset(data_dir: &str) -> Result<()> {
4640
let bk_path = db_path.with_extension("db.bk");
4741
std::fs::rename(db_path, bk_path).context("failed to backup old database")
4842
}
49-
50-
pub async fn get() -> Result<&'static SqlitePool> {
51-
POOL.get()
52-
.ok_or(anyhow!("database pool was not initialized"))
53-
}

server/bleep/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#[cfg(any(bench, test))]
1414
use criterion as _;
1515

16+
use db::SqlDb;
1617
#[cfg(any(bench, test))]
1718
use git_version as _;
1819

@@ -91,6 +92,9 @@ pub struct Application {
9192
/// Main cookie encryption keypair
9293
cookie_key: axum_extra::extract::cookie::Key,
9394

95+
/// SQL database for persistent storage
96+
sql: SqlDb,
97+
9498
/// Analytics backend -- may be unintialized
9599
pub analytics: Option<Arc<analytics::RudderHub>>,
96100
}
@@ -113,7 +117,7 @@ impl Application {
113117
let config = Arc::new(config);
114118
debug!(?config, "effective configuration");
115119

116-
db::init(&config).await?;
120+
let sqlite = db::init(&config).await?.into();
117121

118122
// Initialise Semantic index if `qdrant_url` set in config
119123
let semantic = match config.qdrant_url {
@@ -153,6 +157,7 @@ impl Application {
153157
sync_queue: SyncQueue::start(config.clone()),
154158
cookie_key: config.source.initialize_cookie_key()?,
155159
credentials: config.source.initialize_credentials()?.into(),
160+
sql: sqlite,
156161
repo_pool,
157162
analytics,
158163
semantic,

server/bleep/src/webserver/answer.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use tracing::{debug, info, trace, warn};
2727
use super::middleware::User;
2828
use crate::{
2929
analytics::{EventData, QueryEvent},
30-
db,
30+
db::SqlDb,
3131
query::parser::{self, SemanticQuery},
3232
repo::RepoRef,
3333
Application,
@@ -103,7 +103,7 @@ pub(super) async fn _handle(
103103
thread_id: params.thread_id,
104104
};
105105

106-
let mut conversation = Conversation::load(&conversation_id)
106+
let mut conversation = Conversation::load(&app.sql, &conversation_id)
107107
.await?
108108
.unwrap_or_else(|| Conversation::new(params.repo_ref.clone()));
109109

@@ -200,7 +200,7 @@ pub(super) async fn _handle(
200200
}
201201

202202
// Storing the conversation here allows us to make subsequent requests.
203-
conversation.store(conversation_id).await?;
203+
conversation.store(&ctx.app.sql, conversation_id).await?;
204204
};
205205

206206
let thread_stream = futures::stream::once(async move {
@@ -959,9 +959,8 @@ impl Conversation {
959959
Ok(())
960960
}
961961

962-
async fn store(self, id: ConversationId) -> Result<()> {
962+
async fn store(self, db: &SqlDb, id: ConversationId) -> Result<()> {
963963
info!("writing conversation {}-{}", id.user_id, id.thread_id);
964-
let db = db::get().await?;
965964
let mut transaction = db.begin().await?;
966965

967966
// Delete the old conversation for simplicity. This also deletes all its messages.
@@ -1010,9 +1009,7 @@ impl Conversation {
10101009
Ok(())
10111010
}
10121011

1013-
async fn load(id: &ConversationId) -> Result<Option<Self>> {
1014-
let db = db::get().await?;
1015-
1012+
async fn load(db: &SqlDb, id: &ConversationId) -> Result<Option<Self>> {
10161013
let (user_id, thread_id) = (id.user_id.clone(), id.thread_id.to_string());
10171014

10181015
let row = sqlx::query! {
@@ -1021,7 +1018,7 @@ impl Conversation {
10211018
user_id,
10221019
thread_id,
10231020
}
1024-
.fetch_optional(db)
1021+
.fetch_optional(db.as_ref())
10251022
.await?;
10261023

10271024
let row = match row {

server/bleep/src/webserver/answer/conversations.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use axum::{
2-
extract::{Path, Query},
2+
extract::{Path, Query, State},
33
response::IntoResponse,
44
Extension, Json,
55
};
66
use reqwest::StatusCode;
77

88
use crate::{
9-
db,
109
repo::RepoRef,
1110
webserver::{self, middleware::User, Error, ErrorKind},
11+
Application,
1212
};
1313

1414
use super::{Conversation, ConversationId};
@@ -28,9 +28,9 @@ pub(in crate::webserver) struct List {
2828
pub(in crate::webserver) async fn list(
2929
Extension(user): Extension<User>,
3030
Query(query): Query<List>,
31+
State(app): State<Application>,
3132
) -> webserver::Result<impl IntoResponse> {
32-
let db = db::get().await?;
33-
33+
let db = app.sql.as_ref();
3434
let user_id = user.0.ok_or_else(|| Error::user("missing user ID"))?;
3535

3636
let conversations = if let Some(repo_ref) = query.repo_ref {
@@ -71,8 +71,9 @@ pub(in crate::webserver) struct Delete {
7171
pub(in crate::webserver) async fn delete(
7272
Query(params): Query<Delete>,
7373
Extension(user): Extension<User>,
74+
State(app): State<Application>,
7475
) -> webserver::Result<()> {
75-
let db = db::get().await?;
76+
let db = app.sql.as_ref();
7677
let user_id = user.0.ok_or_else(|| Error::user("missing user ID"))?;
7778

7879
let result = sqlx::query! {
@@ -94,9 +95,10 @@ pub(in crate::webserver) async fn delete(
9495
pub(in crate::webserver) async fn thread(
9596
Path(thread_id): Path<uuid::Uuid>,
9697
Extension(user): Extension<User>,
98+
State(app): State<Application>,
9799
) -> webserver::Result<impl IntoResponse> {
98100
let user_id = user.0.ok_or_else(|| Error::user("missing user ID"))?;
99-
let conversation = Conversation::load(&ConversationId { thread_id, user_id })
101+
let conversation = Conversation::load(&app.sql, &ConversationId { thread_id, user_id })
100102
.await?
101103
.ok_or_else(|| Error::new(ErrorKind::NotFound, "thread was not found"))?;
102104

0 commit comments

Comments
 (0)