pgjdbc-ng icon indicating copy to clipboard operation
pgjdbc-ng copied to clipboard

Support TCP keepalive and user timeout settings

Open jaroslawr opened this issue 3 years ago • 1 comments

Postgres C client libpq supports several options for setting up TCP keepalive:

keepalives_idle keepalives_interval keepalives_count tcp_user_timeout

https://www.postgresql.org/docs/13/libpq-connect.html

Postgres traditional pgjdbc Java client cannot support equivalent options, because it uses traditional Java sockets which do not support the relevant OS socket options. Thus pgjdbc only has a binary TCP keepalive on/off switch, but TCP keepalive without tcp_user_timeout is not reliable, as explained by:

https://blog.cloudflare.com/when-tcp-sockets-refuse-to-die/ https://codearcana.com/posts/2015/08/28/tcp-keepalive-is-a-lie.html

Netty on the other hand has all the relevant support in EpollSocketChannelConfig methods: setTcpKeepIdle setTcpKeepIntvl setTcpKeepCnt setTcpUserTimeout

pgjdbc-ng could expose configuration options similar to those offered by libpq, that would in effect change the EpollSocketChannelConfig for PG connections (and then it needs to handle the socket timeout correctly, maybe it already does?).

jaroslawr avatar Feb 11 '22 18:02 jaroslawr

(This functionality makes client-side load balancing between PG connections possible with reliable failover and no global query timeout)

jaroslawr avatar Feb 11 '22 18:02 jaroslawr