limitd-client
Advanced tools
Comparing version 2.7.2 to 2.7.3
{ | ||
"name": "limitd-client", | ||
"version": "2.7.2", | ||
"version": "2.7.3", | ||
"description": "limitd client for node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,4 +16,4 @@ [![Build Status](https://travis-ci.org/limitd/node-client.svg)](https://travis-ci.org/limitd/node-client) | ||
```javascript | ||
var LimitdClient = require('limitd-client'); | ||
var limitd = new LimitdClient('limitd://localhost:9001'); | ||
const LimitdClient = require('limitd-client'); | ||
const limitd = new LimitdClient('limitd://localhost:9001'); | ||
@@ -85,2 +85,36 @@ //express middleware | ||
## Sharding | ||
Sharding is implemented in the client-side by providing a list of limitd servers. | ||
Example | ||
```javascript | ||
const LimitdClient = require('limitd-client'); | ||
const limitd = new LimitdClient({ | ||
shard: { | ||
hosts: [ 'limitd://host-1', 'limitd://host-2' ] | ||
} | ||
}); | ||
limitd.take('ip', 'test', (err, resp) => console.dir(resp)); | ||
``` | ||
Alternatively you can have a DNS record and use autodiscovery: | ||
```javascript | ||
const LimitdClient = require('limitd-client'); | ||
const limitd = new LimitdClient({ | ||
shard: { | ||
autodiscovery: 'limitds.internal.company.com' | ||
} | ||
}); | ||
limitd.take('ip', 'test', (err, resp) => console.dir(resp)); | ||
``` | ||
This record will be poll every 5 minutes. | ||
## License | ||
@@ -87,0 +121,0 @@ |
@@ -9,2 +9,3 @@ const LimitdClient = require('./client'); | ||
const util = require('util'); | ||
const url = require('url'); | ||
@@ -28,3 +29,9 @@ const REFRESH_AFTER_MS = 1000 * 60 * 5; | ||
if (Array.isArray(this._options.shard.hosts)) { | ||
this.clients = _.sortBy(this._options.shard.hosts).map(host => this.createClient(host)); | ||
this.clients = _.sortBy(this._options.shard.hosts).map(host => { | ||
if (url.parse(host).protocol === null) { | ||
return this.createClient(`limitd://${host}:${this._options.port}`); | ||
} else { | ||
return this.createClient(host); | ||
} | ||
}); | ||
} else if (this._options.shard.autodiscover) { | ||
@@ -31,0 +38,0 @@ this.autodiscover = this._options.shard.autodiscover; |
@@ -18,2 +18,17 @@ const ShardClient = require('../shard_client'); | ||
it('should work when full url are provided', function() { | ||
const client = function(params) { | ||
this.host = params.host; | ||
}; | ||
const ShardClient = ShardClientCtor(client); | ||
const shardClient = new ShardClient({ | ||
shard: { hosts: [ 'limitd://host-2:9231', 'limitd://host-1:9231' ] } | ||
}); | ||
assert.equal(shardClient.clients[0].host, 'limitd://host-1:9231'); | ||
assert.equal(shardClient.clients[1].host, 'limitd://host-2:9231'); | ||
}); | ||
it('should create a new client for each host', function() { | ||
@@ -30,6 +45,17 @@ const client = function(params) { | ||
assert.equal(shardClient.clients[0].host, 'host-1'); | ||
assert.equal(shardClient.clients[1].host, 'host-2'); | ||
assert.equal(shardClient.clients[0].host, 'limitd://host-1:9231'); | ||
assert.equal(shardClient.clients[1].host, 'limitd://host-2:9231'); | ||
}); | ||
['take', 'put', 'wait', 'status', 'on', 'once', 'ping'].forEach(method => { | ||
it(`should define ${method}`, function() { | ||
const shardClient = new ShardClient({ | ||
client: ()=>{}, | ||
shard: { hosts: [ 'host-1', 'host-2' ] } | ||
}); | ||
assert.isFunction(shardClient[method]); | ||
}); | ||
}); | ||
it('should invoke PUT on the client based on the hash', function(done) { | ||
@@ -39,3 +65,3 @@ const client = function(params) { | ||
this.put = function(type, key, count, callback) { | ||
assert.equal(this.host, 'host-2'); | ||
assert.equal(this.host, 'limitd://host-2:9231'); | ||
assert.equal(type, 'ip'); | ||
@@ -63,3 +89,3 @@ assert.equal(key, '10.0.0.1'); | ||
this.take = function(type, key, count, callback) { | ||
assert.equal(this.host, 'host-1'); | ||
assert.equal(this.host, 'limitd://host-1:9231'); | ||
assert.equal(type, 'ip'); | ||
@@ -86,3 +112,3 @@ assert.equal(key, '10.0.0.2'); | ||
this.put = function(type, key, count, callback) { | ||
assert.equal(this.host, 'host-1'); | ||
assert.equal(this.host, 'limitd://host-1:9231'); | ||
assert.equal(type, 'ip'); | ||
@@ -126,6 +152,6 @@ assert.equal(key, '10.0.0.2'); | ||
if (err) { return done(err); } | ||
assert.include(response.items, 'item1-from-host-1'); | ||
assert.include(response.items, 'item2-from-host-1'); | ||
assert.include(response.items, 'item1-from-host-2'); | ||
assert.include(response.items, 'item2-from-host-2'); | ||
assert.include(response.items, 'item1-from-limitd://host-1:9231'); | ||
assert.include(response.items, 'item2-from-limitd://host-1:9231'); | ||
assert.include(response.items, 'item1-from-limitd://host-2:9231'); | ||
assert.include(response.items, 'item2-from-limitd://host-2:9231'); | ||
done(); | ||
@@ -139,3 +165,3 @@ }); | ||
this.status = function(type, prefix, callback) { | ||
if (this.host === 'host-2') { | ||
if (this.host === 'limitd://host-2:9231') { | ||
return callback(new Error('unreachable')); | ||
@@ -161,6 +187,6 @@ } | ||
assert.equal(response.errors[0].message, 'unreachable'); | ||
assert.include(response.items, 'item1-from-host-1'); | ||
assert.include(response.items, 'item2-from-host-1'); | ||
assert.notInclude(response.items, 'item1-from-host-2'); | ||
assert.notInclude(response.items, 'item2-from-host-2'); | ||
assert.include(response.items, 'item1-from-limitd://host-1:9231'); | ||
assert.include(response.items, 'item2-from-limitd://host-1:9231'); | ||
assert.notInclude(response.items, 'item1-from-limitd://host-2:9231'); | ||
assert.notInclude(response.items, 'item2-from-limitd://host-2:9231'); | ||
done(); | ||
@@ -167,0 +193,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41316
1057
123