-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from maxnachlinger/deadlockRetry
Added deadlock retry, bumped version
- Loading branch information
Showing
4 changed files
with
117 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var util = require('util'); | ||
|
||
module.exports = function (params, cb) { | ||
var fn = params.fn; | ||
var args = params.args; | ||
var retryAmount = params.retryAmount || 2; | ||
|
||
if (!fn || !cb) | ||
return cb(new Error('Expected params not passed, param fn and a callback are required.')); | ||
|
||
if (!args || !util.isArray(args)) | ||
args = []; | ||
|
||
var fx = function () { | ||
args.push(function retryCb(err, res) { | ||
if (!err) return cb(err, res); | ||
|
||
if (err.code && err.code === 'ER_LOCK_DEADLOCK' && --retryAmount > 0) { | ||
console.log(err.code + ' returned, retrying, (' + retryAmount + ') tries remaining'); | ||
return setTimeout(fx, 10); | ||
} | ||
|
||
return cb(err, res); | ||
}); | ||
fn.apply(fn, args) | ||
}; | ||
|
||
fx(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"use strict"; | ||
var _ = require('lodash'); | ||
var test = require('tape'); | ||
var deadlockRetry = require('../lib/deadlockRetry.js'); | ||
|
||
test("Retries the requested amount of time on deadlock errors", function (t) { | ||
var amtCalls = 0; | ||
var amtCallsExpected = 3; | ||
|
||
function f(p, cb) { | ||
amtCalls++; | ||
|
||
var err = new Error(); | ||
err.code = 'ER_LOCK_DEADLOCK'; | ||
|
||
return cb(err); | ||
} | ||
|
||
deadlockRetry({fn: f, args: [1], retryAmount: amtCallsExpected}, function (err, res) { | ||
t.equal(amtCalls, amtCallsExpected, amtCallsExpected + ' calls should have been made, received: ' + amtCalls); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test("Doesn't retry for non-errors", function (t) { | ||
var amtCalls = 0; | ||
var amtCallsExpected = 1; | ||
|
||
function f(p, cb) { | ||
amtCalls++; | ||
return cb(new Error()); | ||
} | ||
|
||
deadlockRetry({fn: f, args: [1], retryAmount: amtCallsExpected}, function (err, res) { | ||
t.equal(amtCalls, amtCallsExpected, amtCallsExpected + ' calls should have been made, received: ' + amtCalls); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test("Handles missing args", function (t) { | ||
var amtCalls = 0; | ||
var amtCallsExpected = 1; | ||
|
||
function f(cb) { | ||
amtCalls++; | ||
return cb(new Error()); | ||
} | ||
|
||
deadlockRetry({fn: f}, function (err, res) { | ||
t.equal(amtCalls, amtCallsExpected, amtCallsExpected + ' calls should have been made, received: ' + amtCalls); | ||
t.end(); | ||
}); | ||
}); |