Skip to content

Commit f34dba3

Browse files
Fix ERR_AVVIO_PLUGIN_TIMEOUT with 'ready' client (#116)
* Fix ERR_AVVIO_PLUGIN_TIMEOUT with 'ready' client Adds two testcases with an existing custom client which is already connected to redis and will not emit 'ready' events anymore. One with 'ioredis' and one with 'node-redis' as custom modules. * Add comments to checking the connection-ready state
1 parent cc97a8e commit f34dba3

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,19 @@ function fastifyRedis (fastify, options, next) {
9494
}
9595
}
9696

97-
client
98-
.on('end', onEnd)
99-
.on('error', onError)
100-
.on('ready', onReady)
97+
// node-redis provides the connection-ready state in a .ready property,
98+
// whereas ioredis provides it in a .status property
99+
if (client.ready === true || client.status === 'ready') {
100+
// client is already connected, do not register event handlers
101+
// call next() directly to avoid ERR_AVVIO_PLUGIN_TIMEOUT
102+
next()
103+
} else {
104+
// ready event can still be emitted
105+
client
106+
.on('end', onEnd)
107+
.on('error', onError)
108+
.on('ready', onReady)
109+
}
101110

102111
return
103112
}

test/test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,87 @@ test('custom client', (t) => {
215215
})
216216
})
217217

218+
test('custom ioredis client that is already connected', (t) => {
219+
t.plan(10)
220+
const fastify = Fastify()
221+
const Redis = require('ioredis')
222+
const redis = new Redis({ host: 'localhost', port: 6379 })
223+
224+
// use the client now, so that it is connected and ready
225+
redis.set('key', 'value', (err) => {
226+
t.error(err)
227+
redis.get('key', (err, val) => {
228+
t.error(err)
229+
t.equal(val, 'value')
230+
231+
fastify.register(fastifyRedis, {
232+
client: redis,
233+
lazyConnect: false
234+
})
235+
236+
fastify.ready((err) => {
237+
t.error(err)
238+
t.equal(fastify.redis, redis)
239+
240+
fastify.redis.set('key2', 'value2', (err) => {
241+
t.error(err)
242+
fastify.redis.get('key2', (err, val) => {
243+
t.error(err)
244+
t.equal(val, 'value2')
245+
246+
fastify.close(function (err) {
247+
t.error(err)
248+
fastify.redis.quit(function (err) {
249+
t.error(err)
250+
})
251+
})
252+
})
253+
})
254+
})
255+
})
256+
})
257+
})
258+
259+
test('custom redis client that is already connected', (t) => {
260+
t.plan(10)
261+
const fastify = Fastify()
262+
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
263+
264+
// use the client now, so that it is connected and ready
265+
redis.set('key', 'value', (err) => {
266+
t.error(err)
267+
redis.get('key', (err, val) => {
268+
t.error(err)
269+
t.equal(val, 'value')
270+
271+
fastify.register(fastifyRedis, {
272+
client: redis,
273+
lazyConnect: false
274+
})
275+
276+
fastify.ready((err) => {
277+
t.error(err)
278+
t.equal(fastify.redis, redis)
279+
280+
fastify.redis.set('key2', 'value2', (err) => {
281+
t.error(err)
282+
fastify.redis.get('key2', (err, val) => {
283+
t.error(err)
284+
t.equal(val, 'value2')
285+
286+
fastify.close(function (err) {
287+
t.error(err)
288+
fastify.redis.quit(function (err) {
289+
t.error(err)
290+
})
291+
})
292+
})
293+
})
294+
})
295+
})
296+
})
297+
})
298+
218299
test('custom client gets closed', (t) => {
219300
t.plan(7)
220301
const fastify = Fastify()

0 commit comments

Comments
 (0)