Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve jsdoc types #69

Open
wants to merge 4 commits into
base: elevated_limits_quota_on_params
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 25 additions & 81 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="types.js" />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: I have not seen this before with jsdocs, what is it doing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its to reference types defined in another file. like a require for types.

Not sure if this is the best way though.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I had no idea importing them was actually required!

const ms = require('ms');
const fs = require('fs');
const _ = require('lodash');
Expand Down Expand Up @@ -37,7 +38,7 @@ class LimitDBRedis extends EventEmitter {

/**
* Creates an instance of LimitDB client for Redis.
* @param {params} params - The configuration for the database and client.
* @param {LimitDBParams} params - The configuration for the database and client.
*/
constructor(config) {
super();
Expand Down Expand Up @@ -149,9 +150,9 @@ class LimitDBRedis extends EventEmitter {
}

/**
* @param {string} type
* @param {object} params
* @returns
* @param {type & { overrides: Object?, overridesCache: Object?, overridesMatch: Object? }} type
* @param {GetParams | PutParams | TakeParams | TakeElevatedParams} params
* @returns {NormalizedType}
*/
bucketKeyConfig(type, params) {
if (typeof params.configOverride === 'object') {
Expand Down Expand Up @@ -194,9 +195,9 @@ class LimitDBRedis extends EventEmitter {
/**
* Take N elements from a bucket if available.
*
* @param {takeParams} params - The params for take.
* @param {function(Error, takeResult)} callback.
* @param {function(key, bucketKeyConfig, count)} takeFunc
* @param {TakeParams} params - The params for take.
* @param {function(Error, TakeResult)} callback.
* @param {function(key: string, bucketKeyConfig: NormalizedType, count: number)} takeFunc
*/
_doTake(params, callback, takeFunc) {
const valError = validateParams(params, this.buckets);
Expand Down Expand Up @@ -255,8 +256,9 @@ class LimitDBRedis extends EventEmitter {
/**
* Take N elements from a bucket if available.
*
* @param {takeParams} params - The params for take.
* @param {function(Error, takeResult)} callback.
* @param {TakeParams} params
* @param {function(Error)} callback.
* @param {function(null, TakeResult)} callback.
shadow-dahm marked this conversation as resolved.
Show resolved Hide resolved
*/
take(params, callback) {
this._doTake(params, callback, (key, bucketKeyConfig, count) => {
Expand Down Expand Up @@ -289,6 +291,13 @@ class LimitDBRedis extends EventEmitter {
})
}

/**
* Take N elements from a bucket if available, use elevated limits if configured.
*
* @param {TakeElevatedParams} params
* @param {function(null, TakeElevatedResult)} callback.
* @param {function(Error)} callback.
*/
takeElevated(params, callback) {
let erlParams;

Expand Down Expand Up @@ -355,8 +364,8 @@ class LimitDBRedis extends EventEmitter {
* Take N elements from a bucket if available otherwise wait for them.
* The callback is called when the number of request tokens is available.
*
* @param {waitParams} params - The params for take.
* @param {function(Error, waitResult)} callback.
* @param {WaitParams} params - The params for take.
* @param {function(Error, WaitResult)} callback.
*/
wait(params, callback) {
this.take(params, (err, result) => {
Expand Down Expand Up @@ -385,8 +394,8 @@ class LimitDBRedis extends EventEmitter {
/**
* Put N elements in the bucket.
*
* @param {putParams} params - The params for take.
* @param {function(Error, putResult)} [callback].
* @param {PutParams} params - The params for take.
* @param {function(Error, PutResult)} [callback].
*/
put(params, callback) {
callback = callback || _.noop;
Expand Down Expand Up @@ -438,8 +447,8 @@ class LimitDBRedis extends EventEmitter {
/**
* Get elements in the bucket.
*
* @param {getParams} params - The params for take.
* @param {function(Error, getResult)} [callback].
* @param {GetParams} params - The params for take.
* @param {function(Error, GetResult)} [callback].
shadow-dahm marked this conversation as resolved.
Show resolved Hide resolved
*/
get(params, callback) {
callback = callback || _.noop;
Expand Down Expand Up @@ -508,69 +517,4 @@ class LimitDBRedis extends EventEmitter {
}


module.exports = LimitDBRedis;

/**
* And now some typedefs for you:
*
* @typedef {Object} type
* @property {integer} [per_interval] The number of tokens to add per interval.
* @property {integer} [interval] The length of the interval in milliseconds.
* @property {integer} [size] The maximum number of tokens in the bucket.
* @property {integer} [per_second] The number of tokens to add per second. Equivalent to "interval: 1000, per_interval: x".
* @property {integer} [per_minute] The number of tokens to add per minute. Equivalent to "interval: 60000, per_interval: x".
* @property {integer} [per_hour] The number of tokens to add per hour. Equivalent to "interval: 3600000, per_interval: x".
* @property {integer} [per_day] The number of tokens to add per day. Equivalent to "interval: 86400000, per_interval: x".
*
* @typedef {Object} params
* uri nodes buckets prefix
* @property {string} [params.uri] Address of Redis.
* @property {Object.<string, object>} [params.nodes] Redis Cluster Configuration https://github.com/luin/ioredis#cluster".
* @property {Object.<string, type>} [params.types] The buckets configuration.
* @property {string} [params.prefix] Prefix keys in Redis.
* @property {type} [params.configOverride] Bucket configuration override
*
* @typedef takeParams
* @property {string} type The name of the bucket type.
* @property {string} key The key of the bucket instance.
* @property {integer} [count=1] The number of tokens to take from the bucket.
* @property {type} configOverride Externally provided bucket configruation
*
* @typedef takeResult
* @property {boolean} conformant Returns true if there is enough capacity in the bucket and the tokens has been removed.
* @property {integer} remaining The number of tokens remaining in the bucket.
* @property {integer} reset A unix timestamp indicating when the bucket is going to be full.
* @property {integer} limit The size of the bucket.
*
* @typedef waitParams
* @property {string} type The name of the bucket type.
* @property {string} key The key of the bucket instance.
* @property {integer} [count=1] The number of tokens to wait for.
* @property {type} configOverride Externally provided bucket configruation
*
* @typedef waitResult
* @property {integer} remaining The number of tokens remaining in the bucket.
* @property {integer} reset A unix timestamp indicating when the bucket is going to be full.
* @property {integer} limit The size of the bucket.
*
* @typedef putParams
* @property {string} type The name of the bucket type.
* @property {string} key The key of the bucket instance.
* @property {integer} [count=SIZE] The number of tokens to put in the bucket. Defaults to the size of the bucket.
* @property {type} configOverride Externally provided bucket configruation
*
* @typedef putResult
* @property {integer} remaining The number of tokens remaining in the bucket.
* @property {integer} reset A unix timestamp indicating when the bucket is going to be full.
* @property {integer} limit The size of the bucket.
*
* @typedef getParams
* @property {string} type The name of the bucket type.
* @property {string} key The key of the bucket instance.
* @property {type} configOverride Externally provided bucket configruation
*
* @typedef getResult
* @property {integer} remaining The number of tokens remaining in the bucket.
* @property {integer} reset A unix timestamp indicating when the bucket is going to be full.
* @property {integer} limit The size of the bucket.
*/
module.exports = LimitDBRedis;
155 changes: 155 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// --- Public Types ---

/**
* @typedef {Object} LimitDBParams
* uri nodes buckets prefix
* @property {string} [params.uri] Address of Redis.
* @property {Object.<string, object>} [params.nodes] Redis Cluster Configuration https://github.com/luin/ioredis#cluster".
* @property {Object.<string, type>} [params.types] The buckets configuration.
* @property {string} [params.prefix] Prefix keys in Redis.
* @property {type} [params.configOverride] Bucket configuration override
*/

/**
* @typedef {Object} type
* @property {number} [per_interval] The number of tokens to add per interval.
* @property {number} [interval] The length of the interval in milliseconds.
* @property {number} [size] The maximum number of tokens in the bucket.
* @property {number} [per_second] The number of tokens to add per second. Equivalent to "interval: 1000, per_interval: x".
* @property {number} [per_minute] The number of tokens to add per minute. Equivalent to "interval: 60000, per_interval: x".
* @property {number} [per_hour] The number of tokens to add per hour. Equivalent to "interval: 3600000, per_interval: x".
* @property {number} [per_day] The number of tokens to add per day. Equivalent to "interval: 86400000, per_interval: x".
* @property {number} [unlimited] the maximum number of tokens in the bucket. equivalent to "size: x".
* @property {number} [skip_n_calls] the number of calls to skip. equivalent to "size: x".
* @property {ElevatedLimitParams} [elevated_limits] The elevated limit configuration.
*/

/**
* @typedef TakeParams
* @property {string} type The name of the bucket type.
* @property {string} key The key of the bucket instance.
* @property {number} [count=1] The number of tokens to take from the bucket.
* @property {type} configOverride Externally provided bucket configuration
*/

/**
* @typedef TakeResult
* @property {boolean} conformant Returns true if there is enough capacity in the bucket and the tokens has been removed.
* @property {number} remaining The number of tokens remaining in the bucket.
* @property {number} reset A unix timestamp indicating when the bucket is going to be full.
* @property {number} limit The size of the bucket.
*/
/**

* @typedef WaitParams
* @property {string} type The name of the bucket type.
* @property {string} key The key of the bucket instance.
* @property {number} [count=1] The number of tokens to wait for.
* @property {type} configOverride Externally provided bucket configruation
*/

/**
* @typedef WaitResult
* @property {number} remaining The number of tokens remaining in the bucket.
* @property {number} reset A unix timestamp indicating when the bucket is going to be full.
* @property {number} limit The size of the bucket.
*/

/**
* @typedef PutParams
* @property {string} type The name of the bucket type.
* @property {string} key The key of the bucket instance.
* @property {number} [count=SIZE] The number of tokens to put in the bucket. Defaults to the size of the bucket.
* @property {type} configOverride Externally provided bucket configruation
*/

/**
* @typedef PutResult
* @property {number} remaining The number of tokens remaining in the bucket.
* @property {number} reset A unix timestamp indicating when the bucket is going to be full.
* @property {number} limit The size of the bucket.
*/

/**
* @typedef GetParams
* @property {string} type The name of the bucket type.
* @property {string} key The key of the bucket instance.
* @property {type} configOverride Externally provided bucket configuration
*/

/**
* @typedef GetResult
* @property {number} remaining The number of tokens remaining in the bucket.
* @property {number} reset A unix timestamp indicating when the bucket is going to be full.
* @property {number} limit The size of the bucket.
*/

/**
* @typedef {Object} TakeElevatedParams
* @property {string} type - The name of the bucket type.
* @property {string} key - The key of the bucket instance.
* @property {number} [count=1] - The number of tokens to take from the bucket.
* @property {ElevatedLimitParams} [elevated_limits] - (Optional) The elevated limit configuration.
* @property {type} configOverride Externally provided bucket configuration
*/

/**
* @typedef {Object} ElevatedLimitParams
* @property {string} erl_is_active_key - The key to check if the elevated limits are active.
* @property {string} erl_quota_key - The key to store the quota for the elevated limits.
* @property {number} erl_activation_period_seconds - The activation period for the elevated limits in seconds.
* // temporal options
* @property {number} [per_interval] The number of tokens to add per interval.
* @property {number} [interval] The length of the interval in milliseconds.
* @property {number} [size] The maximum number of tokens in the bucket.
* @property {number} [per_second] The number of tokens to add per second. Equivalent to "interval: 1000, per_interval: x".
* @property {number} [per_minute] The number of tokens to add per minute. Equivalent to "interval: 60000, per_interval: x".
* @property {number} [per_hour] The number of tokens to add per hour. Equivalent to "interval: 3600000, per_interval: x".
* @property {number} [per_day] The number of tokens to add per day. Equivalent to "interval: 86400000, per_interval: x".
* @property {number} [unlimited] The maximum number of tokens in the bucket. Equivalent to "size: x".
*/

/**
* @typedef {Object} TakeElevatedResult
* @property {boolean} conformant - Returns true if there is enough capacity in the bucket and the tokens has been removed.
* @property {number} remaining - The number of tokens remaining in the bucket.
* @property {number} reset - A unix timestamp indicating when the bucket is going to be full.
* @property {number} limit - The size of the bucket.
* @property {boolean} delayed - Indicates if the operation was delayed.
* @property {Elevated_result} elevated_limits - The elevated limit result
*/

/**
* @typedef {Object} Elevated_result
* @property {boolean} erl_configured_for_bucket - Indicates if the bucket is configured for elevated limits.
* @property {boolean} triggered - Indicates if the elevated limits were triggered.
* @property {boolean} activated - Indicates if the elevated limits were activated.
* @property {number} quota_remaining - The remaining quota for elevated limits.
* @property {number} quota_allocated - The allocated quota for elevated limits.
* @property {number} erl_activation_period_seconds - The activation period for elevated limits in seconds.
*/

// --- Internal Types ---

/**
* @typedef {Object} NormalizedType -- the internal representation of a bucket
* @property {number} [per_interval] The number of tokens to add per interval.
* @property {number} [interval] The length of the interval in milliseconds.
* @property {number} [size] The maximum number of tokens in the bucket.
* @property {number} [ttl] The time to live for the bucket in seconds.
* @property {number} [ms_per_interval] The number of milliseconds per interval.
* @property {number} [drip_interval] The interval for the drip in milliseconds.
* @property {number} [unlimited] the maximum number of tokens in the bucket. equivalent to "size: x".
* @property {number} [skip_n_calls] the number of calls to skip. equivalent to "size: x".
* @property {NormalizedType} [elevated_limits] The elevated limit configuration.
* @property {boolean} [erl_configured_for_bucket] Indicates if the bucket is configured for elevated limits.
*/

/**
* @typedef {Object} ElevatedLimitConfiguration
* @property {string} erl_is_active_key - The key to check if the elevated limits are active.
* @property {string} erl_quota_key - The key to store the quota for the elevated limits.
* @property {number} erl_activation_period_seconds - The activation period for the elevated limits in seconds.
* @property {number} erl_quota - The quota for the elevated limits.
* @property {string} erl_quota_interval - The interval for the quota.
*/
Loading