<commit_before>
const debug                = require('debug')('kuss:pool')
const R                    = require('ramda')
const Bluebird             = require('bluebird')
const GenericPool          = require('generic-pool')
const { FibonacciBackoff } = require('simple-backoff')

const CONFIG_PROP_SERVER    = 'server'
const CONFIG_PROP_VALIDATOR = 'validate'
const CONFIG_PROP_TEST      = 'testOnBorrow'

const EVENT_CREATE_ERROR    = 'factoryCreateError'

const DEFAULT_CONFIG = {
  max               : 10
, min               : 2
, idleTimeoutMillis : 30000
, log               : false
, testOnBorrow      : false
, logger            : console
}


function _addBackoff(factory) {
  const backoff              = new FibonacciBackoff()
  const originalCreateMethod = factory.create

  factory.create = function __run() {

    return originalCreateMethod().catch(function ___handleErrorInCreate(e) {

      const wait = backoff.next()

      debug(
        '::_addBackoff - could not create resource - waiting %d ms - %s'
      , wait
      , e.message
      )

      return Bluebird.delay(wait).throw(e)

    })

  }

  return factory<commit_msg>make sure to reset the backoff whenever a successful allocation occurs.<commit_after>
const debug                = require('debug')('kuss:pool')
const R                    = require('ramda')
const Bluebird             = require('bluebird')
const GenericPool          = require('generic-pool')
const { FibonacciBackoff } = require('simple-backoff')

const CONFIG_PROP_SERVER    = 'server'
const CONFIG_PROP_VALIDATOR = 'validate'
const CONFIG_PROP_TEST      = 'testOnBorrow'

const EVENT_CREATE_ERROR    = 'factoryCreateError'

const DEFAULT_CONFIG = {
  max               : 10
, min               : 2
, idleTimeoutMillis : 30000
, log               : false
, testOnBorrow      : false
, logger            : console
}


function _addBackoff(factory) {
  const backoff              = new FibonacciBackoff()
  const originalCreateMethod = factory.create

  factory.create = function __run() {

    return originalCreateMethod().catch(function ___handleErrorInCreate(e) {

      const wait = backoff.next()

      debug(
        '::_addBackoff - could not create resource - waiting %d ms - %s'
      , wait
      , e.message
      )

      return Bluebird.delay(wait).throw(e)

    })

    .then((resource) => {

      backoff.reset()

      return resource

    })

  }

  return factory
{'fraction_non_alphanumeric': 0.09592424866200082, 'fraction_numerical': 0.006587072869493619, 'mean_word_length': 2.3425034387895463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6287996', 'n_tokens_mistral': 711, 'n_tokens_neox': 670, 'n_words': 165}