Skip to content

Commit

Permalink
Update Healthcheck and add javadoc (languagetool-org#9542)
Browse files Browse the repository at this point in the history
* Update Healthcheck and add javadoc

* sebd requests; improve javadoc

* add more javadoc
  • Loading branch information
SteVio89 authored and AdamHider committed Nov 16, 2023
1 parent 68c3e9d commit 39e1aee
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
17 changes: 17 additions & 0 deletions languagetool-core/src/main/java/org/languagetool/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,23 @@ public void setPostDisambiguationChunker(Chunker chunker) {
postDisambiguationChunker = chunker;
}

/**
* Create a shared instance of JLanguageTool to use in rules for further processing
* Instances are shared by Language
* As this is a shared instance, do not modify (add or remove) any rules or filters.
* The alternative to disabling/enabling rules is to select the desired rules from getAllActiveRules(), and run them separately with rule.match(analizedSentence).
*
* Do not call this in a static block or to initialize a static JLanguageTool field in rules or filters classes, this could lead to a deadlock during initialization.
*
* @since 6.1
* @return a shared JLanguageTool instance for this language
*/
public JLanguageTool createDefaultJLanguageTool() {
Language self = this;
Class clazz = this.getClass();
return languagetoolInstances.computeIfAbsent(clazz, _class -> new JLanguageTool(self));
}

/**
* Creates language specific part-of-speech synthesizer. This function will be called
* each time in {@link #getSynthesizer()} if synthesizer is not set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public void handle(HttpExchange httpExchange) throws IOException {
// healthcheck should come before other limit checks (requests per time etc.), to be sure it works:
String pathWithoutVersion = path.substring("/v2/".length());
if (pathWithoutVersion.equals("healthcheck")) {
if (workQueueFull(httpExchange, parameters, "Healthcheck failed: There are currently too many parallel requests.")) {
String message = "Healthcheck failed: There are currently too many parallel requests.";
if (workQueueFull(httpExchange, parameters, message) || textCheckerQueueFull(httpExchange, message)) {
ServerMetricsCollector.getInstance().logFailedHealthcheck();
return;
} else {
Expand Down Expand Up @@ -293,6 +294,14 @@ private boolean workQueueFull(HttpExchange httpExchange, Map<String, String> par
return false;
}

private boolean textCheckerQueueFull(HttpExchange httpExchange, String response) throws IOException {
if(textCheckerV2.checkerQueueAlmostFull()) {
sendError(httpExchange, HTTP_UNAVAILABLE, "Error: " + response);
return true;
}
return false;
}

@NotNull
private String getTextOrDataSizeMessage(Map<String, String> parameters) {
String text = parameters.get("text");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,19 @@ void checkText(AnnotatedText aText, HttpExchange httpExchange, Map<String, Strin

}

public boolean checkerQueueAlmostFull() {
if (this.executorService instanceof ThreadPoolExecutor) {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) this.executorService;
int maxQueueSize = config.getTextCheckerQueueSize();
int queuesize = threadPoolExecutor.getQueue().size();
if (queuesize > maxQueueSize/2) { //should not happen in normal cases (workQueue.size() == config.getTextCheckerQueueSize())
log.warn("TextChecker queue is almost full requests in queue: {} active request: {}", queuesize, threadPoolExecutor.getActiveCount());
return true;
}
}
return false;
}

@NotNull
private Map<String, Integer> getRuleMatchCount(List<CheckResults> res) {
Map<String, Integer> ruleMatchCount = new HashMap<>();
Expand Down

0 comments on commit 39e1aee

Please sign in to comment.