0.0.6 • Published 3 years ago

@momsfriendlydevco/throttle v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@MomsFriendlyDevCo/throttle

Async throttling mechanism based on MongoDB locking.

var Throttle = require('@momsfriendlydevco/throttle');

var throttler = new Throttle();
throttler.init()
	.then(()=> throttler.throttle(() => {
			// Worker is complete when promise resolves
			return Promise.resolve();
		},
		{
			id: 'foo',
			hash: ({foo: 'bar'}),
		}
	))
	.catch(e => {
		// The queue full of workers already in progress.
	});

API

Throttle(settings)

Main constructor. Requires instance.init() to be called before the instance is functional.

Throttle.defaults

Default settings.

SettingTypeDefaultDescription
leadingbooleantrueBypasses use of the queue
queuenumber1Number of items for which to keep queued callbacks
lockobjectSee belowLock package configuration options
lock.expirynumber1 hourThe time in milliseconds until the lock expires
lock.ttlnumber1 minThe time in milliseconds until keep-alive expires
lock.mongodbobjectSee belowMongoDB connection options
lock.mongodb.uristring"mongodb://localhost/mfdc-cache"The MongoDB URI to connect to
lock.mongodb.collectionstring"locks"The name of the collection to use
lock.mongodb.optionsobjectSee codeAdditional connection options to use
lock.omitFieldsarray['_id', '__v']Which fields to autmatically skip when using get()
lock.includeKeysbooleantrueAlso save the key field values, reduces overhead to disable this

throttler.init(settings)

Check all settings and connect to the database.

throttler.throttle(worker, options)

Callback worker will be called immediately if a lock is available or otherwise throttled in a queue. When queue is full promise will be rejected with error "Throttle queue is full".

OptionTypeDefaultDescription
idmixedundefinedKey within lock collection
hashstringundefinedAn object which uniquely identies the object being locked