Skip to content

Commit

Permalink
merge randomInt changes from master
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Jul 16, 2023
1 parent 9f8dc63 commit 3801bb9
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
4 changes: 2 additions & 2 deletions examples/multi-nodes/node-controller.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const _ = require("lodash");
const kleur = require("kleur");
const cluster = require("cluster");
const { humanize } = require("../../src/utils");
const { humanize, randomInt } = require("../../src/utils");

const padS = _.padStart;
const padE = _.padEnd;
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports = {
this.logger.info(`Stopping ${this.nodes.length - num} nodes...`);
const tmp = Array.from(this.nodes);
return _.times(this.nodes.length - num, () => {
const idx = _.random(tmp.length - 1);
const idx = randomInt(tmp.length - 1);
const node = tmp.splice(idx, 1)[0];
if (opts.kill) return this.killNode(node);
else return this.stopNode(node);
Expand Down
4 changes: 2 additions & 2 deletions src/registry/discoverers/etcd3.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const kleur = require("kleur");
const BaseDiscoverer = require("./base");
const { METRIC } = require("../../metrics");
const Serializers = require("../../serializers");
const { removeFromArray } = require("../../utils");
const { removeFromArray, randomInt } = require("../../utils");
const P = require("../../packets");
const C = require("../../constants");

Expand Down Expand Up @@ -43,7 +43,7 @@ class Etcd3Discoverer extends BaseDiscoverer {
});

// Loop counter for full checks. Starts from a random value for better distribution
this.idx = this.opts.fullCheck > 1 ? _.random(this.opts.fullCheck - 1) : 0;
this.idx = this.opts.fullCheck > 1 ? randomInt(this.opts.fullCheck - 1) : 0;

// Etcd client instance
this.client = null;
Expand Down
4 changes: 2 additions & 2 deletions src/registry/discoverers/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { BrokerOptionsError } = require("../../errors");
const BaseDiscoverer = require("./base");
const { METRIC } = require("../../metrics");
const Serializers = require("../../serializers");
const { removeFromArray, isFunction } = require("../../utils");
const { removeFromArray, isFunction, randomInt } = require("../../utils");
const P = require("../../packets");
const C = require("../../constants");

Expand Down Expand Up @@ -43,7 +43,7 @@ class RedisDiscoverer extends BaseDiscoverer {
});

// Loop counter for full checks. Starts from a random value for better distribution
this.idx = this.opts.fullCheck > 1 ? _.random(this.opts.fullCheck - 1) : 0;
this.idx = this.opts.fullCheck > 1 ? randomInt(this.opts.fullCheck - 1) : 0;

// Redis client instance
this.client = null;
Expand Down
4 changes: 2 additions & 2 deletions src/strategies/shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const _ = require("lodash");
const BaseStrategy = require("./base");
const crypto = require("crypto");
const LRU = require("lru-cache");
const { isFunction } = require("../utils");
const { isFunction, randomInt } = require("../utils");

/**
* Sharding invocation strategy
Expand Down Expand Up @@ -76,7 +76,7 @@ class ShardStrategy extends BaseStrategy {
}

// Return a random item (no key)
return list[_.random(0, list.length - 1)];
return list[randomInt(0, list.length - 1)];
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/transporters/tcp/udp-broadcaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const EventEmitter = require("events");
const os = require("os");
const dgram = require("dgram");
const ipaddr = require("ipaddr.js");
const _ = require("lodash");
const { randomInt } = require("../../../src/utils");

/**
* UDP Discovery Server for TcpTransporter
*
Expand Down Expand Up @@ -87,7 +88,7 @@ class UdpServer extends EventEmitter {
})
.then(() => {
// Send first discover message after ~1 sec
setTimeout(() => this.discover(), _.random(500) + 500);
setTimeout(() => this.discover(), randomInt(500) + 500);

this.startDiscovering();
});
Expand Down
28 changes: 28 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,34 @@ const utils = {
.map(x => x.trim())
.filter(Boolean)
: [];
},

/**
* Produces a random floating number between the inclusive lower and upper bounds.
*
* @param {Number} a
* @param {Number} b
* @returns {Number}
*/
random(a = 1, b = 0) {
const lower = Math.min(a, b);
const upper = Math.max(a, b);

return lower + Math.random() * (upper - lower);
},

/**
* Produces a random integer number between the inclusive lower and upper bounds.
*
* @param {Number} a
* @param {Number} b
* @returns {Number}
*/
randomInt(a = 1, b = 0) {
const lower = Math.ceil(Math.min(a, b));
const upper = Math.floor(Math.max(a, b));

return Math.floor(lower + Math.random() * (upper - lower + 1));
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/services/posts.service.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const _ = require("lodash");
const fakerator = require("fakerator")();

const { delay } = require("../../src/utils");
const { delay, randomInt } = require("../../src/utils");

module.exports = function () {
let posts = fakerator.times(fakerator.entity.post, 10);

_.each(posts, (post, i) => {
post.id = i + 1;
post.author = _.random(1, 5);
post.author = randomInt(1, 5);
});

return {
Expand Down
5 changes: 4 additions & 1 deletion test/unit/service-broker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ jest.mock("../../src/utils", () => ({
functionArguments() {
return ["ctx"];
},
deprecate() {}
deprecate() {},
randomInt() {
return 2;
}
}));
polyfillPromise = jest.requireActual("../../src/utils").polyfillPromise;

Expand Down

0 comments on commit 3801bb9

Please sign in to comment.