Skip to content

Commit

Permalink
Use a fixed size response cache to resolve concurrent conflicts betwe…
Browse files Browse the repository at this point in the history
…en codeAction and resolveCodeAction requests
  • Loading branch information
testforstephen authored and rgrunber committed Apr 29, 2021
1 parent 14288d7 commit 2afee43
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -62,7 +63,8 @@
import org.eclipse.lsp4j.jsonrpc.messages.Either;

public class CodeActionHandler {
public static final ResponseStore<Either<ChangeCorrectionProposal, CodeActionProposal>> codeActionStore = new ResponseStore<>();
public static final ResponseStore<Either<ChangeCorrectionProposal, CodeActionProposal>> codeActionStore
= new ResponseStore<>(ForkJoinPool.commonPool().getParallelism());
public static final String COMMAND_ID_APPLY_EDIT = "java.apply.workspaceEdit";

private QuickFixProcessor quickFixProcessor;
Expand All @@ -83,7 +85,6 @@ public CodeActionHandler(PreferenceManager preferenceManager) {
}

public List<Either<Command, CodeAction>> getCodeActionCommands(CodeActionParams params, IProgressMonitor monitor) {
codeActionStore.clear();
if (monitor.isCanceled()) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,34 @@

package org.eclipse.jdt.ls.core.internal.handlers;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public class ResponseStore<T> {
private AtomicLong idSeed = new AtomicLong(0);
private Map<Long, ResponseItem<T>> responseCache = new ConcurrentHashMap<>();
private Map<Long, ResponseItem<T>> responseCache;

/**
* Unlimited cache.
*/
public ResponseStore() {
this.responseCache = new ConcurrentHashMap<>();
}

/**
* Deletes the eldest items if the size of the cache reaches the maximum.
*/
public ResponseStore(int maxSize) {
this.responseCache = Collections.synchronizedMap(new LinkedHashMap<Long, ResponseItem<T>>() {
@Override
protected boolean removeEldestEntry(final Map.Entry eldest) {
return maxSize > 0 && size() > maxSize;
}
});
}

public ResponseItem<T> createResponse() {
Expand Down

0 comments on commit 2afee43

Please sign in to comment.