Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your solution is quite OK for many cases when the number of connected clients is small compared to the performance of the enumerating the collection of their contexts. If you allow some wiggle room in the timeout (i.e. it's OK to disconnect the client somewhere between 55 and 65 seconds of inactivity), I'd just run the purging routine every so often.</p> <p>The other approach which worked great for me was using a queue of activity tokens. Lets use C#:</p> <pre><code>class token { ClientContext theClient; // this is the client we've observed activity on DateTime theTime; // this is the time of observed activity }; class ClientContext { // ... what you need to know about the client DateTime lastActivity; // the time the last activity happened on this client } </code></pre> <p>Every time an activity happens on a particular client, a token is generated and pushed into FIFO queue and <code>lastActivity</code> is updated in the <code>ClientContext</code>.</p> <p>There is another thread which runs the following loop:</p> <ul> <li>extracts the oldest token from the FIFO queue;</li> <li>checks if <code>theTime</code> in this token matches the <code>theClient.lastActivity</code>;</li> <li>if it does, shuts down the client;</li> <li>looks at the next oldest token in the queue, calculates how much time is left till it needs to be shutdown;</li> <li><code>Thread.Sleep(&lt;this time&gt;)</code>;</li> <li>repeat</li> </ul> <p>The price of this approach is a little, but constant, i.e. O(1) overhead. One can come up with faster best case solutions, but it seems to me that it's hard to come up with a one with faster worst case performance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload