[redis] support `preConnect` option
Environment
Unstorage v1.10.2
Reproduction
- Create a Driver or Storage object without manually calling getRedisClient().
- 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
This is not a bug, it's expected behavior
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.
This would be useful for 2 reasons:
- To know that the connection information is not good directly at launch or that the Redis server is unreachable.
- Gain performance on the first call
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 🙏