Skip to content

Commit

Permalink
Update TaskQueue to support serial execution
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Oct 23, 2023
1 parent d9c28f5 commit d52e0cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
15 changes: 15 additions & 0 deletions compiler/crates/relay-lsp/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ fn next_task(
}
}

static SERIAL_NOTIFICATIONS: [&str; 3] = [
"textDocument/didChange",
"textDocument/didOpen",
"textDocument/didClose",
];

struct LSPTaskProcessor;

impl<TPerfLogger: PerfLogger + 'static, TSchemaDocumentation: SchemaDocumentation + 'static>
Expand All @@ -210,6 +216,15 @@ impl<TPerfLogger: PerfLogger + 'static, TSchemaDocumentation: SchemaDocumentatio
}
}
}

fn requires_serial_execution(&self, task: &Task) -> bool {
match task {
Task::InboundMessage(Message::Notification(notification)) => {
SERIAL_NOTIFICATIONS.contains(&notification.method.as_str())
}
_ => false,
}
}
}

fn handle_request<TPerfLogger: PerfLogger + 'static, TSchemaDocumentation: SchemaDocumentation>(
Expand Down
18 changes: 15 additions & 3 deletions compiler/crates/relay-lsp/src/server/task_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct TaskQueue<S, T> {

pub trait TaskProcessor<S, T>: Send + Sync + 'static {
fn process(&self, state: Arc<S>, task: T);

fn requires_serial_execution(&self, task: &T) -> bool;
}

pub struct TaskScheduler<T> {
Expand Down Expand Up @@ -60,13 +62,23 @@ where
let now = Instant::now();
debug!("Processing task {:?}", &task_str);
let processor = Arc::clone(&self.processor);
thread::spawn(move || {

if processor.requires_serial_execution(&task) {
processor.process(state, task);
debug!(
"task {} completed in {}ms",
"task {} completed serially in {}ms",
task_str,
now.elapsed().as_millis()
);
});
} else {
thread::spawn(move || {
processor.process(state, task);
debug!(
"task {} completed in {}ms",
task_str,
now.elapsed().as_millis()
);
});
}
}
}

0 comments on commit d52e0cf

Please sign in to comment.