mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Remove job handle
This commit is contained in:
parent
8bb4380448
commit
0102a01f76
6 changed files with 18 additions and 100 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -609,7 +609,6 @@ dependencies = [
|
||||||
name = "ra_analysis"
|
name = "ra_analysis"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"fst 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"fst 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"im 12.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"im 12.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -7,7 +7,6 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
relative-path = "0.3.7"
|
relative-path = "0.3.7"
|
||||||
log = "0.4.2"
|
log = "0.4.2"
|
||||||
crossbeam-channel = "0.2.4"
|
|
||||||
parking_lot = "0.6.3"
|
parking_lot = "0.6.3"
|
||||||
once_cell = "0.1.5"
|
once_cell = "0.1.5"
|
||||||
rayon = "1.0.2"
|
rayon = "1.0.2"
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
use crossbeam_channel::{bounded, Receiver, Sender};
|
|
||||||
|
|
||||||
pub struct JobHandle {
|
|
||||||
job_alive: Receiver<Never>,
|
|
||||||
_job_canceled: Sender<Never>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct JobToken {
|
|
||||||
_job_alive: Sender<Never>,
|
|
||||||
job_canceled: Receiver<Never>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl JobHandle {
|
|
||||||
pub fn new() -> (JobHandle, JobToken) {
|
|
||||||
let (sender_alive, receiver_alive) = bounded(0);
|
|
||||||
let (sender_canceled, receiver_canceled) = bounded(0);
|
|
||||||
let token = JobToken {
|
|
||||||
_job_alive: sender_alive,
|
|
||||||
job_canceled: receiver_canceled,
|
|
||||||
};
|
|
||||||
let handle = JobHandle {
|
|
||||||
job_alive: receiver_alive,
|
|
||||||
_job_canceled: sender_canceled,
|
|
||||||
};
|
|
||||||
(handle, token)
|
|
||||||
}
|
|
||||||
pub fn has_completed(&self) -> bool {
|
|
||||||
is_closed(&self.job_alive)
|
|
||||||
}
|
|
||||||
pub fn cancel(self) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl JobToken {
|
|
||||||
pub fn is_canceled(&self) -> bool {
|
|
||||||
is_closed(&self.job_canceled)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't actually send messages through the channels,
|
|
||||||
// and instead just check if the channel is closed,
|
|
||||||
// so we use uninhabited enum as a message type
|
|
||||||
enum Never {}
|
|
||||||
|
|
||||||
/// Nonblocking
|
|
||||||
fn is_closed(chan: &Receiver<Never>) -> bool {
|
|
||||||
select! {
|
|
||||||
recv(chan, msg) => match msg {
|
|
||||||
None => true,
|
|
||||||
Some(never) => match never {}
|
|
||||||
}
|
|
||||||
default => false,
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -7,8 +7,6 @@ extern crate ra_editor;
|
||||||
extern crate ra_syntax;
|
extern crate ra_syntax;
|
||||||
extern crate rayon;
|
extern crate rayon;
|
||||||
extern crate relative_path;
|
extern crate relative_path;
|
||||||
#[macro_use]
|
|
||||||
extern crate crossbeam_channel;
|
|
||||||
extern crate im;
|
extern crate im;
|
||||||
extern crate rustc_hash;
|
extern crate rustc_hash;
|
||||||
extern crate salsa;
|
extern crate salsa;
|
||||||
|
@ -16,7 +14,6 @@ extern crate salsa;
|
||||||
mod db;
|
mod db;
|
||||||
mod descriptors;
|
mod descriptors;
|
||||||
mod imp;
|
mod imp;
|
||||||
mod job;
|
|
||||||
mod module_map;
|
mod module_map;
|
||||||
mod roots;
|
mod roots;
|
||||||
mod symbol_index;
|
mod symbol_index;
|
||||||
|
@ -31,7 +28,6 @@ use crate::imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp};
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
descriptors::FnDescriptor,
|
descriptors::FnDescriptor,
|
||||||
job::{JobHandle, JobToken},
|
|
||||||
};
|
};
|
||||||
pub use ra_editor::{
|
pub use ra_editor::{
|
||||||
CompletionItem, FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable,
|
CompletionItem, FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable,
|
||||||
|
|
|
@ -7,7 +7,7 @@ use languageserver_types::{
|
||||||
InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit,
|
InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit,
|
||||||
RenameParams, WorkspaceEdit, PrepareRenameResponse
|
RenameParams, WorkspaceEdit, PrepareRenameResponse
|
||||||
};
|
};
|
||||||
use ra_analysis::{FileId, FoldKind, JobToken, Query, RunnableKind};
|
use ra_analysis::{FileId, FoldKind, Query, RunnableKind};
|
||||||
use ra_syntax::text_utils::contains_offset_nonstrict;
|
use ra_syntax::text_utils::contains_offset_nonstrict;
|
||||||
use serde_json::to_value;
|
use serde_json::to_value;
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@ use crate::{
|
||||||
pub fn handle_syntax_tree(
|
pub fn handle_syntax_tree(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::SyntaxTreeParams,
|
params: req::SyntaxTreeParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let id = params.text_document.try_conv_with(&world)?;
|
let id = params.text_document.try_conv_with(&world)?;
|
||||||
let res = world.analysis().syntax_tree(id);
|
let res = world.analysis().syntax_tree(id);
|
||||||
|
@ -32,7 +31,6 @@ pub fn handle_syntax_tree(
|
||||||
pub fn handle_extend_selection(
|
pub fn handle_extend_selection(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::ExtendSelectionParams,
|
params: req::ExtendSelectionParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<req::ExtendSelectionResult> {
|
) -> Result<req::ExtendSelectionResult> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let file = world.analysis().file_syntax(file_id);
|
let file = world.analysis().file_syntax(file_id);
|
||||||
|
@ -50,7 +48,6 @@ pub fn handle_extend_selection(
|
||||||
pub fn handle_find_matching_brace(
|
pub fn handle_find_matching_brace(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::FindMatchingBraceParams,
|
params: req::FindMatchingBraceParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Vec<Position>> {
|
) -> Result<Vec<Position>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let file = world.analysis().file_syntax(file_id);
|
let file = world.analysis().file_syntax(file_id);
|
||||||
|
@ -73,7 +70,6 @@ pub fn handle_find_matching_brace(
|
||||||
pub fn handle_join_lines(
|
pub fn handle_join_lines(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::JoinLinesParams,
|
params: req::JoinLinesParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<req::SourceChange> {
|
) -> Result<req::SourceChange> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -87,7 +83,6 @@ pub fn handle_join_lines(
|
||||||
pub fn handle_on_enter(
|
pub fn handle_on_enter(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::TextDocumentPositionParams,
|
params: req::TextDocumentPositionParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<req::SourceChange>> {
|
) -> Result<Option<req::SourceChange>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -101,7 +96,6 @@ pub fn handle_on_enter(
|
||||||
pub fn handle_on_type_formatting(
|
pub fn handle_on_type_formatting(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::DocumentOnTypeFormattingParams,
|
params: req::DocumentOnTypeFormattingParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<Vec<TextEdit>>> {
|
) -> Result<Option<Vec<TextEdit>>> {
|
||||||
if params.ch != "=" {
|
if params.ch != "=" {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
@ -121,7 +115,6 @@ pub fn handle_on_type_formatting(
|
||||||
pub fn handle_document_symbol(
|
pub fn handle_document_symbol(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::DocumentSymbolParams,
|
params: req::DocumentSymbolParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<req::DocumentSymbolResponse>> {
|
) -> Result<Option<req::DocumentSymbolResponse>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -160,7 +153,6 @@ pub fn handle_document_symbol(
|
||||||
pub fn handle_workspace_symbol(
|
pub fn handle_workspace_symbol(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::WorkspaceSymbolParams,
|
params: req::WorkspaceSymbolParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<Vec<SymbolInformation>>> {
|
) -> Result<Option<Vec<SymbolInformation>>> {
|
||||||
let all_symbols = params.query.contains("#");
|
let all_symbols = params.query.contains("#");
|
||||||
let libs = params.query.contains("*");
|
let libs = params.query.contains("*");
|
||||||
|
@ -212,7 +204,6 @@ pub fn handle_workspace_symbol(
|
||||||
pub fn handle_goto_definition(
|
pub fn handle_goto_definition(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::TextDocumentPositionParams,
|
params: req::TextDocumentPositionParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<req::GotoDefinitionResponse>> {
|
) -> Result<Option<req::GotoDefinitionResponse>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -232,7 +223,6 @@ pub fn handle_goto_definition(
|
||||||
pub fn handle_parent_module(
|
pub fn handle_parent_module(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: TextDocumentIdentifier,
|
params: TextDocumentIdentifier,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Vec<Location>> {
|
) -> Result<Vec<Location>> {
|
||||||
let file_id = params.try_conv_with(&world)?;
|
let file_id = params.try_conv_with(&world)?;
|
||||||
let mut res = Vec::new();
|
let mut res = Vec::new();
|
||||||
|
@ -247,7 +237,6 @@ pub fn handle_parent_module(
|
||||||
pub fn handle_runnables(
|
pub fn handle_runnables(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::RunnablesParams,
|
params: req::RunnablesParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Vec<req::Runnable>> {
|
) -> Result<Vec<req::Runnable>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -351,7 +340,6 @@ pub fn handle_runnables(
|
||||||
pub fn handle_decorations(
|
pub fn handle_decorations(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: TextDocumentIdentifier,
|
params: TextDocumentIdentifier,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Vec<Decoration>> {
|
) -> Result<Vec<Decoration>> {
|
||||||
let file_id = params.try_conv_with(&world)?;
|
let file_id = params.try_conv_with(&world)?;
|
||||||
highlight(&world, file_id)
|
highlight(&world, file_id)
|
||||||
|
@ -360,7 +348,6 @@ pub fn handle_decorations(
|
||||||
pub fn handle_completion(
|
pub fn handle_completion(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::CompletionParams,
|
params: req::CompletionParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<req::CompletionResponse>> {
|
) -> Result<Option<req::CompletionResponse>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -392,7 +379,6 @@ pub fn handle_completion(
|
||||||
pub fn handle_folding_range(
|
pub fn handle_folding_range(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: FoldingRangeParams,
|
params: FoldingRangeParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<Vec<FoldingRange>>> {
|
) -> Result<Option<Vec<FoldingRange>>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -425,7 +411,6 @@ pub fn handle_folding_range(
|
||||||
pub fn handle_signature_help(
|
pub fn handle_signature_help(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::TextDocumentPositionParams,
|
params: req::TextDocumentPositionParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<req::SignatureHelp>> {
|
) -> Result<Option<req::SignatureHelp>> {
|
||||||
use languageserver_types::{ParameterInformation, SignatureInformation};
|
use languageserver_types::{ParameterInformation, SignatureInformation};
|
||||||
|
|
||||||
|
@ -464,7 +449,6 @@ pub fn handle_signature_help(
|
||||||
pub fn handle_prepare_rename(
|
pub fn handle_prepare_rename(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::TextDocumentPositionParams,
|
params: req::TextDocumentPositionParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<PrepareRenameResponse>> {
|
) -> Result<Option<PrepareRenameResponse>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -486,7 +470,6 @@ pub fn handle_prepare_rename(
|
||||||
pub fn handle_rename(
|
pub fn handle_rename(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: RenameParams,
|
params: RenameParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<WorkspaceEdit>> {
|
) -> Result<Option<WorkspaceEdit>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -523,7 +506,6 @@ pub fn handle_rename(
|
||||||
pub fn handle_references(
|
pub fn handle_references(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::ReferenceParams,
|
params: req::ReferenceParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<Vec<Location>>> {
|
) -> Result<Option<Vec<Location>>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
@ -539,7 +521,6 @@ pub fn handle_references(
|
||||||
pub fn handle_code_action(
|
pub fn handle_code_action(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::CodeActionParams,
|
params: req::CodeActionParams,
|
||||||
_token: JobToken,
|
|
||||||
) -> Result<Option<CodeActionResponse>> {
|
) -> Result<Option<CodeActionResponse>> {
|
||||||
let file_id = params.text_document.try_conv_with(&world)?;
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id);
|
let line_index = world.analysis().file_line_index(file_id);
|
||||||
|
|
|
@ -8,9 +8,9 @@ use gen_lsp_server::{
|
||||||
handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse,
|
handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse,
|
||||||
};
|
};
|
||||||
use languageserver_types::NumberOrString;
|
use languageserver_types::NumberOrString;
|
||||||
use ra_analysis::{FileId, JobHandle, JobToken, LibraryData};
|
use ra_analysis::{FileId, LibraryData};
|
||||||
use rayon::{self, ThreadPool};
|
use rayon::{self, ThreadPool};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashSet;
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -47,7 +47,7 @@ pub fn main_loop(
|
||||||
info!("server initialized, serving requests");
|
info!("server initialized, serving requests");
|
||||||
let mut state = ServerWorldState::new();
|
let mut state = ServerWorldState::new();
|
||||||
|
|
||||||
let mut pending_requests = FxHashMap::default();
|
let mut pending_requests = FxHashSet::default();
|
||||||
let mut subs = Subscriptions::new();
|
let mut subs = Subscriptions::new();
|
||||||
let main_res = main_loop_inner(
|
let main_res = main_loop_inner(
|
||||||
internal_mode,
|
internal_mode,
|
||||||
|
@ -92,7 +92,7 @@ fn main_loop_inner(
|
||||||
fs_worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
|
fs_worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
|
||||||
ws_worker: Worker<PathBuf, Result<CargoWorkspace>>,
|
ws_worker: Worker<PathBuf, Result<CargoWorkspace>>,
|
||||||
state: &mut ServerWorldState,
|
state: &mut ServerWorldState,
|
||||||
pending_requests: &mut FxHashMap<u64, JobHandle>,
|
pending_requests: &mut FxHashSet<u64>,
|
||||||
subs: &mut Subscriptions,
|
subs: &mut Subscriptions,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let (libdata_sender, libdata_receiver) = unbounded();
|
let (libdata_sender, libdata_receiver) = unbounded();
|
||||||
|
@ -204,22 +204,21 @@ fn main_loop_inner(
|
||||||
fn on_task(
|
fn on_task(
|
||||||
task: Task,
|
task: Task,
|
||||||
msg_sender: &Sender<RawMessage>,
|
msg_sender: &Sender<RawMessage>,
|
||||||
pending_requests: &mut FxHashMap<u64, JobHandle>,
|
pending_requests: &mut FxHashSet<u64>,
|
||||||
) {
|
) {
|
||||||
match task {
|
match task {
|
||||||
Task::Respond(response) => {
|
Task::Respond(response) => {
|
||||||
if let Some(handle) = pending_requests.remove(&response.id) {
|
if pending_requests.remove(&response.id) {
|
||||||
assert!(handle.has_completed());
|
|
||||||
}
|
|
||||||
msg_sender.send(RawMessage::Response(response))
|
msg_sender.send(RawMessage::Response(response))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Task::Notify(n) => msg_sender.send(RawMessage::Notification(n)),
|
Task::Notify(n) => msg_sender.send(RawMessage::Notification(n)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_request(
|
fn on_request(
|
||||||
world: &mut ServerWorldState,
|
world: &mut ServerWorldState,
|
||||||
pending_requests: &mut FxHashMap<u64, JobHandle>,
|
pending_requests: &mut FxHashSet<u64>,
|
||||||
pool: &ThreadPool,
|
pool: &ThreadPool,
|
||||||
sender: &Sender<Task>,
|
sender: &Sender<Task>,
|
||||||
req: RawRequest,
|
req: RawRequest,
|
||||||
|
@ -253,8 +252,8 @@ fn on_request(
|
||||||
.on::<req::References>(handlers::handle_references)?
|
.on::<req::References>(handlers::handle_references)?
|
||||||
.finish();
|
.finish();
|
||||||
match req {
|
match req {
|
||||||
Ok((id, handle)) => {
|
Ok(id) => {
|
||||||
let inserted = pending_requests.insert(id, handle).is_none();
|
let inserted = pending_requests.insert(id);
|
||||||
assert!(inserted, "duplicate request: {}", id);
|
assert!(inserted, "duplicate request: {}", id);
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
@ -265,7 +264,7 @@ fn on_request(
|
||||||
fn on_notification(
|
fn on_notification(
|
||||||
msg_sender: &Sender<RawMessage>,
|
msg_sender: &Sender<RawMessage>,
|
||||||
state: &mut ServerWorldState,
|
state: &mut ServerWorldState,
|
||||||
pending_requests: &mut FxHashMap<u64, JobHandle>,
|
pending_requests: &mut FxHashSet<u64>,
|
||||||
subs: &mut Subscriptions,
|
subs: &mut Subscriptions,
|
||||||
not: RawNotification,
|
not: RawNotification,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
@ -277,9 +276,7 @@ fn on_notification(
|
||||||
panic!("string id's not supported: {:?}", id);
|
panic!("string id's not supported: {:?}", id);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let Some(handle) = pending_requests.remove(&id) {
|
pending_requests.remove(&id);
|
||||||
handle.cancel();
|
|
||||||
}
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
Err(not) => not,
|
Err(not) => not,
|
||||||
|
@ -336,7 +333,7 @@ fn on_notification(
|
||||||
|
|
||||||
struct PoolDispatcher<'a> {
|
struct PoolDispatcher<'a> {
|
||||||
req: Option<RawRequest>,
|
req: Option<RawRequest>,
|
||||||
res: Option<(u64, JobHandle)>,
|
res: Option<u64>,
|
||||||
pool: &'a ThreadPool,
|
pool: &'a ThreadPool,
|
||||||
world: &'a ServerWorldState,
|
world: &'a ServerWorldState,
|
||||||
sender: &'a Sender<Task>,
|
sender: &'a Sender<Task>,
|
||||||
|
@ -345,7 +342,7 @@ struct PoolDispatcher<'a> {
|
||||||
impl<'a> PoolDispatcher<'a> {
|
impl<'a> PoolDispatcher<'a> {
|
||||||
fn on<'b, R>(
|
fn on<'b, R>(
|
||||||
&'b mut self,
|
&'b mut self,
|
||||||
f: fn(ServerWorld, R::Params, JobToken) -> Result<R::Result>,
|
f: fn(ServerWorld, R::Params) -> Result<R::Result>,
|
||||||
) -> Result<&'b mut Self>
|
) -> Result<&'b mut Self>
|
||||||
where
|
where
|
||||||
R: req::Request,
|
R: req::Request,
|
||||||
|
@ -358,11 +355,10 @@ impl<'a> PoolDispatcher<'a> {
|
||||||
};
|
};
|
||||||
match req.cast::<R>() {
|
match req.cast::<R>() {
|
||||||
Ok((id, params)) => {
|
Ok((id, params)) => {
|
||||||
let (handle, token) = JobHandle::new();
|
|
||||||
let world = self.world.snapshot();
|
let world = self.world.snapshot();
|
||||||
let sender = self.sender.clone();
|
let sender = self.sender.clone();
|
||||||
self.pool.spawn(move || {
|
self.pool.spawn(move || {
|
||||||
let resp = match f(world, params, token) {
|
let resp = match f(world, params) {
|
||||||
Ok(resp) => RawResponse::ok::<R>(id, &resp),
|
Ok(resp) => RawResponse::ok::<R>(id, &resp),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string())
|
RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string())
|
||||||
|
@ -371,14 +367,14 @@ impl<'a> PoolDispatcher<'a> {
|
||||||
let task = Task::Respond(resp);
|
let task = Task::Respond(resp);
|
||||||
sender.send(task);
|
sender.send(task);
|
||||||
});
|
});
|
||||||
self.res = Some((id, handle));
|
self.res = Some(id);
|
||||||
}
|
}
|
||||||
Err(req) => self.req = Some(req),
|
Err(req) => self.req = Some(req),
|
||||||
}
|
}
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish(&mut self) -> ::std::result::Result<(u64, JobHandle), RawRequest> {
|
fn finish(&mut self) -> ::std::result::Result<u64, RawRequest> {
|
||||||
match (self.res.take(), self.req.take()) {
|
match (self.res.take(), self.req.take()) {
|
||||||
(Some(res), None) => Ok(res),
|
(Some(res), None) => Ok(res),
|
||||||
(None, Some(req)) => Err(req),
|
(None, Some(req)) => Err(req),
|
||||||
|
|
Loading…
Reference in a new issue