Skip to content

Instantly share code, notes, and snippets.

@sandfox
Forked from anonymous/a.js
Last active January 20, 2017 10:23
Show Gist options
  • Save sandfox/b68eb2d96f1a36945ab838d893a2c7d2 to your computer and use it in GitHub Desktop.
Save sandfox/b68eb2d96f1a36945ab838d893a2c7d2 to your computer and use it in GitHub Desktop.
const genericPool = require("generic-pool");
const factory = {
create: function() {
return new Promise(function(resolve, reject){
setTimeout(function(){
reject(new Error('cannot create resource'))
}, 1000)
})
},
destroy: function () { return Promise.resolve() }
}
const opts = {
acquireTimeoutMillis: 5000
}
const myPool = genericPool.createPool(factory, opts)
myPool.on('factoryCreateError', function(err){
console.log('factory create error, %s',err)
})
myPool.acquire()
.then(function() {
console.log('acquired a resource')
}).catch(function(err){
console.log('failed to acquire resource: %s', err)
})
.then(function(){
console.log('pool stats', {
min: myPool.min,
max: myPool.max,
size: myPool.size,
available: myPool.available,
})
})
setInterval(function(){
// just here to keep the event loop spinning
}, 5000)
factory create error, Error: cannot create resource
factory create error, Error: cannot create resource
factory create error, Error: cannot create resource
factory create error, Error: cannot create resource
failed to acquire resource: TimeoutError: ResourceRequest timed out
pool stats { min: 0, max: 1, size: 1, available: 0 }
factory create error, Error: cannot create resource
## END OF OUTPUT ##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment