MortezaBashsiz / nipovpn

Powerfull http proxy
GNU General Public License v3.0
183 stars 11 forks source link

Timeout implementation for connections. #90

Closed Ali-Nasrolahi closed 2 months ago

Ali-Nasrolahi commented 2 months ago

Objective

Automatically closing a connection, which becomes stalled for some reason. Fixes MortezaBashsiz/nipovpn#79

Implementation Details

Before every read/write operation, a timer (deadline_timer) gets started/reseted, and after each successful I/O; the timer gets cancelled. When timer's handlers called ,in case of an expiration, it closes associated socket with the connection; by doing so, any blocking I/O on respected socket will be interrupted.

This patch only addresses TCPConnection and TCPClient classes' read and write methods. The underlying implementation is identical for both classes.

Test

For testing, I did following scenarios while timeout is specified:

My tests are not addressing every block of read and write code, therefore this patch should be tested further to ensure stability.

About the diffs

Timeout configuration is identical to timeWait and uses similar value range.
I have implemented three main method to handle the timer: resetTimeout, cancelTimeout and onTimeout. And few other minor changes.

Please, let me know of any issue/suggestion :)

Ali-Nasrolahi commented 2 months ago

Sure!

I have modified the patch based on your requested changes.

Also, I believe the performance decrement is due to the frequency of start/reset-ing and cancel-ing the timeout's handler during each read/write operation. Which as you have mentioned, needs to be improved.