Git Product home page Git Product logo

queue's Introduction

NGN Queue

A plugin for NGN

The NGN Queue is a collection of functions, stored in the order they're added t othe collection. "Running" a queue executes these functions, in parallel or sequentially.

Documentation for this plugin does not exist yet, but the inline comments in the code are thorough. The unit tests provide use cases, and a series of live examples are available on codepen.

The fundamental/basic example:

import NGN from 'https://cdn.jsdelivr.net/npm/ngn@latest/index.js'
import Queue from 'https://cdn.jsdelivr.net/npm/@ngnjs/queue/index.js'

const tasks = new Queue()

tasks.add(function () {
  console.log('Run task 1')
})

tasks.add(next => {
  setTimeout(() => {
    console.log('Run async task.')
    next()
  }, 300)
})

tasks.add('t3', function () {
  console.log('Run task named ' + this.name)
})

tasks.on('end', function () {
  console.log('All Done!')
})

tasks.runSync()

queue's People

Contributors

coreybutler avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

hixio-mh

queue's Issues

Queue Throttling

Is your feature request related to a problem? Please describe.
Sometimes it is important to cap the number of operations executed within a period of time.

One use case is API operations. APIs often have rate limiting, which restricts how many API requests are allowed in a specific period of time. For example, an API may only allow 100 requests per minute. If 1000 requests need to be made, logic indicates it should take 10 minutes to run all the requests (100 per minute).

Describe the solution you'd like
I would like to configure throttling. The queue automatically batch tasks to fit within their time-restricted window or maximum
concurrent operations window. For example:

Rate Limiting

const tasks = new Queue()

tasks.ratelimit(100, 60 * 1000) // 100 requests, 60 seconds/60K milliseconds

for (i=0; i < 1000; i++) {
  tasks.add(() => ... do something...)
}

let plan = tasks.plan()
console.log(`Expected to take ${plan.executionDuration} milliseconds`)

tasks.on('complete', () => console.log('Done'))
tasks.run()

Sometimes limits aren't time sensitive, but only X number of operations can be performed at the same time (i.e. maximum simultaneous operations). This may look like:

Maximum Concurrency

const tasks = new Queue()

tasks.limit(10) // 10 operations at a time
...

The code above would only allow 10 simultaneous operations to work at the same time.

Describe alternatives you've considered
There are no alternatives in NGN. It must all be done by hand or with another library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.