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

Add try catch to connect() #2674

Open
wants to merge 1 commit into
base: production
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion packages/orm/ioredis/src/utils/registerConnectionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export function registerConnectionProvider({provide, name = "default"}: CreateCo
} as RedisOptions);
}

await connection.connect();
try {
Copy link
Collaborator

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.

Copy link
Collaborator

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 :)

Copy link
Author

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.

Copy link
Collaborator

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

Copy link
Author

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.

Copy link
Collaborator

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 ;)

await connection.connect();
} catch (e) {}
ygpark80 marked this conversation as resolved.
Show resolved Hide resolved

logger.info("Connected to redis database...");

Expand Down
Loading