Better memory management for the out buffer

This commit is contained in:
2016-07-27 07:38:39 +02:00
parent 48ff975ffd
commit 56ebb7b8fa
3 changed files with 52 additions and 24 deletions

View File

@ -35,13 +35,13 @@ import java.util.Set;
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.CLIENT;
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.SYNC;
import static ch.dissem.bitmessage.ports.NetworkHandler.MAX_MESSAGE_SIZE;
/**
* Represents the current state of a connection.
*/
public class ConnectionInfo extends AbstractConnection {
private ByteBuffer out = ByteBuffer.allocate(MAX_MESSAGE_SIZE);
private final ByteBuffer headerOut = ByteBuffer.allocate(24);
private ByteBuffer payloadOut;
private V3MessageReader reader = new V3MessageReader();
private boolean syncFinished;
private long lastUpdate = Long.MAX_VALUE;
@ -50,7 +50,7 @@ public class ConnectionInfo extends AbstractConnection {
NetworkAddress node, NetworkHandler.MessageListener listener,
Set<InventoryVector> commonRequestedObjects, long syncTimeout) {
super(context, mode, node, listener, commonRequestedObjects, syncTimeout);
out.flip();
headerOut.flip();
if (mode == CLIENT || mode == SYNC) {
send(new Version.Builder().defaults(peerNonce).addrFrom(host).addrRecv(node).build());
}
@ -76,17 +76,23 @@ public class ConnectionInfo extends AbstractConnection {
}
public void updateWriter() {
if ((out == null || !out.hasRemaining()) && !sendingQueue.isEmpty()) {
out.clear();
if (!headerOut.hasRemaining() && !sendingQueue.isEmpty()) {
headerOut.clear();
MessagePayload payload = sendingQueue.poll();
new NetworkMessage(payload).write(out);
out.flip();
payloadOut = new NetworkMessage(payload).writeHeaderAndGetPayloadBuffer(headerOut);
headerOut.flip();
lastUpdate = System.currentTimeMillis();
}
}
public ByteBuffer getOutBuffer() {
return out;
public ByteBuffer[] getOutBuffers() {
return new ByteBuffer[]{headerOut, payloadOut};
}
public void cleanupBuffers() {
if (payloadOut != null && !payloadOut.hasRemaining()) {
payloadOut = null;
}
}
public void updateReader() {

View File

@ -36,10 +36,7 @@ import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.*;
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.*;
import static ch.dissem.bitmessage.networking.AbstractConnection.State.ACTIVE;
@ -66,7 +63,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
private InternalContext ctx;
private Selector selector;
private ServerSocketChannel serverChannel;
private Map<ConnectionInfo, SelectionKey> connections = synchronizedMap(new WeakHashMap<ConnectionInfo, SelectionKey>());
private Map<ConnectionInfo, SelectionKey> connections = new ConcurrentHashMap<>();
private int requestedObjectsCount;
private Thread starter;
@ -81,13 +78,11 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
ConnectionInfo connection = new ConnectionInfo(ctx, SYNC,
new NetworkAddress.Builder().ip(server).port(port).stream(1).build(),
listener, new HashSet<InventoryVector>(), timeoutInSeconds);
connections.put(connection, null);
while (channel.isConnected() && !connection.isSyncFinished()) {
write(channel, connection);
read(channel, connection);
Thread.sleep(10);
}
connections.remove(connection);
LOG.info("Synchronization finished");
}
return null;
@ -302,16 +297,21 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
private static void write(SocketChannel channel, ConnectionInfo connection)
throws IOException {
writeBuffer(connection.getOutBuffer(), channel);
writeBuffer(connection.getOutBuffers(), channel);
connection.updateWriter();
writeBuffer(connection.getOutBuffer(), channel);
writeBuffer(connection.getOutBuffers(), channel);
connection.cleanupBuffers();
}
private static void writeBuffer(ByteBuffer buffer, SocketChannel channel) throws IOException {
if (buffer != null && buffer.hasRemaining()) {
channel.write(buffer);
private static void writeBuffer(ByteBuffer[] buffers, SocketChannel channel) throws IOException {
if (buffers[1] == null) {
if (buffers[0].hasRemaining()) {
channel.write(buffers[0]);
}
} else if (buffers[1].hasRemaining() || buffers[0].hasRemaining()) {
channel.write(buffers);
}
}