APIs to manage counters
APIs to manage the counter for specific occurrences.
- The count object
- rl.countCreate()
The count object provides the following APIs to modify the value of a counter
for the current key.
All API specifications use the count object that is assumed to be defined, for
example, by the following statements.
var rl = require('ratelimit');
var count = rl.countCreate('192.168.1.1', 15);
rl.countCreate()
Creates and initializes a count
object.
- Syntax
rl.countCreate(key[,initialValue])
- Parameters
-
- key
- The unique identifier that represents a counter. The key identifies the traffic type. The key can be any characters accessible in the GatewayScript file.
- initialValue
- The initial value of a counter. This parameter is an integer. The value is in the range 0 - (2**53 - 1). The default value is zero.
- Guidelines
- The rl.countCreate() API creates a counter for counting the number of any specific occurrences. The counter is associated with the specified key.
- Example
-
Define the initial value of a counter as 15. This counter is for key
192.168.1.1
.var rl = require('ratelimit'); var count = rl.countCreate('192.168.1.1', 15);
count.add()
Increases the value of a counter.
- Syntax
count.add(number,function(err,count))
- Parameters
-
- number
- The specific number to add to the counter. This parameter is an integer. The value is in the range 0 - (2**53 - 1).
- err
- The error information if any error occurs during the processing.
- count
- The current value of a counter.
- Guidelines
- The
count.add()
API increases the value of the counter for the current key by a certain number. - Example
- Add the value of a counter by one.
var hm = require('header-metadata'), sm = require('service-metadata'), rl = require('ratelimit'); var key = hm.current.get('Host'); //Define the initial value of the counter as 10 var initialValue = 10; var counter = rl.countCreate(key, initialValue); counter.add(1, function(err, count){ if (err) { console.error('Error:' + err); //Error handling } else { console.info('Add one token to counter successfully'); } });
count.count()
Returns the value of a counter.
- Syntax
count.count(function(err,count))
- Parameters
-
- err
- The error information if any error occurs during the processing.
- count
- The current value of a counter.
- Guidelines
- The
count.count()
API obtains the value of the counter for the current key.
count.reset()
Resets the counter to its initial value.
- Syntax
count.reset(function(err))
- Parameters
-
- err
- The error information if any error occurs during the processing.
- Guidelines
- The count.reset() API resets the value of the counter for the current key to its initial value. The rl.countCreate() API defined the initial value. If the initial value is not provided, the counter value is reset to zero. The reset value takes effect immediately after the count.reset() API is called.
- Example
- Reset the value of the counter for key 192.168.1.1 to its initial
value.
var rl = require('ratelimit'); var counter = rl.countCreate('192.168.1.1'); if(counter){ counter.reset(function(err) { if (err) { console.error('Error:' + err); } else { console.info('Reset the counter'); } }); }
count.set()
Assigns a new value to the counter.
- Syntax
count.set(number,function(err))
- Parameters
-
- number
- The value to assign to the counter. This parameter is an integer. The value is in the range 0 - (2**53 - 1).
- err
- The error information if any error occurs during the processing.
- Guidelines
- The count.set() API assigns a new value to the value of the counter for the current key. The new value takes effect immediately after the count.set() API is called.
- Example
- Set the value of the counter for key 192.168.1.1 to
100.
var rl = require('ratelimit'); var counter = rl.countCreate('192.168.1.1'); var number = 100; counter.set(number, function(err){ if(err) { console.error('Error:' + err); session.reject('exceeded limit'); } else { console.info('Count is set'); } });
count.sub()
Subtracts the value of a counter.
- Syntax
count.sub(number,function(err,count))
- Parameters
-
- number
- The specific number to subtract from the counter. This parameter is an integer. The value is in the range 0 - (2**53 - 1).
- err
- The error information if any error occurs during the processing.
- count
- The current value of the counter. The value can be negative.
- Guidelines
- The count.sub() API subtracts the value of the counter for the current key by a certain number.
- Example
- Subtract the value of a counter by one.
var hm = require('header-metadata'), sm = require('service-metadata'), rl = require('ratelimit'); var key = hm.current.get('Host'); //Define the initial value of the counter as 10 var initialValue = 10; var counter = rl.countCreate(key, initialValue); counter.sub(1, function(err, count){ if (err) { console.error('Error:' + err); //Error handling } else { console.info('Subtract one token from counter'); } });