-
Notifications
You must be signed in to change notification settings - Fork 129
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
base: master
Are you sure you want to change the base?
Conversation
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.
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
lib/connectors/mysql-connector.ts
Outdated
@@ -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/[email protected]/src/constant/errors.ts"; |
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.
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
lib/connectors/mysql-connector.ts
Outdated
@@ -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 |
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.
Just wondering why it's 4 hours by default? Do we need a default even?
lib/connectors/mysql-connector.ts
Outdated
/** | ||
* 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 |
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 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:
- Let's keep
query
clean with justqueryDescription
andclient?
- Add
reconnectOnTimeout?: boolean
toMySQLOptions
- Use
this._options.reconnectOnTimeout
instead ofreconnectAttempt
in this method
lib/connectors/mysql-connector.ts
Outdated
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); | ||
} |
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.
- This needs formatting (you can try
deno fmt
or install an IDE deno plugin that does it by default on file save) - 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;
}
- No need for comments, the code is very readable (you did a good job already!)
lib/connectors/mysql-connector.ts
Outdated
/** | ||
* Reconnect client by close existing and reconnecting it | ||
*/ |
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 think you can make it even simpler:
/** Reconnect current client connection. */
lib/connectors/mysql-connector.ts
Outdated
* Reconnect client by close existing and reconnecting it | ||
*/ | ||
async reconnect(){ | ||
console.log('Debug >> Reconnect MySQL Client'); |
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 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
I will edit my pull request when I got free time, ahah I made a lot of small mistake. |
I'm so busy this moment sorry for the delay, this night I push new changes on PR. |
Sorry for the delay, it's pushed! |
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.
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
a03f051
to
be20558
Compare
Done :) |
Related to #257