Skip to content

Commit

Permalink
minor improvements, inject ExecutorService into plugin class if reque…
Browse files Browse the repository at this point in the history
…sted
  • Loading branch information
astei committed May 14, 2023
1 parent 256e95c commit afe738e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ default Optional<?> getInstance() {
*
* @return an {@link ExecutorService} associated with this plugin
*/
ExecutorService getService();
ExecutorService getExecutorService();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ public void setInstance(Object instance) {
}

@Override
public ExecutorService getService() {
public ExecutorService getExecutorService() {
if (this.service == null) {
synchronized (this) {
if (this.service == null) {
String name = this.description.getName().orElse(this.description.getId());
this.service = Executors.unconfigurableExecutorService(
Executors.newCachedThreadPool(
new ThreadFactoryBuilder().setDaemon(true)
.setNameFormat(description.getId() + " - Task Scheduler #%d")
.setNameFormat(name + " - Task Executor #%d")
.build()
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Scopes;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -52,5 +54,7 @@ public void configure(Binder binder) {
.toInstance(basePluginPath.resolve(description.getId()));
binder.bind(PluginDescription.class).toInstance(description);
binder.bind(PluginContainer.class).toInstance(pluginContainer);

binder.bind(ExecutorService.class).toProvider(pluginContainer::getExecutorService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public boolean shutdown() throws InterruptedException {
timerExecutionService.shutdown();
for (final PluginContainer container : this.pluginManager.getPlugins()) {
if (container instanceof VelocityPluginContainer) {
(container).getService().shutdown();
(container).getExecutorService().shutdown();
}
}

Expand All @@ -128,7 +128,7 @@ public boolean shutdown() throws InterruptedException {
continue;
}
final String id = container.getDescription().getId();
final ExecutorService service = (container).getService();
final ExecutorService service = (container).getExecutorService();

try {
if (!service.awaitTermination(10, TimeUnit.SECONDS)) {
Expand Down Expand Up @@ -268,7 +268,7 @@ public void cancel() {

@Override
public void run() {
container.getService().execute(() -> {
container.getExecutorService().execute(() -> {
currentTaskThread = Thread.currentThread();
try {
if (runnable != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Optional<?> getInstance() {
}

@Override
public ExecutorService getService() {
public ExecutorService getExecutorService() {
return service;
}
}
Expand Down

0 comments on commit afe738e

Please sign in to comment.