Skip to content

Commit 5acf199

Browse files
authored
load plugins in parallel (#89)
1 parent 9d6b5f7 commit 5acf199

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

crates/kiorg/src/plugins/manager.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ impl PluginManager {
263263

264264
let entries =
265265
std::fs::read_dir(&self.plugin_dir).map_err(|e| PluginError::IoError { source: e })?;
266+
267+
let mut paths = Vec::new();
266268
for entry in entries {
267269
let entry = entry.map_err(|e| PluginError::IoError { source: e })?;
268270
let path = entry.path();
@@ -274,9 +276,28 @@ impl PluginManager {
274276
if let Some(filename) = path.file_name().and_then(|n| n.to_str())
275277
&& filename.starts_with(PLUGIN_PREFIX)
276278
{
277-
info!("Discovered plugin file: {:?}", path);
279+
paths.push(path);
280+
}
281+
}
282+
283+
if paths.is_empty() {
284+
return Ok(());
285+
}
278286

279-
match self.load_single_plugin(&path) {
287+
info!("Loading {} plugins in parallel", paths.len());
288+
289+
let mut handles = Vec::new();
290+
for path in paths.into_iter() {
291+
let handle = std::thread::spawn(move || {
292+
let result = Self::load_single_plugin(&path);
293+
(path, result)
294+
});
295+
handles.push(handle);
296+
}
297+
298+
for handle in handles {
299+
match handle.join() {
300+
Ok((path, result)) => match result {
280301
Ok(plugin) => {
281302
let name = plugin.metadata.name.clone();
282303

@@ -305,6 +326,9 @@ impl PluginManager {
305326
error: e.to_string(),
306327
});
307328
}
329+
},
330+
Err(err) => {
331+
error!(err =? err, "Plugin loading thread panicked");
308332
}
309333
}
310334
}
@@ -313,7 +337,7 @@ impl PluginManager {
313337
}
314338

315339
/// Load a single plugin from the given path
316-
fn load_single_plugin(&self, path: &PathBuf) -> Result<LoadedPlugin, PluginError> {
340+
fn load_single_plugin(path: &PathBuf) -> Result<LoadedPlugin, PluginError> {
317341
// Start the plugin process
318342
let mut cmd = Command::new(path);
319343
cmd.stdin(Stdio::piped())
@@ -327,7 +351,7 @@ impl PluginManager {
327351
})?;
328352

329353
// Perform hello handshake to get plugin metadata
330-
let (metadata, error) = match self.perform_hello_handshake(&mut child, path) {
354+
let (metadata, error) = match Self::perform_hello_handshake(&mut child, path) {
331355
Ok(meta) => (meta, None),
332356
Err(PluginError::Incompatible {
333357
protocol_version,
@@ -379,7 +403,6 @@ impl PluginManager {
379403

380404
/// Perform hello handshake with a plugin to get metadata and capabilities
381405
fn perform_hello_handshake(
382-
&self,
383406
child: &mut Child,
384407
plugin_path: &std::path::Path,
385408
) -> Result<PluginMetadata, PluginError> {

0 commit comments

Comments
 (0)