mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-07 16:01:55 +01:00
Fix heap buffer being used for cached packets
This commit is contained in:
parent
7f0dc6d40a
commit
f3a67f8f50
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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()));
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user