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

feat: Add server registered/unregistered events #1386

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2024 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/

package com.velocitypowered.api.event.proxy.server;

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import org.jetbrains.annotations.NotNull;

/**
* This event is fired by the proxy after a backend server is registered to the server map.
* Currently, it may occur when a server is registered dynamically at runtime or when a server is
* replaced due to configuration reload.
*
* @see com.velocitypowered.api.proxy.ProxyServer#registerServer(ServerInfo)
Twarug marked this conversation as resolved.
Show resolved Hide resolved
*
* @param registeredServer A registeredServer that just has been registered.
Twarug marked this conversation as resolved.
Show resolved Hide resolved
* @since 3.3.0
*/
@Beta
public record ServerRegisteredEvent(@NotNull RegisteredServer registeredServer) {
Twarug marked this conversation as resolved.
Show resolved Hide resolved
public ServerRegisteredEvent {
Preconditions.checkNotNull(registeredServer, "registeredServer");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2024 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/

package com.velocitypowered.api.event.proxy.server;

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import org.jetbrains.annotations.NotNull;

/**
* This event is fired by the proxy after a backend server is unregistered from the server map.
* Currently, it may occur when a server is unregistered dynamically at runtime
* or when a server is replaced due to configuration reload.
*
* @see com.velocitypowered.api.proxy.ProxyServer#unregisterServer(ServerInfo)
Twarug marked this conversation as resolved.
Show resolved Hide resolved
*
* @param unregisteredServer A registeredServer that just has been unregistered.
Twarug marked this conversation as resolved.
Show resolved Hide resolved
* @since 3.3.0
*/
@Beta
public record ServerUnregisteredEvent(@NotNull RegisteredServer unregisteredServer) {
Twarug marked this conversation as resolved.
Show resolved Hide resolved
public ServerUnregisteredEvent {
Preconditions.checkNotNull(unregisteredServer, "unregisteredServer");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.event.proxy.server.ServerRegisteredEvent;
import com.velocitypowered.api.event.proxy.server.ServerUnregisteredEvent;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
import com.velocitypowered.proxy.VelocityServer;
Expand Down Expand Up @@ -84,6 +86,10 @@ public RegisteredServer register(ServerInfo serverInfo) {
throw new IllegalArgumentException(
"Server with name " + serverInfo.getName() + " already registered");
} else if (existing == null) {
if (server != null) {
server.getEventManager().fireAndForget(new ServerRegisteredEvent(rs));
}

return rs;
} else {
return existing;
Expand All @@ -107,5 +113,9 @@ public void unregister(ServerInfo serverInfo) {
"Trying to remove server %s with differing information", serverInfo.getName());
Preconditions.checkState(servers.remove(lowerName, rs),
"Server with name %s replaced whilst unregistering", serverInfo.getName());

if (server != null) {
server.getEventManager().fireAndForget(new ServerUnregisteredEvent(rs));
}
}
}