improv: Use HashSet for tracking finished plugins
This commit is contained in:
parent
48f09be4c9
commit
0195939ed8
1 changed files with 14 additions and 11 deletions
|
|
@ -7,7 +7,10 @@ use flume::{unbounded, Receiver, Sender};
|
||||||
use futures_lite::{future, StreamExt};
|
use futures_lite::{future, StreamExt};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
use std::io::{self, Write};
|
use std::{
|
||||||
|
collections::HashSet,
|
||||||
|
io::{self, Write},
|
||||||
|
};
|
||||||
|
|
||||||
pub type PluginKey = usize;
|
pub type PluginKey = usize;
|
||||||
|
|
||||||
|
|
@ -31,7 +34,7 @@ pub async fn main() {
|
||||||
|
|
||||||
pub struct Service<O> {
|
pub struct Service<O> {
|
||||||
active_search: Vec<(PluginKey, PluginSearchResult)>,
|
active_search: Vec<(PluginKey, PluginSearchResult)>,
|
||||||
awaiting_results: usize,
|
awaiting_results: HashSet<PluginKey>,
|
||||||
last_query: String,
|
last_query: String,
|
||||||
output: O,
|
output: O,
|
||||||
plugins: Slab<PluginConnector>,
|
plugins: Slab<PluginConnector>,
|
||||||
|
|
@ -43,7 +46,7 @@ impl<O: Write> Service<O> {
|
||||||
pub fn new(output: O) -> Self {
|
pub fn new(output: O) -> Self {
|
||||||
Self {
|
Self {
|
||||||
active_search: Vec::new(),
|
active_search: Vec::new(),
|
||||||
awaiting_results: 0,
|
awaiting_results: HashSet::new(),
|
||||||
last_query: String::new(),
|
last_query: String::new(),
|
||||||
output,
|
output,
|
||||||
plugins: Slab::new(),
|
plugins: Slab::new(),
|
||||||
|
|
@ -218,9 +221,9 @@ impl<O: Write> Service<O> {
|
||||||
self.respond(&Response::Fill(text));
|
self.respond(&Response::Fill(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finished(&mut self, _plugin: PluginKey) {
|
fn finished(&mut self, plugin: PluginKey) {
|
||||||
self.awaiting_results -= 1;
|
self.awaiting_results.remove(&plugin);
|
||||||
if self.awaiting_results == 0 {
|
if self.awaiting_results.is_empty() {
|
||||||
if self.search_scheduled {
|
if self.search_scheduled {
|
||||||
self.search(String::new());
|
self.search(String::new());
|
||||||
return;
|
return;
|
||||||
|
|
@ -254,7 +257,7 @@ impl<O: Write> Service<O> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search(&mut self, query: String) {
|
fn search(&mut self, query: String) {
|
||||||
if self.awaiting_results > 0 {
|
if !self.awaiting_results.is_empty() {
|
||||||
tracing::debug!("backing off from search until plugins are ready");
|
tracing::debug!("backing off from search until plugins are ready");
|
||||||
if !self.search_scheduled {
|
if !self.search_scheduled {
|
||||||
self.interrupt();
|
self.interrupt();
|
||||||
|
|
@ -306,20 +309,20 @@ impl<O: Write> Service<O> {
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
tracing::debug!("submitted query to {}", plugin.config.name);
|
tracing::debug!("submitted query to {}", plugin.config.name);
|
||||||
self.awaiting_results += 1;
|
self.awaiting_results.insert(isolated);
|
||||||
self.no_sort = plugin.config.query.no_sort;
|
self.no_sort = plugin.config.query.no_sort;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for plugin in query_queue {
|
for plugin_id in query_queue {
|
||||||
if let Some(plugin) = self.plugins.get_mut(plugin) {
|
if let Some(plugin) = self.plugins.get_mut(plugin_id) {
|
||||||
if plugin
|
if plugin
|
||||||
.sender_exec()
|
.sender_exec()
|
||||||
.send(Request::Search(query.to_owned()))
|
.send(Request::Search(query.to_owned()))
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
tracing::debug!("submitted query to {}", plugin.config.name);
|
tracing::debug!("submitted query to {}", plugin.config.name);
|
||||||
self.awaiting_results += 1;
|
self.awaiting_results.insert(plugin_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue