Skip to content

Commit 730d523

Browse files
authored
SDK - Added once_cell crate to make RUNTIME lazy (#1386)
1 parent 87dd551 commit 730d523

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

pgml-sdks/pgml/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pgml-sdks/pgml/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ colored = "2"
4444
ctrlc = "3"
4545
inquire = "0.6"
4646
parking_lot = "0.12.1"
47+
once_cell = "1.19.0"
4748

4849
[features]
4950
default = []

pgml-sdks/pgml/src/lib.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//!
55
//! With this SDK, you can seamlessly manage various database tables related to documents, text chunks, text splitters, LLM (Language Model) models, and embeddings. By leveraging the SDK's capabilities, you can efficiently index LLM embeddings using PgVector for fast and accurate queries.
66
7+
use once_cell::sync::Lazy;
78
use parking_lot::RwLock;
89
use sqlx::{postgres::PgPoolOptions, PgPool};
910
use std::collections::HashMap;
@@ -123,24 +124,15 @@ fn internal_init_logger(level: Option<String>, format: Option<String>) -> anyhow
123124

124125
// Normally the global async runtime is handled by tokio but because we are a library being called
125126
// by javascript and other langauges, we occasionally need to handle it ourselves
126-
#[allow(dead_code)]
127-
static mut RUNTIME: Option<Runtime> = None;
127+
static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
128+
Builder::new_multi_thread()
129+
.enable_all()
130+
.build()
131+
.expect("Error creating tokio runtime")
132+
});
128133

129-
#[allow(dead_code)]
130134
fn get_or_set_runtime<'a>() -> &'a Runtime {
131-
unsafe {
132-
if let Some(r) = &RUNTIME {
133-
r
134-
} else {
135-
// Need to use multi thread for JavaScript
136-
let runtime = Builder::new_multi_thread()
137-
.enable_all()
138-
.build()
139-
.expect("Error creating tokio runtime");
140-
RUNTIME = Some(runtime);
141-
get_or_set_runtime()
142-
}
143-
}
135+
&RUNTIME
144136
}
145137

146138
#[cfg(feature = "python")]

0 commit comments

Comments
 (0)