The current implementation of the natsNewClient function in codebase has a few areas that need improvement for better connection management and logging. The following issues have been identified:
Redundant Connection Attempts:
The current implementation attempts to connect to the NATS server in a loop, retrying up to 10 times with a 1-second delay. This can lead to unnecessary retries and increased latency in establishing a connection.
Proposed Solution
Simplifying Connection Logic:
Removing the loop for connection attempts and replacing it with a single connection attempt using NATS connection parameters such as ReconnectWait and MaxReconnects.
Improving Error Handling:
Implementing NATS connection event handlers (DisconnectErrHandler, ReconnectHandler, and ClosedHandler) to provide detailed logs for connection lifecycle events.
Description
The current implementation of the nats
NewClient
function in codebase has a few areas that need improvement for better connection management and logging. The following issues have been identified:Redundant Connection Attempts:
Proposed Solution
Simplifying Connection Logic:
ReconnectWait
andMaxReconnects
.Improving Error Handling:
DisconnectErrHandler
,ReconnectHandler
, andClosedHandler
) to provide detailed logs for connection lifecycle events.https://github.com/forta-network/forta-node/pull/862