impossibl / pgjdbc-ng

A new JDBC driver for PostgreSQL aimed at supporting the advanced features of JDBC and Postgres
https://impossibl.github.io/pgjdbc-ng
Other
596 stars 108 forks source link

Support TCP keepalive and user timeout settings #570

Open jaroslawr opened 2 years ago

jaroslawr commented 2 years ago

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 commented 2 years ago

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