#2753: Add configurable remote ping caching

This commit is contained in:
Mystiflow 2020-01-24 10:48:50 +11:00 committed by md_5
parent a4512e50fb
commit 636c020772
4 changed files with 38 additions and 1 deletions

View File

@ -56,6 +56,14 @@ public interface ProxyConfig
*/ */
boolean isLogCommands(); 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. * Returns the player max.
* *

View File

@ -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 @Override
public InetSocketAddress getAddress() public InetSocketAddress getAddress()
{ {
@ -139,6 +151,18 @@ public class BungeeServerInfo implements ServerInfo
{ {
Preconditions.checkNotNull( callback, "callback" ); 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() ChannelFutureListener listener = new ChannelFutureListener()
{ {
@Override @Override

View File

@ -52,6 +52,7 @@ public class Configuration implements ProxyConfig
*/ */
private boolean logCommands; private boolean logCommands;
private boolean logPings = true; private boolean logPings = true;
private int remotePingCache = -1;
private int playerLimit = -1; private int playerLimit = -1;
private Collection<String> disabledCommands; private Collection<String> disabledCommands;
private int throttle = 4000; private int throttle = 4000;
@ -85,6 +86,7 @@ public class Configuration implements ProxyConfig
onlineMode = adapter.getBoolean( "online_mode", onlineMode ); onlineMode = adapter.getBoolean( "online_mode", onlineMode );
logCommands = adapter.getBoolean( "log_commands", logCommands ); logCommands = adapter.getBoolean( "log_commands", logCommands );
logPings = adapter.getBoolean( "log_pings", logPings ); logPings = adapter.getBoolean( "log_pings", logPings );
remotePingCache = adapter.getInt( "remote_ping_cache", remotePingCache );
playerLimit = adapter.getInt( "player_limit", playerLimit ); playerLimit = adapter.getInt( "player_limit", playerLimit );
throttle = adapter.getInt( "connection_throttle", throttle ); throttle = adapter.getInt( "connection_throttle", throttle );
throttleLimit = adapter.getInt( "connection_throttle_limit", throttleLimit ); throttleLimit = adapter.getInt( "connection_throttle_limit", throttleLimit );

View File

@ -4,6 +4,7 @@ import com.google.gson.Gson;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.md_5.bungee.BungeeCord; 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.Callback;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.ServerPing; import net.md_5.bungee.api.ServerPing;
@ -65,7 +66,9 @@ public class PingHandler extends PacketHandler
public void handle(StatusResponse statusResponse) throws Exception public void handle(StatusResponse statusResponse) throws Exception
{ {
Gson gson = BungeeCord.getInstance().gson; 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(); channel.close();
} }