nats-io / nats.java

Java client for NATS
Apache License 2.0
563 stars 153 forks source link

Improve socket close behavior #1155

Closed scottf closed 3 months ago

scottf commented 3 months ago

Force Reconnect improvement

Execute forceClose with an executor task inside forceReconnect. This prevents the force reconnect behavior from being blocked if close blocks.


SocketDataPortWithWriteTimeout tuning

In the SocketDataPortWithWriteTimeout implementation of the timeout watcher task:

  1. Call the forceReconnect directly instead of running it on an executor task thread. It's already running on an executor task thread because that's where the watcher runs.
  2. Removed the call to close the output stream of the socket. It's going to get closed anyway when the socket gets closed.

Socket SO LINGER

Added an Options.socketSoLinger(int socketSoLinger) for the user to set the underlying socket.setSoLinger(). This will be used in normal close operations. If a forceClose is requested, any option value supplied will be overridden with setSoLinger(true, 0)


Force Close

Added a forceClose() to the DataPort interface (with a default implementation that calls close(), to ensure interface backward compatibility)

  1. the base internal implementation, SocketDataPort implements forceClose()
  2. forceReconnect calls forceClose()
  3. handleCommunicationIssue calls forceClose