Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: moved packet write to EventLoop to ensure packet ordering. #1

Open
wants to merge 20 commits into
base: feature/tablistupdate-event
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reintroduce sync event execution to the Velocity event system
This required a not-insubstantial number of bug fixes, since the sync support had bit-rotted somewhat. This PR also corrects a number of bugs.

Finally. the per-plugin executor services are now used to execute all async event tasks.
  • Loading branch information
astei committed Sep 16, 2024
commit 4f227badc20dc30b0f6d84b5349c8809481dcbb1
18 changes: 18 additions & 0 deletions api/src/main/java/com/velocitypowered/api/event/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,28 @@ default <E> void register(Object plugin, Class<E> eventClass, EventHandler<E> ha
* @param postOrder the order in which events should be posted to the handler
* @param handler the handler to register
* @param <E> the event type to handle
* @deprecated use {@link #register(Object, Class, short, EventHandler)} instead
*/
@Deprecated
<E> void register(Object plugin, Class<E> eventClass, PostOrder postOrder,
EventHandler<E> handler);

/**
* Requests that the specified {@code handler} listen for events and associate it with the {@code
* plugin}.
*
* <p>Note that this method will register a non-asynchronous listener by default. If you want to
* use an asynchronous event handler, return {@link EventTask#async(Runnable)} from the handler.</p>
*
* @param plugin the plugin to associate with the handler
* @param eventClass the class for the event handler to register
* @param postOrder the relative order in which events should be posted to the handler
* @param handler the handler to register
* @param <E> the event type to handle
*/
<E> void register(Object plugin, Class<E> eventClass, short postOrder,
EventHandler<E> handler);

/**
* Fires the specified event to the event bus asynchronously. This allows Velocity to continue
* servicing connections while a plugin handles a potentially long-running operation such as a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
*/
public enum PostOrder {

FIRST, EARLY, NORMAL, LATE, LAST
FIRST, EARLY, NORMAL, LATE, LAST, CUSTOM

}
34 changes: 24 additions & 10 deletions api/src/main/java/com/velocitypowered/api/event/Subscribe.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,38 @@
/**
* The order events will be posted to this listener.
*
* @deprecated specify the order using {@link #priority()} instead
* @return the order
*/
@Deprecated
PostOrder order() default PostOrder.NORMAL;

/**
* Whether the handler must be called asynchronously.
* The priority of this event handler. Priorities are used to determine the order in which event
* handlers are called. The higher the priority, the earlier the event handler will be called.
*
* <p><strong>This option currently has no effect, but in the future it will. In Velocity 3.0.0,
* all event handlers run asynchronously by default. You are encouraged to determine whether or
* not to enable it now. This option is being provided as a migration aid.</strong></p>
* <p>Note that due to compatibility constraints, you must specify {@link PostOrder#CUSTOM}
* in order to use this field.</p>
*
* <p>If this method returns {@code true}, the method is guaranteed to be executed
* asynchronously. Otherwise, the handler may be executed on the current thread or
* asynchronously. <strong>This still means you must consider thread-safety in your
* event listeners</strong> as the "current thread" can and will be different each time.</p>
* @return the priority
*/
short priority() default Short.MIN_VALUE;

/**
* Whether the handler must be called asynchronously. By default, all event handlers are called
* asynchronously.
*
* <p>For performance (for instance, if you use {@link EventTask#withContinuation}), you can
* optionally specify <code>false</code>. This option will become {@code false} by default
* in a future release of Velocity.</p>
*
* <p>If this is {@code true}, the method is guaranteed to be executed asynchronously. Otherwise,
* the handler may be executed on the current thread or asynchronously. <strong>This still means
* you must consider thread-safety in your event listeners</strong> as the "current thread" can
* and will be different each time.</p>
*
* <p>If any method handler targeting an event type is marked with {@code true}, then every
* handler targeting that event type will be executed asynchronously.</p>
* <p>Note that if any method handler targeting an event type is marked with {@code true}, then
* every handler targeting that event type will be executed asynchronously.</p>
*
* @return Requires async
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,6 @@ public void shutdown(boolean explicitExit, Component reason) {

eventManager.fire(new ProxyShutdownEvent()).join();

timedOut = !eventManager.shutdown() || timedOut;
timedOut = !scheduler.shutdown() || timedOut;

if (timedOut) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
Expand All @@ -71,6 +73,7 @@ public class VelocityCommandManager implements CommandManager {
private final SuggestionsProvider<CommandSource> suggestionsProvider;
private final CommandGraphInjector<CommandSource> injector;
private final Map<String, CommandMeta> commandMetas;
private final ExecutorService asyncExecutor;

/**
* Constructs a command manager.
Expand All @@ -89,6 +92,7 @@ public VelocityCommandManager(final VelocityEventManager eventManager) {
this.suggestionsProvider = new SuggestionsProvider<>(this.dispatcher, this.lock.readLock());
this.injector = new CommandGraphInjector<>(this.dispatcher, this.lock.readLock());
this.commandMetas = new ConcurrentHashMap<>();
this.asyncExecutor = ForkJoinPool.commonPool(); // TODO: remove entirely
}

public void setAnnounceProxyCommands(boolean announceProxyCommands) {
Expand Down Expand Up @@ -266,7 +270,7 @@ public CompletableFuture<Boolean> executeAsync(final CommandSource source, final
return false;
}
return executeImmediately0(source, commandResult.getCommand().orElse(event.getCommand()));
}, eventManager.getAsyncExecutor());
}, asyncExecutor);
}

@Override
Expand All @@ -275,8 +279,7 @@ public CompletableFuture<Boolean> executeImmediatelyAsync(
Preconditions.checkNotNull(source, "source");
Preconditions.checkNotNull(cmdLine, "cmdLine");

return CompletableFuture.supplyAsync(
() -> executeImmediately0(source, cmdLine), eventManager.getAsyncExecutor());
return CompletableFuture.supplyAsync(() -> executeImmediately0(source, cmdLine), asyncExecutor);
}

/**
Expand Down
Loading