Make scrypt.maxPending a config option.

This also renames some internal uses of "max_pending" to match the
prevailing capitalization style.
This commit is contained in:
Ryan Kelly 2014-09-29 14:21:38 +10:00
Родитель d826fce497
Коммит 6ead098acc
3 изменённых файлов: 22 добавлений и 14 удалений

Просмотреть файл

@ -146,6 +146,13 @@ module.exports = function (fs, path, url, convict) {
env: 'TOOBUSY_MAX_LAG'
}
},
scrypt: {
maxPending: {
doc: "Max number of scrypt hash operations that can be pending",
default: 0,
env: 'SCRYPT_MAX_PENDING'
}
},
i18n: {
defaultLanguage: {
format: String,

Просмотреть файл

@ -6,8 +6,8 @@ var P = require('../promise')
var scrypt_hash = require('scrypt-hash')
// The maximum numer of hash operations allowed concurrently.
// This can be customized by setting the `max_pending` attribute
// on the exported object.
// This can be customized by setting the `maxPending` attribute on the
// exported object, or by setting the `scrypt.maxPending` config option.
const DEFAULT_MAX_PENDING = 100
module.exports = function(log, config) {
@ -15,9 +15,12 @@ module.exports = function(log, config) {
var scrypt = {
hash: hash,
// The current number of hash operations in progress.
num_pending: 0,
// The maximum number of hash operations that may be in progress.
max_pending: DEFAULT_MAX_PENDING
numPending: 0,
// The maximum number of hash operations that may be in progress.
maxPending: DEFAULT_MAX_PENDING
}
if (config.scrypt && config.scrypt.hasOwnProperty("maxPending")) {
scrypt.maxPending = config.scrypt.maxPending
}
/** hash - Creates an scrypt hash asynchronously
@ -28,13 +31,13 @@ module.exports = function(log, config) {
*/
function hash(input, salt, N, r, p, len) {
var d = P.defer()
if (scrypt.max_pending > 0 && scrypt.num_pending > scrypt.max_pending) {
if (scrypt.maxPending > 0 && scrypt.numPending > scrypt.maxPending) {
d.reject(new Error('too many pending scrypt hashes'))
} else {
scrypt.num_pending += 1
scrypt.numPending += 1
scrypt_hash(input, salt, N, r, p, len,
function (err, hash) {
scrypt.num_pending -= 1
scrypt.numPending -= 1
return err ? d.reject(err) : d.resolve(hash.toString('hex'))
}
)

Просмотреть файл

@ -5,7 +5,7 @@
var test = require('../ptaptest')
var promise = require('../../promise')
var log = {}
var config = {}
var config = { scrypt: { maxPending: 5 } }
var scrypt = require('../../crypto/scrypt')(log, config)
@ -29,16 +29,14 @@ test(
function (t) {
var K1 = Buffer('f84913e3d8e6d624689d0a3e9678ac8dcc79d2c2f3d9641488cd9d6ef6cd83dd', 'hex')
var salt = Buffer('identity.mozilla.com/picl/v1/scrypt')
// Set a lower max_pending so the test runs quicker.
var orig_max_pending = scrypt.max_pending;
scrypt.max_pending = 5;
// Send many concurent requests.
// Check the we're using the lower maxPending setting from config.
t.equal(scrypt.maxPending, 5, 'maxPending is correctly set from config')
// Send many concurrent requests.
// Not yielding the event loop ensures they will pile up quickly.
var promises = [];
for (var i = 0; i < 10; i++) {
promises.push(scrypt.hash(K1, salt, 65536, 8, 1, 32))
}
scrypt.max_pending = orig_max_pending;
return promise.all(promises).then(
function () {
t.fail('too many pending scrypt hashes were allowed')