Skip to content

Commit

Permalink
fix: clientClose to work with custom client (#177)
Browse files Browse the repository at this point in the history
* fix: closeClient does not work

* Update README.md
  • Loading branch information
starnayuta authored Apr 10, 2023
1 parent a7bf45d commit 5f73081
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ fastify.register(require('@fastify/redis'), { client })
Note: by default, *@fastify/redis* will **not** automatically close the client
connection when the Fastify server shuts down.

To automatically close the client connection, set clientClose to true.

```js
fastify.register(require('@fastify/redis'), { client, closeClient: true })
```

## Registering multiple Redis client instances

By using the `namespace` option you can register multiple Redis client instances.
Expand Down
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fp = require('fastify-plugin')
const Redis = require('ioredis')

function fastifyRedis (fastify, options, next) {
const { namespace, url, ...redisOptions } = options
const { namespace, url, closeClient = false, ...redisOptions } = options

let client = options.client || null

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

if (!client) {
if (client) {
if (closeClient === true) {
fastify.addHook('onClose', closeNamedInstance)
}
} else {
try {
if (url) {
client = new Redis(url, redisOptions)
Expand All @@ -40,7 +44,11 @@ function fastifyRedis (fastify, options, next) {
if (fastify.redis) {
return next(new Error('@fastify/redis has already been registered'))
} else {
if (!client) {
if (client) {
if (closeClient === true) {
fastify.addHook('onClose', close)
}
} else {
try {
if (url) {
client = new Redis(url, redisOptions)
Expand Down
87 changes: 87 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,93 @@ test('custom ioredis client that is already connected', (t) => {
})
})

test('If closeClient is enabled, close the client.', (t) => {
t.plan(10)
const fastify = Fastify()
const Redis = require('ioredis')
const redis = new Redis({ host: 'localhost', port: 6379 })

redis.set('key', 'value', (err) => {
t.error(err)
redis.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')

fastify.register(fastifyRedis, {
client: redis,
closeClient: true
})

fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis, redis)

fastify.redis.set('key2', 'value2', (err) => {
t.error(err)
fastify.redis.get('key2', (err, val) => {
t.error(err)
t.equal(val, 'value2')

const originalQuit = fastify.redis.quit
fastify.redis.quit = (callback) => {
t.pass('redis client closed')
originalQuit.call(fastify.redis, callback)
}

fastify.close(function (err) {
t.error(err)
})
})
})
})
})
})
})

test('If closeClient is enabled, close the client namespace.', (t) => {
t.plan(10)
const fastify = Fastify()
const Redis = require('ioredis')
const redis = new Redis({ host: 'localhost', port: 6379 })

redis.set('key', 'value', (err) => {
t.error(err)
redis.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')

fastify.register(fastifyRedis, {
client: redis,
namespace: 'foo',
closeClient: true
})

fastify.ready((err) => {
t.error(err)
t.equal(fastify.redis.foo, redis)

fastify.redis.foo.set('key2', 'value2', (err) => {
t.error(err)
fastify.redis.foo.get('key2', (err, val) => {
t.error(err)
t.equal(val, 'value2')

const originalQuit = fastify.redis.foo.quit
fastify.redis.foo.quit = (callback) => {
t.pass('redis client closed')
originalQuit.call(fastify.redis.foo, callback)
}

fastify.close(function (err) {
t.error(err)
})
})
})
})
})
})
})

test('fastify.redis.test should throw with duplicate connection namespaces', (t) => {
t.plan(1)

Expand Down
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ declare namespace fastifyRedis {
}) | {
client: Redis | Cluster;
namespace?: string;
/**
* @default false
*/
closeClient?: boolean;
}
/*
Expand Down

0 comments on commit 5f73081

Please sign in to comment.