grindcode / elastic-loop

Intervals with variable timeout.

Home Page:https://www.npmjs.com/package/elastic-loop

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Elastic Loop

Build Status Dependency Status devDependency Status

Runs an interval with variable timeout. Useful for loops adapting to heavy load or other kind of situations.

Get Started

npm install elastic-loop

API

loop(function, stress, [options])

Runs function in loop, mutating timeout depending on stress. Returns instance containing end function.

  • function: Interval function. (Function)
  • stress: Function returning bool. (Function)
  • options: optional, but must be an Object if specified, containing zero or more of the following properties:
    • timeout: Default interval timeout in milliseconds. (Number; default: 1000)
    • modifier: Timeout multiplier on stress. (Float; default: 1.20)
    • min: Min stress multiplier. (Number|Float; default: 1)
    • max: Max stress multiplier. (Number|Float; default: 0:infinite)

Usage

const loop = require('elastic-loop')
const busy = require('node-busy')

const monitor = busy()

// @cycle: current cycle fingerprint
function run (cycle) {
  // → @cycle: { timeout: 1000, stress: 1 }
}

function stress () {
  // returns true if overloaded, false otherwise
  return monitor.blocked
}

const cycle = loop(run, stress)

// cycle.end()

License

See the License file.

About

Intervals with variable timeout.

https://www.npmjs.com/package/elastic-loop

License:MIT License


Languages

Language:JavaScript 100.0%