mirror of
https://github.com/openai/codex.git
synced 2026-04-28 02:11:08 +03:00
tokio locks
This commit is contained in:
1
codex-rs/Cargo.lock
generated
1
codex-rs/Cargo.lock
generated
@@ -1247,6 +1247,7 @@ version = "0.0.0"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
"thiserror 2.0.16",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -6,6 +6,7 @@ edition.workspace = true
|
||||
[dependencies]
|
||||
serde_json = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
tokio = { workspace = true, features = ["rt", "sync"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -4,9 +4,11 @@ use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::OnceLock;
|
||||
use thiserror::Error;
|
||||
use tokio::runtime::Builder as RuntimeBuilder;
|
||||
use tokio::runtime::Handle as RuntimeHandle;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
const INTERNAL_STORAGE_FILENAME: &str = "internal_storage.json";
|
||||
|
||||
@@ -58,14 +60,14 @@ impl Storage {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read(&self, key: &str) -> Result<Option<String>, InternalStorageError> {
|
||||
let _guard = self.lock.lock().expect("internal storage lock poisoned");
|
||||
async fn read_async(&self, key: &str) -> Result<Option<String>, InternalStorageError> {
|
||||
let _guard = self.lock.lock().await;
|
||||
let map = self.read_map()?;
|
||||
Ok(map.get(key).map(value_to_string))
|
||||
}
|
||||
|
||||
fn write(&self, key: &str, value: &str) -> Result<(), InternalStorageError> {
|
||||
let _guard = self.lock.lock().expect("internal storage lock poisoned");
|
||||
async fn write_async(&self, key: &str, value: &str) -> Result<(), InternalStorageError> {
|
||||
let _guard = self.lock.lock().await;
|
||||
let mut map = self.read_map()?;
|
||||
map.insert(key.to_string(), Value::String(value.to_string()));
|
||||
self.write_map(&map)
|
||||
@@ -87,11 +89,19 @@ pub fn initialize(codex_home: PathBuf) {
|
||||
}
|
||||
|
||||
pub fn read(key: &str) -> Result<Option<String>, InternalStorageError> {
|
||||
storage()?.read(key)
|
||||
let fut = storage()?.read_async(key);
|
||||
match block_on(fut) {
|
||||
Ok(res) => res,
|
||||
Err(err) => Err(err.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(key: &str, value: &str) -> Result<(), InternalStorageError> {
|
||||
storage()?.write(key, value)
|
||||
let fut = storage()?.write_async(key, value);
|
||||
match block_on(fut) {
|
||||
Ok(res) => res,
|
||||
Err(err) => Err(err.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn storage() -> Result<&'static Storage, InternalStorageError> {
|
||||
@@ -111,3 +121,16 @@ fn value_to_string(value: &Value) -> String {
|
||||
_ => value.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn block_on<F>(fut: F) -> Result<F::Output, io::Error>
|
||||
where
|
||||
F: std::future::Future,
|
||||
{
|
||||
match RuntimeHandle::try_current() {
|
||||
Ok(handle) => Ok(handle.block_on(fut)),
|
||||
Err(_) => {
|
||||
let rt = RuntimeBuilder::new_current_thread().enable_all().build()?;
|
||||
Ok(rt.block_on(fut))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user