🌐 AI搜索 & 代理 主页
Skip to content
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
Added once_cell crate to make RUNTIME lazy
  • Loading branch information
SilasMarvin committed Mar 22, 2024
commit 8e11c7d55c3eafde373a8cdba7911289f28eace6
1 change: 1 addition & 0 deletions pgml-sdks/pgml/Cargo.lock

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

1 change: 1 addition & 0 deletions pgml-sdks/pgml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ colored = "2"
ctrlc = "3"
inquire = "0.6"
parking_lot = "0.12.1"
once_cell = "1.19.0"

[features]
default = []
Expand Down
24 changes: 8 additions & 16 deletions pgml-sdks/pgml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//!
//! 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.

use once_cell::sync::Lazy;
use parking_lot::RwLock;
use sqlx::{postgres::PgPoolOptions, PgPool};
use std::collections::HashMap;
Expand Down Expand Up @@ -123,24 +124,15 @@ fn internal_init_logger(level: Option<String>, format: Option<String>) -> anyhow

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

#[allow(dead_code)]
fn get_or_set_runtime<'a>() -> &'a Runtime {
unsafe {
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh wow, makes sense. This is totally not thread safe!

if let Some(r) = &RUNTIME {
r
} else {
// Need to use multi thread for JavaScript
let runtime = Builder::new_multi_thread()
.enable_all()
.build()
.expect("Error creating tokio runtime");
RUNTIME = Some(runtime);
get_or_set_runtime()
}
}
&RUNTIME
}

#[cfg(feature = "python")]
Expand Down