Added timeout for the server checker CUSTOM tactic

This commit is contained in:
Jaime Martínez Rincón 2017-11-04 15:30:56 +01:00
parent 0eacf4da31
commit 5f7b7deb48
5 changed files with 19 additions and 15 deletions

View File

@ -3,15 +3,11 @@ package com.jaimemartz.playerbalancer.connection;
import com.jaimemartz.playerbalancer.PlayerBalancer; import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider; import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
import com.jaimemartz.playerbalancer.connection.provider.types.*; import com.jaimemartz.playerbalancer.connection.provider.types.*;
import com.jaimemartz.playerbalancer.ping.ServerStatus;
import com.jaimemartz.playerbalancer.section.ServerSection; import com.jaimemartz.playerbalancer.section.ServerSection;
import com.jaimemartz.playerbalancer.settings.props.features.BalancerProps;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public enum ProviderType { public enum ProviderType {
NONE { NONE {

View File

@ -16,7 +16,9 @@ public enum PingTactic {
public void ping(ServerInfo server, Callback<ServerStatus> callback, PlayerBalancer plugin) { public void ping(ServerInfo server, Callback<ServerStatus> callback, PlayerBalancer plugin) {
plugin.getProxy().getScheduler().runAsync(plugin, () -> { plugin.getProxy().getScheduler().runAsync(plugin, () -> {
try { try {
StatusResponse response = utility.ping(server.getAddress()); StatusResponse response = utility.ping(
server.getAddress(),
plugin.getSettings().getServerCheckerProps().getTimeout());
callback.done(new ServerStatus( callback.done(new ServerStatus(
response.getDescription().toLegacyText(), response.getDescription().toLegacyText(),
response.getPlayers().getOnline(), response.getPlayers().getOnline(),

View File

@ -20,6 +20,9 @@ public class ServerCheckerProps {
@Setting @Setting
private int interval; private int interval;
@Setting
private int timeout;
@Setting(value = "marker-descs") @Setting(value = "marker-descs")
private List<String> markerDescs; private List<String> markerDescs;
@ -58,6 +61,14 @@ public class ServerCheckerProps {
this.interval = interval; this.interval = interval;
} }
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public List<String> getMarkerDescs() { public List<String> getMarkerDescs() {
return markerDescs; return markerDescs;
} }
@ -81,6 +92,7 @@ public class ServerCheckerProps {
", tactic=" + tactic + ", tactic=" + tactic +
", attempts=" + attempts + ", attempts=" + attempts +
", interval=" + interval + ", interval=" + interval +
", timeout=" + timeout +
", markerDescs=" + markerDescs + ", markerDescs=" + markerDescs +
", debug=" + debug + ", debug=" + debug +
'}'; '}';

View File

@ -17,7 +17,6 @@ import java.net.Socket;
import java.util.List; import java.util.List;
public final class ServerListPing { public final class ServerListPing {
private int timeout = 7000;
private static Gson gson = new GsonBuilder() private static Gson gson = new GsonBuilder()
.registerTypeAdapter(BaseComponent.class, new ComponentSerializer()) .registerTypeAdapter(BaseComponent.class, new ComponentSerializer())
.registerTypeAdapter(TextComponent.class, new TextComponentSerializer()) .registerTypeAdapter(TextComponent.class, new TextComponentSerializer())
@ -48,7 +47,7 @@ public final class ServerListPing {
} }
} }
public StatusResponse ping(InetSocketAddress host) throws IOException { public StatusResponse ping(InetSocketAddress host, int timeout) throws IOException {
try (Socket socket = new Socket()) { try (Socket socket = new Socket()) {
socket.setSoTimeout(timeout); socket.setSoTimeout(timeout);
socket.connect(host, timeout); socket.connect(host, timeout);
@ -118,14 +117,6 @@ public final class ServerListPing {
} }
} }
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public int getTimeout() {
return timeout;
}
public static class StatusResponse { public static class StatusResponse {
private BaseComponent description; private BaseComponent description;
private Players players; private Players players;

View File

@ -132,6 +132,9 @@ features {
# The interval between every round of checks (in milliseconds) # The interval between every round of checks (in milliseconds)
interval=10000 interval=10000
# The timeout of a ping, only applied to the CUSTOM tactic
timeout=7000
# When true, the plugin will print useful info when a server gets checked # When true, the plugin will print useful info when a server gets checked
debug-info=false debug-info=false