Skip to content

Commit 7cd58b0

Browse files
authored
Redis: prevent offline queuing and retries for commands (#18561)
* Add an error handler to ensure the Redis server connection is forcibly closed * Disable the Redis offline queue and retries
1 parent 78799b0 commit 7cd58b0

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

lib/redis/create-client.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,30 @@ module.exports = function createClient (options = {}) {
4242
}
4343
},
4444

45+
// Any running command that is unfulfilled when a connection is lost should
46+
// NOT be retried after the connection has been reestablished.
47+
retry_unfulfilled_commands: false,
48+
49+
// If we failed to send a new command during a disconnection, do NOT
50+
// enqueue it to send later after the connection has been [re-]established.
51+
// This is also critical to preventing a backend pile-up!
52+
enable_offline_queue: false,
53+
4554
// Expand whatever other options and overrides were provided
4655
...options
4756
})
4857

58+
// Handle connection errors to prevent killing the Node.js process
59+
client.on('error', (connectError) => {
60+
try {
61+
// Forcibly close the connection to the Redis server.
62+
// Allow all still running commands to silently fail immediately.
63+
client.end(false)
64+
} catch (disconnectError) {
65+
// Swallow any failure
66+
}
67+
})
68+
4969
// If a `name` was provided, use it in the prefix for logging event messages
5070
const logPrefix = '[redis' + (name ? ` (${name})` : '') + ']'
5171

0 commit comments

Comments
 (0)