13
0
geforkt von Mirrors/Velocity

Try to avoid locking, use an actual concurrent data structure

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-09-18 16:40:51 -04:00
Ursprung 037dceb599
Commit 44b1b82b09
2 geänderte Dateien mit 19 neuen und 67 gelöschten Zeilen

Datei anzeigen

@ -7,13 +7,10 @@ import com.velocitypowered.api.proxy.server.ServerInfo;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import java.util.*; import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ServerMap { public class ServerMap {
private final VelocityServer server; private final VelocityServer server;
private final Map<String, RegisteredServer> servers = new HashMap<>(); private final Map<String, RegisteredServer> servers = new HashMap<>();
private final ReadWriteLock lock = new ReentrantReadWriteLock();
public ServerMap(VelocityServer server) { public ServerMap(VelocityServer server) {
this.server = server; this.server = server;
@ -22,47 +19,27 @@ public class ServerMap {
public Optional<RegisteredServer> getServer(String name) { public Optional<RegisteredServer> getServer(String name) {
Preconditions.checkNotNull(name, "server"); Preconditions.checkNotNull(name, "server");
String lowerName = name.toLowerCase(Locale.US); String lowerName = name.toLowerCase(Locale.US);
lock.readLock().lock(); return Optional.ofNullable(servers.get(lowerName));
try {
return Optional.ofNullable(servers.get(lowerName));
} finally {
lock.readLock().unlock();
}
} }
public Collection<RegisteredServer> getAllServers() { public Collection<RegisteredServer> getAllServers() {
lock.readLock().lock(); return ImmutableList.copyOf(servers.values());
try {
return ImmutableList.copyOf(servers.values());
} finally {
lock.readLock().unlock();
}
} }
public RegisteredServer register(ServerInfo serverInfo) { public RegisteredServer register(ServerInfo serverInfo) {
Preconditions.checkNotNull(serverInfo, "serverInfo"); Preconditions.checkNotNull(serverInfo, "serverInfo");
String lowerName = serverInfo.getName().toLowerCase(Locale.US); String lowerName = serverInfo.getName().toLowerCase(Locale.US);
lock.writeLock().lock(); VelocityRegisteredServer rs = new VelocityRegisteredServer(server, serverInfo);
try { Preconditions.checkArgument(servers.putIfAbsent(lowerName, rs) == null, "Server with name %s already registered", serverInfo.getName());
VelocityRegisteredServer rs = new VelocityRegisteredServer(server, serverInfo); return rs;
Preconditions.checkArgument(servers.putIfAbsent(lowerName, rs) == null, "Server with name %s already registered", serverInfo.getName());
return rs;
} finally {
lock.writeLock().unlock();
}
} }
public void unregister(ServerInfo serverInfo) { public void unregister(ServerInfo serverInfo) {
Preconditions.checkNotNull(serverInfo, "serverInfo"); Preconditions.checkNotNull(serverInfo, "serverInfo");
String lowerName = serverInfo.getName().toLowerCase(Locale.US); String lowerName = serverInfo.getName().toLowerCase(Locale.US);
lock.writeLock().lock(); RegisteredServer rs = servers.get(lowerName);
try { Preconditions.checkArgument(rs != null, "Server with name %s is not registered!", serverInfo.getName());
RegisteredServer rs = servers.get(lowerName); Preconditions.checkArgument(rs.getServerInfo().equals(serverInfo), "Trying to remove server %s with differing information", serverInfo.getName());
Preconditions.checkArgument(rs != null, "Server with name %s is not registered!", serverInfo.getName()); Preconditions.checkState(servers.remove(lowerName, rs), "Server with name %s replaced whilst unregistering", serverInfo.getName());
Preconditions.checkArgument(rs.getServerInfo().equals(serverInfo), "Trying to remove server %s with differing information", serverInfo.getName());
servers.remove(lowerName);
} finally {
lock.writeLock().unlock();
}
} }
} }

Datei anzeigen

@ -25,6 +25,7 @@ import io.netty.handler.timeout.ReadTimeoutHandler;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
@ -36,8 +37,7 @@ import static com.velocitypowered.proxy.network.Connections.MINECRAFT_ENCODER;
public class VelocityRegisteredServer implements RegisteredServer { public class VelocityRegisteredServer implements RegisteredServer {
private final VelocityServer server; private final VelocityServer server;
private final ServerInfo serverInfo; private final ServerInfo serverInfo;
private final Set<ConnectedPlayer> players = new HashSet<>(); private final Set<ConnectedPlayer> players = ConcurrentHashMap.newKeySet();
private final ReadWriteLock playersLock = new ReentrantReadWriteLock();
public VelocityRegisteredServer(VelocityServer server, ServerInfo serverInfo) { public VelocityRegisteredServer(VelocityServer server, ServerInfo serverInfo) {
this.server = server; this.server = server;
@ -51,12 +51,7 @@ public class VelocityRegisteredServer implements RegisteredServer {
@Override @Override
public Collection<Player> getPlayersConnected() { public Collection<Player> getPlayersConnected() {
playersLock.readLock().lock(); return ImmutableList.copyOf(players);
try {
return ImmutableList.copyOf(players);
} finally {
playersLock.readLock().unlock();
}
} }
@Override @Override
@ -94,43 +89,23 @@ public class VelocityRegisteredServer implements RegisteredServer {
} }
public void addPlayer(ConnectedPlayer player) { public void addPlayer(ConnectedPlayer player) {
playersLock.writeLock().lock(); players.add(player);
try {
players.add(player);
} finally {
playersLock.writeLock().unlock();
}
} }
public void removePlayer(ConnectedPlayer player) { public void removePlayer(ConnectedPlayer player) {
playersLock.writeLock().lock(); players.remove(player);
try {
players.remove(player);
} finally {
playersLock.writeLock().unlock();
}
} }
@Override @Override
public boolean sendPluginMessage(ChannelIdentifier identifier, byte[] data) { public boolean sendPluginMessage(ChannelIdentifier identifier, byte[] data) {
ServerConnection backendConnection = null; for (ConnectedPlayer player : players) {
playersLock.readLock().lock(); if (player.getConnectedServer() != null && player.getConnectedServer().getServerInfo().equals(serverInfo)) {
try { ServerConnection connection = player.getConnectedServer();
for (ConnectedPlayer player : players) { return connection.sendPluginMessage(identifier, data);
if (player.getConnectedServer() != null && player.getConnectedServer().getServerInfo().equals(serverInfo)) {
backendConnection = player.getConnectedServer();
break;
}
} }
if (backendConnection == null) {
return false;
}
} finally {
playersLock.readLock().unlock();
} }
return backendConnection.sendPluginMessage(identifier, data); return false;
} }
@Override @Override