-
-
Notifications
You must be signed in to change notification settings - Fork 287
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
Add try catch to connect() #2674
base: production
Are you sure you want to change the base?
Conversation
WalkthroughThe recent update introduces a safeguard in the connection setup process within the ORM package. A Changes
Recent Review DetailsConfiguration used: CodeRabbit UI Files selected for processing (1)
Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
@ygpark80 thanks forvthe PR. Just rework your commit message please (follow the angular commit convention pls ;)) |
@@ -69,7 +69,9 @@ export function registerConnectionProvider({provide, name = "default"}: CreateCo | |||
} as RedisOptions); | |||
} | |||
|
|||
await connection.connect(); | |||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But adding this try catch will hide the connection error. I’m not favorable to do add that without loing the error.
Also I prefer to catch the error only if an option is enable. By default, the error must be thrown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion:
const error = await catchAsyncError(() => connection.connect());
if (error && !connectionFailSilently) {
throw error
}
export interface BaseIORedisConfiguration {
name?: TokenProvider;
cache?: boolean;
connectionFailSilently?: boolean; // add this option
}
- add documentation on ioredis.md page to describe this new option (and your usecase can help to understand why this option exists)
- add unit test to cover this new usecase :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of creating lots of options since ioredis already has tons. lol. If this is used in a serverless environment, not all resources may be available on startup. However, registerConnectionProvider uses lazyConnect: true and tries to connect to Redis during component initialization. But if it fails, the whole server won't start up, which I think is a bigger problem than losing the error.
I think there is another option: do not use lazyConnect: true and leave ioredis as is. ioredis will use the auto-reconnect feature if an incident like mine happens. You don't have to wait until the connection is ready because ioredis already handles (queues) commands internally.
I think Ts.ED should not let people have too many options, as developers should just go ahead and write logic. Adding an option, in my humble opinion, is unnecessary and also confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I understand your point, but lazyConnect is here to solve another issue, and it's normal to stop the bootstrap server when the redis connection isn't available, because this problem will cause other issuer later on the runtime.
We can use the lazyConnect option to address your issue and keep the existing behavior as is it.
const {name, cache, lazyConnect = true, ...redisOptions} = item;
//...
connection = new Redis.Cluster(nodes, {
...clusterOptions,
lazyConnect // replace all lazyConnect in the provider
});
//...
if (lazyConnect) {
await connection.connect();
}
I think this is the better option for you and me :D
See you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I'll do some work sometime this week.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me know when it's ok for you ;)
@@ -69,7 +69,9 @@ export function registerConnectionProvider({provide, name = "default"}: CreateCo | |||
} as RedisOptions); | |||
} | |||
|
|||
await connection.connect(); | |||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion:
const error = await catchAsyncError(() => connection.connect());
if (error && !connectionFailSilently) {
throw error
}
export interface BaseIORedisConfiguration {
name?: TokenProvider;
cache?: boolean;
connectionFailSilently?: boolean; // add this option
}
- add documentation on ioredis.md page to describe this new option (and your usecase can help to understand why this option exists)
- add unit test to cover this new usecase :)
Sorry about that. it has been updated. |
b76e7c8
to
9230a2a
Compare
e7aa499
to
000a6b4
Compare
de8df3b
to
474e2d8
Compare
Information
As I mentioned in this comment, an error can arise during the connect() function and render the component useless. We need to add a try-catch block. In my case, I was getting an ETIMEDOUT error.
Summary by CodeRabbit