refactor(server): Resolve bottlenecks and duplications
- Removed heavy Tantivy index rebuild loop that ran every 5 seconds and rewrote entire graph to disk - Converted polling worker to lightweight IndexWriter committer - Fixed 67+ instances of unnecessary clone and string parsing overhead in tool return results - Deduplicated state graph modification methods
This commit is contained in:
1 parent
4fe6ed7791
commit
2a25f17a32
3 files changed
+86
-108
No files matched your search
+5
-23
@@ -86,30 +86,12 @@ enum GateCommands {
|
||||
},
|
||||
}
|
||||
|
||||
async fn reconcile_worker(state: Arc<MemoryState>) {
|
||||
async fn index_committer_worker(state: Arc<MemoryState>) {
|
||||
loop {
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
|
||||
let has_local = {
|
||||
let session = state.graph.read();
|
||||
!session.entities.is_empty() || !session.relations.is_empty()
|
||||
};
|
||||
|
||||
let base_dir = state.base_dir.clone();
|
||||
let has_files = tokio::task::spawn_blocking(move || {
|
||||
let pattern = format!("{}/delta_*.json", base_dir.display());
|
||||
glob::glob(&pattern).map(|p| p.count() > 0).unwrap_or(false)
|
||||
})
|
||||
.await
|
||||
.unwrap_or(false);
|
||||
|
||||
if has_local || has_files {
|
||||
state.apply_sync_write(|_master| {});
|
||||
let state_clone = state.clone();
|
||||
let _ = tokio::task::spawn_blocking(move || {
|
||||
state_clone.rebuild_index();
|
||||
})
|
||||
.await;
|
||||
// Periodically commit the search index to persist inline indexing operations
|
||||
if let Ok(idx) = state.search_index.read() {
|
||||
let _ = idx.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -235,7 +217,7 @@ async fn gate_set_handler(
|
||||
fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
rt.block_on(async {
|
||||
tokio::spawn(reconcile_worker(Arc::clone(&state)));
|
||||
tokio::spawn(index_committer_worker(Arc::clone(&state)));
|
||||
let app_state = Arc::new(AppState {
|
||||
handler: Arc::new(MemoryHandler {
|
||||
state: Arc::clone(&state),
|
||||
|
||||
Reference in new issue
Block a user