Skip to content

Commit

Permalink
GH-750 - introduce dedicated thread pool executors to avoid handing t…
Browse files Browse the repository at this point in the history
…hreads to block everything else
  • Loading branch information
martinlippert committed Apr 13, 2022
1 parent cda0b75 commit 5d0bed4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -90,6 +92,8 @@ public class SimpleTextDocumentService implements TextDocumentService, DocumentE
private final ListenerList<TextDocument> documentCloseListeners = new ListenerList<>();
private final ListenerList<TextDocument> documentOpenListeners = new ListenerList<>();
private List<Consumer<TextDocumentSaveChange>> documentSaveListeners = ImmutableList.of();

private final Executor messageWorkerThreadPool;

private CompletionHandler completionHandler;
private CompletionResolveHandler completionResolveHandler;
Expand All @@ -104,6 +108,8 @@ public class SimpleTextDocumentService implements TextDocumentService, DocumentE
public SimpleTextDocumentService(SimpleLanguageServer server, LanguageServerProperties props) {
this.server = server;
this.props = props;

this.messageWorkerThreadPool = Executors.newCachedThreadPool();
}

/**
Expand Down Expand Up @@ -253,7 +259,7 @@ private TrackedDocument createDocument(final String url, final LanguageId langua
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(CompletionParams position) {
log.info("completion request arrived: " + position.getTextDocument().getUri());

return CompletableFutures.computeAsync(cancelToken -> {
return CompletableFutures.computeAsync(messageWorkerThreadPool, cancelToken -> {
CompletionHandler h = completionHandler;

if (h != null) {
Expand All @@ -269,7 +275,7 @@ public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completio
public CompletableFuture<CompletionItem> resolveCompletionItem(CompletionItem unresolved) {
log.info("Completion item resolve request received: {}", unresolved.getLabel());

return CompletableFutures.computeAsync(cancelToken -> {
return CompletableFutures.computeAsync(messageWorkerThreadPool, cancelToken -> {
try {
CompletionResolveHandler h = completionResolveHandler;
if (h != null) {
Expand All @@ -291,7 +297,7 @@ public CompletableFuture<CompletionItem> resolveCompletionItem(CompletionItem un
public CompletableFuture<Hover> hover(HoverParams hoverParams) {
log.debug("hover requested for {}", hoverParams.getPosition());

CompletableFuture<Hover> result = CompletableFutures.computeAsync(cancelToken -> {
CompletableFuture<Hover> result = CompletableFutures.computeAsync(messageWorkerThreadPool, cancelToken -> {
return computeHover(cancelToken, hoverParams);
});

Expand Down Expand Up @@ -330,7 +336,7 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio

DefinitionHandler h = this.definitionHandler;
if (h != null) {
return CompletableFutures.computeAsync(cancelToken -> {
return CompletableFutures.computeAsync(messageWorkerThreadPool, cancelToken -> {

cancelToken.checkCanceled();

Expand Down Expand Up @@ -361,7 +367,7 @@ public CompletableFuture<List<? extends Location>> references(ReferenceParams pa
ReferencesHandler h = this.referencesHandler;
if (h != null) {

return CompletableFutures.computeAsync(cancelToken -> {
return CompletableFutures.computeAsync(messageWorkerThreadPool, cancelToken -> {
List<? extends Location> list = h.handle(cancelToken, params);
return list != null && list.isEmpty() ? null : list;
});
Expand All @@ -376,7 +382,7 @@ public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> docume
DocumentSymbolHandler h = this.documentSymbolHandler;
if (h != null) {

return CompletableFutures.computeAsync(cancelToken -> {
return CompletableFutures.computeAsync(messageWorkerThreadPool, cancelToken -> {
cancelToken.checkCanceled();

try {
Expand Down Expand Up @@ -433,7 +439,7 @@ public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams param
CodeLensHandler handler = this.codeLensHandler;

if (handler != null) {
return CompletableFutures.computeAsync(cancelToken -> {
return CompletableFutures.computeAsync(messageWorkerThreadPool, cancelToken -> {
return handler.handle(cancelToken, params);
});
}
Expand All @@ -445,7 +451,7 @@ public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) {
CodeLensResolveHandler handler = this.codeLensResolveHandler;
if (handler != null) {

return CompletableFutures.computeAsync(cancelToken -> {
return CompletableFutures.computeAsync(messageWorkerThreadPool, cancelToken -> {
return handler.handle(unresolved);
});

Expand Down Expand Up @@ -476,15 +482,15 @@ public void didSave(DidSaveTextDocumentParams params) {
}
}
}
});
}, messageWorkerThreadPool);
}
}

@Override
public CompletableFuture<List<? extends DocumentHighlight>> documentHighlight(DocumentHighlightParams highlightParams) {
DocumentHighlightHandler handler = this.documentHighlightHandler;
if (handler != null) {
return CompletableFutures.computeAsync(cancelToken -> {
return CompletableFutures.computeAsync(messageWorkerThreadPool, cancelToken -> {
return handler.handle(cancelToken, highlightParams);

});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Pivotal, Inc.
* Copyright (c) 2019, 2022 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -20,6 +20,8 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
Expand All @@ -36,7 +38,6 @@
*/
@SuppressWarnings("restriction")
public class SpringProcessConnectorLocal {


private static final Logger log = LoggerFactory.getLogger(SpringProcessConnectorLocal.class);

Expand All @@ -47,12 +48,16 @@ public class SpringProcessConnectorLocal {
private final Set<SpringProcessDescriptor> processes;

private final SpringProcessConnectorService processConnectorService;
private final Executor statusUpdateThreadPool;

private boolean projectsChanged;


public SpringProcessConnectorLocal(SpringProcessConnectorService processConnector, ProjectObserver projectObserver) {
this.projects = new ConcurrentHashMap<>();
this.processes = Collections.synchronizedSet(new HashSet<>());
this.statusUpdateThreadPool = Executors.newFixedThreadPool(10);

this.projectsChanged = false;

this.processConnectorService = processConnector;
Expand Down Expand Up @@ -168,7 +173,7 @@ private void updateStatus(SpringProcessDescriptor[] processes) {
List<CompletableFuture<Void>> futures = new ArrayList<>();

for (SpringProcessDescriptor process : processes) {
futures.add(process.updateStatus(projects::containsKey, projects::get));
futures.add(process.updateStatus(projects::containsKey, projects::get, statusUpdateThreadPool));
}

CompletableFuture<Void> allStatusUpdates = CompletableFuture.allOf((CompletableFuture[]) futures.toArray(new CompletableFuture[futures.size()]));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Pivotal, Inc.
* Copyright (c) 2019, 2022 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -12,6 +12,7 @@

import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Predicate;

import org.slf4j.Logger;
Expand Down Expand Up @@ -57,7 +58,7 @@ public class SpringProcessDescriptor {

private SpringProcessStatus status;
private String projectName;

public SpringProcessDescriptor(String processKey, String processID, String processName, VirtualMachineDescriptor vm) {
this.processKey = processKey;
this.processID = processID;
Expand Down Expand Up @@ -109,11 +110,11 @@ public boolean equals(Object obj) {
return true;
}

public CompletableFuture<Void> updateStatus(Predicate<String> projectIsKnown, Predicate<String> projectHasActuators) {
public CompletableFuture<Void> updateStatus(Predicate<String> projectIsKnown, Predicate<String> projectHasActuators, Executor statusUpdateThreadPool) {
return CompletableFuture.supplyAsync(() -> {
this.status = checkStatus(projectIsKnown, projectHasActuators);
return null;
});
}, statusUpdateThreadPool);
}

private SpringProcessStatus checkStatus(Predicate<String> projectIsKnown, Predicate<String> projectHasActuators) {
Expand Down

0 comments on commit 5d0bed4

Please sign in to comment.