coopernurse / node-pool

Generic resource pooling for node.js
2.38k stars 259 forks source link

Add support for backoff rate for failed allocations. #140

Open gkorland opened 8 years ago

gkorland commented 8 years ago

The current allocation mechanism doesn't use any backoff mechanism which causes the some of the DBs to "punish" the requester.

See: simple-backoff

sandfox commented 7 years ago

Whilst there is no mechaminsm for this in the pool itself, it's reasonably easy to add to the the user supplied factory function.

taken from another issue...

'use strict'

const genericPool = require('generic-pool')
const retry = require('retry-as-promised')

let createAttempts = 0

const retryLog = function(msg){
  console.log(msg)
}

const create = function () {
  createAttempts++
  if(createAttempts < 5){
    return Promise.reject(new Error(`failing on attempt ${createAttempts}`))
  }
  return Promise.resolve({name:'thingy'})
}

const factory = {
  create: function () {
     return retry(create, {backoffBase: 1000, max: 10,name: 'factory.create', report:retryLog})
  },
  destroy: function () { return Promise.resolve() }
}

const pool = genericPool.createPool(factory)

pool.on("factoryCreateError", e => {
  console.log("factoryCreateError", e);
});

pool.acquire().then((resource)=>{
  console.log('got a resource', resource)
}).catch((err)=>{
  console.log('could not get a resource')
})

For the moment this is probably the best place to add it till I come up with a good plan.

t3hmrman commented 5 years ago

Hey @sandfox , Is this still a feature you're considering putting in this library, or would you rather prefer leaving it as a common pattern/cookbook thing?