unstorage icon indicating copy to clipboard operation
unstorage copied to clipboard

[redis] support `preConnect` option

Open olivbau opened this issue 1 year ago • 4 comments

Environment

Unstorage v1.10.2

Reproduction

  1. Create a Driver or Storage object without manually calling getRedisClient().
  2. Attempt to call setItem or getItem.

Describe the bug

The Redis instance is initialized only when the getRedisClient() function is called. However, getRedisClient() is never invoked during the creation of the Driver or the Storage objects. As a result, the first call to getRedisClient() occurs only when setItem or getItem is called.

This causes an issue where the Redis instance is created and attempts to get or set an item, but the ioredis instance has not yet established a connection to the Redis server.

Expected Behavior

The Redis instance should be initialized and connected to the Redis server before any get or set operations are performed.

Actual Behavior

The Redis instance is created at the time of the first setItem or getItem call, and the ioredis instance is not yet connected.

Possible Solution

Ensure that the getRedisClient() function is called after beeing defined to establish a connection to the Redis server before any operations are performed. From what I can see, it seems to be same for Mongodb

Additional context

No response

Logs

No response

olivbau avatar Jul 30 '24 10:07 olivbau

This is not a bug, it's expected behavior image

GalacticHypernova avatar Dec 05 '24 11:12 GalacticHypernova

Indeed it is intended for perf, an im not sure what issue it is causing.

But we can introduce a preconnect: true driver option to allow this.

pi0 avatar Dec 05 '24 12:12 pi0

This would be useful for 2 reasons:

  1. To know that the connection information is not good directly at launch or that the Redis server is unreachable.
  2. Gain performance on the first call

olivbau avatar Dec 06 '24 00:12 olivbau

Alternative is that you do a getItem as healthcheack on server startup. (this, also allows you to directly catch errors instead of relying on global rejection)

PR is still welcome 🙏

pi0 avatar Dec 06 '24 09:12 pi0