Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clientClose to work #177

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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