![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Node.js – retry_strategy Property in Redis
The retry_strategy is a function that receives objects as parameters including the retry attempt,total_retry_time that indicates the time passed after it was connected the last time, the error due to which the connection was lost, and the number of times_connected in total.
If a number is returned from this function, the next retry will take place after that time only in milliseconds and if you send a non-number, no further retry will take place.
Syntax
retry_strategy: funciton(options)
Example 1
Create a file with the name "retryStrategy.js" and copy the following code. After creating the file, use the command "node retryStrategy.js" to run this code as shown in the example below
// retry_strategy Property Demo Example // Importing the redis module const redis = require("redis"); // Creating redis client with retry_strategy const client = redis.createClient({ retry_strategy: function(options) { if (options.error && options.error.code === "ECONNREFUSED") { // If redis refuses the connection or is not able to connect return new Error("The server refused the connection"); } if (options.total_retry_time > 1000 * 60 * 60) { // End reconnection after the specified time limit return new Error("Retry time exhausted"); } if (options.attempt > 10) { // End reconnecting with built in error return undefined; } // reconnect after return Math.min(options.attempt * 100, 3000); }, }); console.log("Connection created successfully !") client.set("key", "TutorialsPoint"); // This will return a JavaScript String client.get("key", function(err, reply) { console.log(reply); });
Output
Connection created successfully! TutorialsPoint
The following error is thrown in case it is not able to establish a connection with Redis.
Error: Redis connection in broken state: retry aborted. at RedisClient.connection_gone (/home/redis/node_modules/redis/index.js:569:30) at RedisClient.on_error (/home/redis/node_modules/redis/index.js:346:10) at Socket.<anonymous> (/home/redis/node_modules/redis/index.js:223:14) at Socket.emit (events.js:198:13) at emitErrorNT (internal/streams/destroy.js:91:8) at emitErrorAndCloseNT (internal/streams/destroy.js:59:3) at process._tickCallback (internal/process/next_tick.js:63:19)
Advertisements