node-rate-limiter-flexible icon indicating copy to clipboard operation
node-rate-limiter-flexible copied to clipboard

PM2 cluster error "timeout: no answer from master in time" if the master is not already available when the client is created

Open ighunter opened this issue 1 year ago • 2 comments

Following the example at https://github.com/animir/node-rate-limiter-flexible/wiki/PM2-cluster, we have one single-instance pm2 service for the RateLimiterClusterMasterPM2, and then we have another clustered service that uses new RateLimiterCluster() to initialize the rate-limiting, then we call .consume() in the client later.

If the order and timing of the service startup is as follows:

  1. new RateLimiterClusterMasterPM2(pm2) happens in the master service,
  2. new RateLimiterCluster() happens in the client services,

then all is good, and it works as expected.

However, if the order and timing of the service startup is as follows:

  1. new RateLimiterCluster() happens in a client service,
  2. then new RateLimiterClusterMasterPM2(pm2) happens in the master service,

then later when we call .consume() in the client we get this error:

Error: RateLimiterCluster timeout: no answer from master in time
     at Timeout._onTimeout (node_modules/rate-limiter-flexible/lib/RateLimiterCluster.js:188:14)
     at listOnTimeout (node:internal/timers:569:17)
     at process.processTimers (node:internal/timers:512:7)

Setting an arbitrarily large timeoutMs in the options passed to new RateLimiterCluster() such that the master should definitely have started within that many ms appears to make no difference.

This sequence of events also results in the same exception:

  1. new RateLimiterClusterMasterPM2(pm2) happens in the master service,
  2. new RateLimiterCluster() happens in the client services,
  3. One or more .consume()s happen successfully,
  4. The master service restarts while the client services continue to run uninterrupted,

then any further .consume() calls in the clients fail with RateLimiterCluster timeout: no answer from master in time

Is there something we should be doing differently, or is this something that can be fixed?

Node version: 18.17.1 rate-limiter-flexible version: 2.4.1 OS: Ubuntu 22.04.3 LTS

ighunter avatar Dec 12 '23 08:12 ighunter

@ighunter Hi, thank you for the detailed description. This looks like a bug. Can you start the master process and only after it starts workers? Can you restart workers, when master restarts? I know this isn't the best solution, but you can try until we understand the problem.

animir avatar Dec 16 '23 12:12 animir

Thanks @animir - yes, we have workarounds in place for now :ok_hand:

ighunter avatar Dec 18 '23 07:12 ighunter