Fix heap buffer being used for cached packets

This commit is contained in:
themode 2021-03-29 20:31:06 +02:00
parent 7f0dc6d40a
commit f3a67f8f50
4 changed files with 9 additions and 8 deletions

View File

@ -30,7 +30,8 @@ import java.util.concurrent.TimeUnit;
public class ChunkDataPacket implements ServerPacket, CacheablePacket {
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
private static final TemporaryCache<TimedBuffer> CACHE = new TemporaryCache<>(5, TimeUnit.MINUTES);
private static final TemporaryCache<TimedBuffer> CACHE = new TemporaryCache<>(5, TimeUnit.MINUTES,
notification -> notification.getValue().getBuffer().release());
public boolean fullChunk;
public Biome[] biomes;

View File

@ -15,7 +15,8 @@ import java.util.concurrent.TimeUnit;
public class UpdateLightPacket implements ServerPacket, CacheablePacket {
private static final TemporaryCache<TimedBuffer> CACHE = new TemporaryCache<>(5, TimeUnit.MINUTES);
private static final TemporaryCache<TimedBuffer> CACHE = new TemporaryCache<>(5, TimeUnit.MINUTES,
notification -> notification.getValue().getBuffer().release());
public int chunkX;
public int chunkZ;

View File

@ -153,13 +153,10 @@ public class NettyPlayerConnection extends PlayerConnection {
timestamp > timedBuffer.getTimestamp();
if (shouldUpdate) {
final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, false);
TimedBuffer oldBuffer = timedBuffer;
// Buffer freed by guava cache #removalListener
final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, true);
timedBuffer = new TimedBuffer(buffer, timestamp);
temporaryCache.cache(identifier, timedBuffer);
if (oldBuffer != null) {
oldBuffer.getBuffer().release();
}
}
write(new FramedPacket(timedBuffer.getBuffer()));

View File

@ -2,6 +2,7 @@ package net.minestom.server.utils.cache;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -22,10 +23,11 @@ public class TemporaryCache<T> {
*
* @param duration the time before considering an object unused
*/
public TemporaryCache(long duration, TimeUnit timeUnit) {
public TemporaryCache(long duration, TimeUnit timeUnit, RemovalListener<UUID, T> removalListener) {
this.cache = CacheBuilder.newBuilder()
.expireAfterWrite(duration, timeUnit)
.softValues()
.removalListener(removalListener)
.build();
}