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 failover handling #91

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ declare namespace serverlessMysql {
* Integer The minimum number of seconds that a connection must be idle before the module will recycle it. 3
*/
zombieMinTimeout?: number
/**
* Boolean Handle a failover with a DNS switchover gracefully. Defaults to false.
*/
handleFailover?: boolean
}

class Transaction {
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = (params) => {
// Init setting values
let MYSQL, manageConns, cap, base, maxRetries, connUtilization, backoff,
zombieMinTimeout, zombieMaxTimeout, maxConnsFreq, usedConnsFreq,
onConnect, onConnectError, onRetry, onClose, onError, onKill, onKillError, PromiseLibrary
onConnect, onConnectError, onRetry, onClose, onError, onKill, onKillError, PromiseLibrary, handleFailover

/********************************************************************/
/** HELPER/CONVENIENCE FUNCTIONS **/
Expand Down Expand Up @@ -187,7 +187,9 @@ module.exports = (params) => {
// If no args are passed in a transaction, ignore query
if (this && this.rollback && args.length === 0) { return resolve([]) }
client.query(...args, async (err, results) => {
if (err && err.code === 'PROTOCOL_SEQUENCE_TIMEOUT') {
if (err && (err.code === 'PROTOCOL_SEQUENCE_TIMEOUT'
|| (handleFailover && err.code === 'ER_OPTION_PREVENTS_STATEMENT'))
) {
client.destroy() // destroy connection on timeout
resetClient() // reset the client
reject(err) // reject the promise with the error
Expand Down Expand Up @@ -366,6 +368,7 @@ module.exports = (params) => {
zombieMaxTimeout = Number.isInteger(cfg.zombieMaxTimeout) ? cfg.zombieMaxTimeout : 60*15 // default to 15 minutes
maxConnsFreq = Number.isInteger(cfg.maxConnsFreq) ? cfg.maxConnsFreq : 15*1000 // default to 15 seconds
usedConnsFreq = Number.isInteger(cfg.usedConnsFreq) ? cfg.usedConnsFreq : 0 // default to 0 ms
handleFailover = cfg.handleFailover === true ? true : false // default to false

// Event handlers
onConnect = typeof cfg.onConnect === 'function' ? cfg.onConnect : () => {}
Expand Down