diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-05-11 21:44:26 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-05-11 21:44:26 +0200 |
commit | 8e57505ad9fd020f2f0241c06e5b57f20da248d2 (patch) | |
tree | d3047f8f045d4a662e533057cef4b329da57037c /container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/Janitor.java | |
parent | ae03641a1c2ec122051ddd01f2dd398f87cc0c46 (diff) |
Janitor threadpool must be available across JettyHttpServer instances
Diffstat (limited to 'container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/Janitor.java')
-rw-r--r-- | container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/Janitor.java | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/Janitor.java b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/Janitor.java new file mode 100644 index 00000000000..cd2b9ca23c0 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/Janitor.java @@ -0,0 +1,46 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jdisc.http.server.jetty; + +import com.google.inject.Inject; +import com.yahoo.component.AbstractComponent; +import com.yahoo.concurrent.DaemonThreadFactory; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; + +/** + * Separate janitor threadpool for tasks that cannot be executed on the jdisc default threadpool due to risk of deadlock. + * Modelled as a separate component as the underlying executor must be available across {@link JettyHttpServer} instances. + * + * @author bjorncs + */ +public class Janitor extends AbstractComponent { + + private static final Logger log = Logger.getLogger(Janitor.class.getName()); + + private final ExecutorService executor; + + @Inject + public Janitor() { + int threadPoolSize = Math.max(2, Runtime.getRuntime().availableProcessors()/4); + log.info("Creating janitor executor with " + threadPoolSize + " threads"); + this.executor = Executors.newFixedThreadPool(threadPoolSize, new DaemonThreadFactory("jdisc-janitor-")); + } + + public void scheduleTask(Runnable task) { executor.execute(task); } + + @Override + public void deconstruct() { + try { + executor.shutdown(); + if (!executor.awaitTermination(10, TimeUnit.SECONDS)) { + log.warning("Failed to shutdown janitor in time"); + } + } catch (InterruptedException e) { + log.warning("Interrupted while shutting down janitor"); + Thread.currentThread().interrupt(); + } + } +} |