#2710: Store queue of pending keepalives

This allows the server to send them at a rate greater than the client reply time.
This commit is contained in:
polo1k 2020-01-24 13:13:01 +11:00 committed by md_5
parent 3f6aa0336c
commit eeb374798b
4 changed files with 22 additions and 9 deletions

View File

@ -3,6 +3,9 @@ package net.md_5.bungee;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.LinkedList;
import java.util.Queue;
import lombok.Data;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
@ -26,8 +29,7 @@ public class ServerConnection implements Server
@Getter @Getter
private final boolean forgeServer = false; private final boolean forgeServer = false;
@Getter @Getter
@Setter private final Queue<KeepAliveData> keepAlives = new LinkedList<>();
private long sentPingId = -1;
private final Unsafe unsafe = new Unsafe() private final Unsafe unsafe = new Unsafe()
{ {
@ -87,4 +89,12 @@ public class ServerConnection implements Server
{ {
return unsafe; return unsafe;
} }
@Data
public static class KeepAliveData
{
private final long id;
private final long time;
}
} }

View File

@ -93,9 +93,6 @@ public final class UserConnection implements ProxiedPlayer
/*========================================================================*/ /*========================================================================*/
@Getter @Getter
@Setter @Setter
private long sentPingTime;
@Getter
@Setter
private int ping = 100; private int ping = 100;
@Getter @Getter
@Setter @Setter

View File

@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.md_5.bungee.ServerConnection; import net.md_5.bungee.ServerConnection;
import net.md_5.bungee.ServerConnection.KeepAliveData;
import net.md_5.bungee.UserConnection; import net.md_5.bungee.UserConnection;
import net.md_5.bungee.Util; import net.md_5.bungee.Util;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
@ -124,8 +125,10 @@ public class DownstreamBridge extends PacketHandler
@Override @Override
public void handle(KeepAlive alive) throws Exception public void handle(KeepAlive alive) throws Exception
{ {
server.setSentPingId( alive.getRandomId() ); if ( server.getKeepAlives().size() < bungee.getConfig().getTimeout() / 50 ) // Allow a theoretical maximum of 1 keepalive per tick
con.setSentPingTime( System.currentTimeMillis() ); {
server.getKeepAlives().add( new KeepAliveData( alive.getRandomId(), System.currentTimeMillis() ) );
}
} }
@Override @Override

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import net.md_5.bungee.BungeeCord; import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ServerConnection.KeepAliveData;
import net.md_5.bungee.UserConnection; import net.md_5.bungee.UserConnection;
import net.md_5.bungee.Util; import net.md_5.bungee.Util;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
@ -121,9 +122,11 @@ public class UpstreamBridge extends PacketHandler
@Override @Override
public void handle(KeepAlive alive) throws Exception public void handle(KeepAlive alive) throws Exception
{ {
if ( alive.getRandomId() == con.getServer().getSentPingId() ) KeepAliveData keepAliveData = con.getServer().getKeepAlives().poll();
if ( keepAliveData != null && alive.getRandomId() == keepAliveData.getId() )
{ {
int newPing = (int) ( System.currentTimeMillis() - con.getSentPingTime() ); int newPing = (int) ( System.currentTimeMillis() - keepAliveData.getTime() );
con.getTabListHandler().onPingChange( newPing ); con.getTabListHandler().onPingChange( newPing );
con.setPing( newPing ); con.setPing( newPing );
} else } else