Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Ioredis: al volver a conectar redis a través de connect() , llamar a connect no restablece los tiempos de reintento a 0

Tengo las siguientes opciones configuradas cuando me conecto a Redis

 var client = new Redis({ port: 63xx, // Redis port host: REDISHOST, // Redis host family: 4, // 4 (IPv4) or 6 (IPv6) db: 0, lazyConnect: true, // The milliseconds before a timeout occurs during the initial connection to the Redis server. connectTimeout: 3000, retryStrategy: function (times) { if (times > 3) { logger.error("redisRetryError", 'Redis reconnect exhausted after 3 retries.'); return null; } return 200; } });

Más adelante, estoy exportando este cliente a lo largo del proyecto para consultas redis. El problema es cuando llega la Solicitud 1 y hay algún problema con redis, intenta conectarse automáticamente 4 veces (+1 para el intento inicial). Luego arroja un error que se maneja. Entonces, por ahora, la variable times (usada en retrystrategy()) tendrá 4 como valor.

La próxima vez que llegue la Solicitud 2 y veamos que Redis está desconectado, nos volveremos a conectar usando el método client.connect() :

 static async getData(key) { try { // if connection is ended then we are trying to reconnect it. if (client.status === 'end') { await logger.warning(`reconnectingRedis`, 'Redis is not connected. Trying to reconnect to Redis!'); await client.connect(); } let output = await client.get(key); return JSON.parse(output); } catch (error) { ApiError.throw(error, errorCode.REDIS_GET_ERROR_CODE); } }

esta vez, redis intenta volver a conectarse, pero no restablece la variable times utilizada en retrystrategy(), por lo que esta variable ahora tiene 5. Y si este intento también falla, retrystrategy() simplemente arroja un error como times > 3

Entonces, efectivamente, la Solicitud 1 obtiene 4 intentos y la Solicitud 2 obtiene solo 1

¿Cómo puedo arreglar esto, para que la Solicitud 2 también tenga 4 intentos?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Para solucionar este problema, cambié la función retryStrategy utilizada al crear redis de la siguiente manera:

 retryStrategy: function (times) { if (times % 4 ==0) { logger.error("redisRetryError", 'Redis reconnect exhausted after 3 retries.'); return null; } return 200; }

Tenga en cuenta que tomé mod 4 de la variable times y al hacerlo siempre obtendremos un valor en el rango de 0-3.

Entonces, para la solicitud 2 cuando la variable times tiene 5, su mod 4 dará 1 y se intentará, la próxima vez los tiempos serán 6, por lo que el modo 4 es 2 y se probará y así sucesivamente. Hasta que se convierte en 8, en este caso, el mod 4 dará 0 y el reintento se detiene.

Esto solucionó el problema para mí.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!