Remove RecordingThreadFactory since it's actually a terrible idea
This commit is contained in:
@@ -9,7 +9,6 @@ import com.velocitypowered.api.event.EventManager;
|
||||
import com.velocitypowered.api.event.PostOrder;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.plugin.PluginManager;
|
||||
import com.velocitypowered.proxy.util.concurrency.RecordingThreadFactory;
|
||||
import net.kyori.event.EventSubscriber;
|
||||
import net.kyori.event.PostResult;
|
||||
import net.kyori.event.SimpleEventBus;
|
||||
@@ -43,14 +42,12 @@ public class VelocityEventManager implements EventManager {
|
||||
new ASMEventExecutorFactory<>(new PluginClassLoader(new URL[0])),
|
||||
new VelocityMethodScanner());
|
||||
private final ExecutorService service;
|
||||
private final RecordingThreadFactory recordingThreadFactory;
|
||||
private final PluginManager pluginManager;
|
||||
|
||||
public VelocityEventManager(PluginManager pluginManager) {
|
||||
this.pluginManager = pluginManager;
|
||||
this.recordingThreadFactory = new RecordingThreadFactory(new ThreadFactoryBuilder()
|
||||
this.service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactoryBuilder()
|
||||
.setNameFormat("Velocity Event Executor - #%d").setDaemon(true).build());
|
||||
this.service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), recordingThreadFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,17 +85,11 @@ public class VelocityEventManager implements EventManager {
|
||||
logger.error("Some errors occurred whilst posting event {}.", event);
|
||||
int i = 0;
|
||||
for (Throwable exception : result.exceptions().values()) {
|
||||
logger.error("#{}: \n", i++, exception);
|
||||
logger.error("#{}: \n", ++i, exception);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (recordingThreadFactory.currentlyInFactory()) {
|
||||
// Optimization: fire the event immediately, we are on the event handling thread.
|
||||
runEvent.run();
|
||||
return CompletableFuture.completedFuture(event);
|
||||
}
|
||||
|
||||
CompletableFuture<E> eventFuture = new CompletableFuture<>();
|
||||
service.execute(() -> {
|
||||
runEvent.run();
|
||||
|
@@ -1,44 +0,0 @@
|
||||
package com.velocitypowered.proxy.util.concurrency;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.MapMaker;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
/**
|
||||
* A {@link ThreadFactory} that records the threads it has created. Once a thread terminates, it is automatically removed
|
||||
* from the recorder.
|
||||
*/
|
||||
public class RecordingThreadFactory implements ThreadFactory {
|
||||
private final ThreadFactory backing;
|
||||
private final Set<Thread> threads = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
|
||||
|
||||
public RecordingThreadFactory(ThreadFactory backing) {
|
||||
this.backing = Preconditions.checkNotNull(backing, "backing");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Thread newThread(Runnable runnable) {
|
||||
Preconditions.checkNotNull(runnable, "runnable");
|
||||
return backing.newThread(() -> {
|
||||
threads.add(Thread.currentThread());
|
||||
try {
|
||||
runnable.run();
|
||||
} finally {
|
||||
threads.remove(Thread.currentThread());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean currentlyInFactory() {
|
||||
return threads.contains(Thread.currentThread());
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
int size() {
|
||||
return threads.size();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user