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

fix: Auto reconnection to the database server is not triggered #269

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

bleugateau
Copy link

Related to #257

Copy link
Owner

@eveningkid eveningkid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright so logic-wise it's all good!

All my comments are about keeping the codebase consistent/coherent (you couldn't have guessed it!).

It'll be ready to merge after these changes :)

Thanks again for helping and take your time, there's no rush

@@ -4,6 +4,7 @@ import type { Connector, ConnectorOptions } from "./connector.ts";
import { SQLTranslator } from "../translators/sql-translator.ts";
import type { SupportedSQLDatabaseDialect } from "../translators/sql-translator.ts";
import type { QueryDescription } from "../query-builder.ts";
import { ResponseTimeoutError } from "https://deno.land/x/mysql@v2.8.0/src/constant/errors.ts";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be added to /deps.ts and then imported from there.

It's just to keep external dependencies cleaner, all in one place :)

MysqlClientResponseTimeoutError would be a good alias based on other imports from that driver

@@ -46,6 +48,7 @@ export class MySQLConnector implements Connector {
password: this._options.password,
port: this._options.port ?? 3306,
charset: this._options.charset ?? "utf8",
idleTimeout: this._options.idleTimeout ?? 4 * 3600 * 1000
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering why it's 4 hours by default? Do we need a default even?

Comment on lines 68 to 77
/**
* Executing query
* @param queryDescription {QueryDescription}
* @param client {MySQLClient | MySQLConnection}
* @param reconnectAttempt {boolean} - Used for reconnect client when ResponseTimeoutError happened
*/
async query(
queryDescription: QueryDescription,
client?: MySQLClient | MySQLConnection,
reconnectAttempt: boolean = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the intent but this connector follows an interface (Connector) and can't be tweaked for just this one.

Honestly, we could send this to the connector options instead but I don't see why someone wouldn't want it to reconnect and get an error instead you know what I mean?

So:

  1. Let's keep query clean with just queryDescription and client?
  2. Add reconnectOnTimeout?: boolean to MySQLOptions
  3. Use this._options.reconnectOnTimeout instead of reconnectAttempt in this method

Comment on lines 89 to 104
let result = null;

try{
result = await queryClient[queryMethod](subqueries[i]);
}
catch(e){
//prevent unhandled behavior
if(!(e instanceof ResponseTimeoutError) || !reconnectAttempt){
return result;
}

//reconnect client, at this moment we can't subscribe to connectionState of mysql driver, we need to do this
await this.reconnect();

return await this.query(queryDescription, client, false);
}
Copy link
Owner

@eveningkid eveningkid Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This needs formatting (you can try deno fmt or install an IDE deno plugin that does it by default on file save)
  2. I think it is more readable to go with the edge case in the if statement:
try {
  const result = await queryClient[queryMethod](subqueries[i]);
  
  if (i === subqueries.length - 1) {
    return result;
  }
} catch (error) {
  if (this._options.reconnectOnTimeout && error instanceof ResponseTimeoutError) {
    await this.reconnect();
    // No need for await below since there's no other statement after this return.
    // Also not passing `false` is fine, it can try many reconnections not just one
    return this.query(queryDescription, client);
  }
  
  // Other errors should still be raised
  throw error;
}
  1. No need for comments, the code is very readable (you did a good job already!)

Comment on lines 116 to 118
/**
* Reconnect client by close existing and reconnecting it
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can make it even simpler:

/** Reconnect current client connection. */

* Reconnect client by close existing and reconnecting it
*/
async reconnect(){
console.log('Debug >> Reconnect MySQL Client');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it isn't written anywhere but there's actually a helper for that :D
https://github.com/eveningkid/denodb/blob/master/lib/helpers/log.ts#L11-L13

Maybe we could use a warning for that and just say "Reconnecting MySQL client" yes

@bleugateau
Copy link
Author

bleugateau commented Jun 4, 2021

I will edit my pull request when I got free time, ahah I made a lot of small mistake.
My bad, I should have rechecked my fork before pushing it !

@bleugateau
Copy link
Author

I'm so busy this moment sorry for the delay, this night I push new changes on PR.

@bleugateau
Copy link
Author

bleugateau commented Jun 16, 2021

Sorry for the delay, it's pushed!

Copy link
Owner

@eveningkid eveningkid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes, I've just added comments.

I feel like maybe you missed a few of my previous comments so be sure to check them before we can move on.

Thank you

lib/connectors/mysql-connector.ts Outdated Show resolved Hide resolved
lib/connectors/mysql-connector.ts Outdated Show resolved Hide resolved
lib/connectors/mysql-connector.ts Outdated Show resolved Hide resolved
lib/connectors/mysql-connector.ts Outdated Show resolved Hide resolved
lib/connectors/mysql-connector.ts Outdated Show resolved Hide resolved
lib/connectors/mysql-connector.ts Outdated Show resolved Hide resolved
@bleugateau bleugateau force-pushed the master branch 2 times, most recently from a03f051 to be20558 Compare June 19, 2021 10:00
@bleugateau
Copy link
Author

bleugateau commented Jun 23, 2021

Done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants