Skip to content

Commit 5f73081

Browse files
authored
fix: clientClose to work with custom client (#177)
* fix: closeClient does not work * Update README.md
1 parent a7bf45d commit 5f73081

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ fastify.register(require('@fastify/redis'), { client })
100100
Note: by default, *@fastify/redis* will **not** automatically close the client
101101
connection when the Fastify server shuts down.
102102

103+
To automatically close the client connection, set clientClose to true.
104+
105+
```js
106+
fastify.register(require('@fastify/redis'), { client, closeClient: true })
107+
```
108+
103109
## Registering multiple Redis client instances
104110

105111
By using the `namespace` option you can register multiple Redis client instances.

index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fp = require('fastify-plugin')
44
const Redis = require('ioredis')
55

66
function fastifyRedis (fastify, options, next) {
7-
const { namespace, url, ...redisOptions } = options
7+
const { namespace, url, closeClient = false, ...redisOptions } = options
88

99
let client = options.client || null
1010

@@ -21,7 +21,11 @@ function fastifyRedis (fastify, options, next) {
2121
return fastify.redis[namespace].quit()
2222
}
2323

24-
if (!client) {
24+
if (client) {
25+
if (closeClient === true) {
26+
fastify.addHook('onClose', closeNamedInstance)
27+
}
28+
} else {
2529
try {
2630
if (url) {
2731
client = new Redis(url, redisOptions)
@@ -40,7 +44,11 @@ function fastifyRedis (fastify, options, next) {
4044
if (fastify.redis) {
4145
return next(new Error('@fastify/redis has already been registered'))
4246
} else {
43-
if (!client) {
47+
if (client) {
48+
if (closeClient === true) {
49+
fastify.addHook('onClose', close)
50+
}
51+
} else {
4452
try {
4553
if (url) {
4654
client = new Redis(url, redisOptions)

test/index.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,93 @@ test('custom ioredis client that is already connected', (t) => {
242242
})
243243
})
244244

245+
test('If closeClient is enabled, close the client.', (t) => {
246+
t.plan(10)
247+
const fastify = Fastify()
248+
const Redis = require('ioredis')
249+
const redis = new Redis({ host: 'localhost', port: 6379 })
250+
251+
redis.set('key', 'value', (err) => {
252+
t.error(err)
253+
redis.get('key', (err, val) => {
254+
t.error(err)
255+
t.equal(val, 'value')
256+
257+
fastify.register(fastifyRedis, {
258+
client: redis,
259+
closeClient: true
260+
})
261+
262+
fastify.ready((err) => {
263+
t.error(err)
264+
t.equal(fastify.redis, redis)
265+
266+
fastify.redis.set('key2', 'value2', (err) => {
267+
t.error(err)
268+
fastify.redis.get('key2', (err, val) => {
269+
t.error(err)
270+
t.equal(val, 'value2')
271+
272+
const originalQuit = fastify.redis.quit
273+
fastify.redis.quit = (callback) => {
274+
t.pass('redis client closed')
275+
originalQuit.call(fastify.redis, callback)
276+
}
277+
278+
fastify.close(function (err) {
279+
t.error(err)
280+
})
281+
})
282+
})
283+
})
284+
})
285+
})
286+
})
287+
288+
test('If closeClient is enabled, close the client namespace.', (t) => {
289+
t.plan(10)
290+
const fastify = Fastify()
291+
const Redis = require('ioredis')
292+
const redis = new Redis({ host: 'localhost', port: 6379 })
293+
294+
redis.set('key', 'value', (err) => {
295+
t.error(err)
296+
redis.get('key', (err, val) => {
297+
t.error(err)
298+
t.equal(val, 'value')
299+
300+
fastify.register(fastifyRedis, {
301+
client: redis,
302+
namespace: 'foo',
303+
closeClient: true
304+
})
305+
306+
fastify.ready((err) => {
307+
t.error(err)
308+
t.equal(fastify.redis.foo, redis)
309+
310+
fastify.redis.foo.set('key2', 'value2', (err) => {
311+
t.error(err)
312+
fastify.redis.foo.get('key2', (err, val) => {
313+
t.error(err)
314+
t.equal(val, 'value2')
315+
316+
const originalQuit = fastify.redis.foo.quit
317+
fastify.redis.foo.quit = (callback) => {
318+
t.pass('redis client closed')
319+
originalQuit.call(fastify.redis.foo, callback)
320+
}
321+
322+
fastify.close(function (err) {
323+
t.error(err)
324+
})
325+
})
326+
})
327+
})
328+
})
329+
})
330+
})
331+
245332
test('fastify.redis.test should throw with duplicate connection namespaces', (t) => {
246333
t.plan(1)
247334

types/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ declare namespace fastifyRedis {
2424
}) | {
2525
client: Redis | Cluster;
2626
namespace?: string;
27+
/**
28+
* @default false
29+
*/
2730
closeClient?: boolean;
2831
}
2932
/*

0 commit comments

Comments
 (0)