Skip to content

IdleConnections

Thomas Schwotzer edited this page Sep 13, 2023 · 3 revisions

This discussion is less important fro applications running on hardware with less restrictions when it comes to the number of open connections. But even there, keeping a connection open for ever is not always a good idea.

An ASAP peer can hardy decide when new data are provided by its application or from a connected peer. But applications developers might be able to make an educated guess.

A chat application could close an idle connection after a view milliseconds. It takes at least seconds for human users to create a new message. This time could be used to contact another device and come back to the first one later.

StreamPair wraps In- and Outputstream and a view optional information into a single object. Here is an example:

InputStream is; OutputStream os; 
// put both streams together and label with other sides ID or address.
StreamPair streamPair = StreamPairImpl.getStreamPairWithEndpointAddress(is, os, "AliceID");

Now, a IdleStreamPairCloser can be produced to observe this connection:

int maxIdleInMillis = 100;
IdleStreamPairCloser idleCloser = IdleStreamPairCloser.getIdleStreamsCloser(streamPair, maxIdleInMillis);
idleCloser.start(); // optional

This new idleCloser would close both streams if - in this case - 100 milli seconds not byte was transmitted over neither stream. When does the international timer starts? Their are two options:

  • Immediately after calling start().
  • It starts after the first byte was transmitted if start() is not called.

Now, both streams are under oberservation. They can be used to call directly handleConnection on our peer. We strongly recommend to use our EncounterManager. It solves our two other problems.