Use the transport's idle timeout, not a hardcoded value.

This commit is contained in:
akwizgran
2014-12-14 15:18:39 +00:00
parent d4fa656dbb
commit 29a6596ee3
15 changed files with 77 additions and 26 deletions

View File

@@ -55,7 +55,6 @@ import org.briarproject.api.messaging.TransportUpdate;
*/
class DuplexOutgoingSession implements MessagingSession, EventListener {
private static final int MAX_IDLE_TIME = 30 * 1000; // Milliseconds
private static final Logger LOG =
Logger.getLogger(DuplexOutgoingSession.class.getName());
@@ -69,7 +68,7 @@ class DuplexOutgoingSession implements MessagingSession, EventListener {
private final EventBus eventBus;
private final ContactId contactId;
private final TransportId transportId;
private final long maxLatency;
private final long maxLatency, maxIdleTime;
private final OutputStream out;
private final PacketWriter packetWriter;
private final BlockingQueue<ThrowingRunnable<IOException>> writerTasks;
@@ -79,13 +78,14 @@ class DuplexOutgoingSession implements MessagingSession, EventListener {
DuplexOutgoingSession(DatabaseComponent db, Executor dbExecutor,
EventBus eventBus, PacketWriterFactory packetWriterFactory,
ContactId contactId, TransportId transportId, long maxLatency,
OutputStream out) {
long maxIdleTime, OutputStream out) {
this.db = db;
this.dbExecutor = dbExecutor;
this.eventBus = eventBus;
this.contactId = contactId;
this.transportId = transportId;
this.maxLatency = maxLatency;
this.maxIdleTime = maxIdleTime;
this.out = out;
packetWriter = packetWriterFactory.createPacketWriter(out);
writerTasks = new LinkedBlockingQueue<ThrowingRunnable<IOException>>();
@@ -110,8 +110,8 @@ class DuplexOutgoingSession implements MessagingSession, EventListener {
while(!interrupted) {
// Flush the stream if it's going to be idle
if(writerTasks.isEmpty()) out.flush();
ThrowingRunnable<IOException> task = writerTasks.poll(
MAX_IDLE_TIME, MILLISECONDS);
ThrowingRunnable<IOException> task =
writerTasks.poll(maxIdleTime, MILLISECONDS);
if(task == null) {
LOG.info("Idle timeout");
continue; // Flush and wait again

View File

@@ -49,11 +49,16 @@ class MessagingSessionFactoryImpl implements MessagingSessionFactory {
messageVerifier, packetReaderFactory, c, t, in);
}
public MessagingSession createOutgoingSession(ContactId c, TransportId t,
long maxLatency, boolean duplex, OutputStream out) {
if(duplex) return new DuplexOutgoingSession(db, dbExecutor, eventBus,
packetWriterFactory, c, t, maxLatency, out);
else return new SimplexOutgoingSession(db, dbExecutor, eventBus,
public MessagingSession createSimplexOutgoingSession(ContactId c,
TransportId t, long maxLatency, OutputStream out) {
return new SimplexOutgoingSession(db, dbExecutor, eventBus,
packetWriterFactory, c, t, maxLatency, out);
}
public MessagingSession createDuplexOutgoingSession(ContactId c,
TransportId t, long maxLatency, long maxIdleTime,
OutputStream out) {
return new DuplexOutgoingSession(db, dbExecutor, eventBus,
packetWriterFactory, c, t, maxLatency, maxIdleTime, out);
}
}

View File

@@ -107,14 +107,27 @@ class ConnectionManagerImpl implements ConnectionManager {
}
}
private MessagingSession createOutgoingSession(StreamContext ctx,
TransportConnectionWriter w, boolean duplex) throws IOException {
private MessagingSession createSimplexOutgoingSession(StreamContext ctx,
TransportConnectionWriter w) throws IOException {
try {
StreamWriter streamWriter = streamWriterFactory.createStreamWriter(
w.getOutputStream(), w.getMaxFrameLength(), ctx);
return messagingSessionFactory.createOutgoingSession(
return messagingSessionFactory.createSimplexOutgoingSession(
ctx.getContactId(), ctx.getTransportId(), w.getMaxLatency(),
duplex, streamWriter.getOutputStream());
streamWriter.getOutputStream());
} finally {
ByteUtils.erase(ctx.getSecret());
}
}
private MessagingSession createDuplexOutgoingSession(StreamContext ctx,
TransportConnectionWriter w) throws IOException {
try {
StreamWriter streamWriter = streamWriterFactory.createStreamWriter(
w.getOutputStream(), w.getMaxFrameLength(), ctx);
return messagingSessionFactory.createDuplexOutgoingSession(
ctx.getContactId(), ctx.getTransportId(), w.getMaxLatency(),
w.getMaxIdleTime(), streamWriter.getOutputStream());
} finally {
ByteUtils.erase(ctx.getSecret());
}
@@ -199,7 +212,7 @@ class ConnectionManagerImpl implements ConnectionManager {
connectionRegistry.registerConnection(contactId, transportId);
try {
// Create and run the outgoing session
createOutgoingSession(ctx, writer, false).run();
createSimplexOutgoingSession(ctx, writer).run();
disposeWriter(false);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
@@ -287,7 +300,7 @@ class ConnectionManagerImpl implements ConnectionManager {
}
try {
// Create and run the outgoing session
outgoingSession = createOutgoingSession(ctx, writer, true);
outgoingSession = createDuplexOutgoingSession(ctx, writer);
outgoingSession.run();
disposeWriter(false);
} catch(IOException e) {
@@ -353,7 +366,7 @@ class ConnectionManagerImpl implements ConnectionManager {
});
try {
// Create and run the outgoing session
outgoingSession = createOutgoingSession(ctx, writer, true);
outgoingSession = createDuplexOutgoingSession(ctx, writer);
outgoingSession.run();
disposeWriter(false);
} catch(IOException e) {

View File

@@ -56,6 +56,10 @@ public abstract class FilePlugin implements SimplexPlugin {
return maxLatency;
}
public long getMaxIdleTime() {
return Long.MAX_VALUE; // We don't need keepalives
}
public boolean isRunning() {
return running;
}

View File

@@ -35,6 +35,10 @@ class FileTransportWriter implements TransportConnectionWriter {
return plugin.getMaxLatency();
}
public long getMaxIdleTime() {
return plugin.getMaxIdleTime();
}
public long getCapacity() {
return capacity;
}

View File

@@ -67,6 +67,10 @@ class TcpTransportConnection implements DuplexTransportConnection {
return plugin.getMaxLatency();
}
public long getMaxIdleTime() {
return plugin.getMaxIdleTime();
}
public long getCapacity() {
return Long.MAX_VALUE;
}