diff --git a/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java b/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java index 40ffa7793..7a8dbda40 100644 --- a/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java +++ b/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java @@ -56,6 +56,14 @@ public interface ProxyConfig */ boolean isLogCommands(); + /** + * Time in milliseconds to cache server list info from a ping request from + * the proxy to a server. + * + * @return cache time + */ + int getRemotePingCache(); + /** * Returns the player max. * diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java b/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java index cab2c21b4..b7452a297 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java @@ -123,6 +123,18 @@ public class BungeeServerInfo implements ServerInfo } } + private long lastPing; + private ServerPing cachedPing; + + public void cachePing(ServerPing serverPing) + { + if ( ProxyServer.getInstance().getConfig().getRemotePingCache() > 0 ) + { + this.cachedPing = serverPing; + this.lastPing = System.currentTimeMillis(); + } + } + @Override public InetSocketAddress getAddress() { @@ -139,6 +151,18 @@ public class BungeeServerInfo implements ServerInfo { Preconditions.checkNotNull( callback, "callback" ); + int pingCache = ProxyServer.getInstance().getConfig().getRemotePingCache(); + if ( pingCache > 0 && cachedPing != null && ( lastPing - System.currentTimeMillis() ) > pingCache ) + { + cachedPing = null; + } + + if ( cachedPing != null ) + { + callback.done( cachedPing, null ); + return; + } + ChannelFutureListener listener = new ChannelFutureListener() { @Override diff --git a/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java b/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java index 49638c3aa..82ba6afb0 100644 --- a/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java +++ b/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java @@ -52,6 +52,7 @@ public class Configuration implements ProxyConfig */ private boolean logCommands; private boolean logPings = true; + private int remotePingCache = -1; private int playerLimit = -1; private Collection disabledCommands; private int throttle = 4000; @@ -85,6 +86,7 @@ public class Configuration implements ProxyConfig onlineMode = adapter.getBoolean( "online_mode", onlineMode ); logCommands = adapter.getBoolean( "log_commands", logCommands ); logPings = adapter.getBoolean( "log_pings", logPings ); + remotePingCache = adapter.getInt( "remote_ping_cache", remotePingCache ); playerLimit = adapter.getInt( "player_limit", playerLimit ); throttle = adapter.getInt( "connection_throttle", throttle ); throttleLimit = adapter.getInt( "connection_throttle_limit", throttleLimit ); diff --git a/proxy/src/main/java/net/md_5/bungee/connection/PingHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/PingHandler.java index 6bc14d675..6df3f3dd9 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/PingHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/PingHandler.java @@ -4,6 +4,7 @@ import com.google.gson.Gson; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import lombok.RequiredArgsConstructor; import net.md_5.bungee.BungeeCord; +import net.md_5.bungee.BungeeServerInfo; import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ServerPing; @@ -65,7 +66,9 @@ public class PingHandler extends PacketHandler public void handle(StatusResponse statusResponse) throws Exception { Gson gson = BungeeCord.getInstance().gson; - callback.done( gson.fromJson( statusResponse.getResponse(), ServerPing.class ), null ); + ServerPing serverPing = gson.fromJson( statusResponse.getResponse(), ServerPing.class ); + ( (BungeeServerInfo) target ).cachePing( serverPing ); + callback.done( serverPing, null ); channel.close(); }