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

Remove usage of _.random #1212

Merged
merged 2 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions dev/bulkhead.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const _ = require("lodash");
const ServiceBroker = require("../src/service-broker");
const E = require("../src/errors");
const { randomInt } = require("../src/utils");

const broker = new ServiceBroker({
bulkhead: {
Expand Down Expand Up @@ -31,7 +31,7 @@ broker.createService({
concurrency: 1
},
async handler(ctx) {
await this.Promise.delay(_.random(500, 2500));
await this.Promise.delay(randomInt(500, 2500));

this.logger.info("First called.", ctx.params);

Expand All @@ -45,7 +45,7 @@ broker.createService({
concurrency: 1
},
async handler(ctx) {
await this.Promise.delay(_.random(500, 2500));
await this.Promise.delay(randomInt(500, 2500));

this.logger.info("Second called.", ctx.params);

Expand All @@ -62,15 +62,15 @@ broker.createService({
},
async handler(ctx) {
this.logger.info("Event received.", ctx.params);
await this.Promise.delay(_.random(500, 2500));
await this.Promise.delay(randomInt(500, 2500));
this.logger.info("User created.", ctx.params);
}
},

"post.created": {
async handler(ctx) {
this.logger.info("Event received.", ctx.params);
await this.Promise.delay(_.random(500, 2500));
await this.Promise.delay(randomInt(500, 2500));
this.logger.info("Post created.", ctx.params);
}
}
Expand Down
8 changes: 3 additions & 5 deletions dev/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
const _ = require("lodash");
const kleur = require("kleur");
const fs = require("fs");
const { MoleculerError, MoleculerRetryableError } = require("../src/errors");
const Middlewares = require("..").Middlewares;

const ServiceBroker = require("../src/service-broker");
const { randomInt } = require("../src/utils");

// Create broker
const broker = new ServiceBroker({
Expand Down Expand Up @@ -138,9 +137,8 @@ broker.createService({
actions: {
add(ctx) {
broker.logger.info(_.padEnd(`${ctx.params.count}. Add ${ctx.params.a} + ${ctx.params.b}`, 20), `(from: ${ctx.nodeID})`);
if (_.random(100) > 70)
if (randomInt(100) > 70)
return this.Promise.reject(new MoleculerRetryableError("Random error!", 510));

return {
count: ctx.params.count,
res: Number(ctx.params.a) + Number(ctx.params.b)
Expand Down Expand Up @@ -173,7 +171,7 @@ broker
pendingInfo = ` [${pendingReqs.join(",")}]`;
}

const payload = { a: _.random(0, 10), b: _.random(0, 10) };
const payload = { a: randomInt(0, 10), b: randomInt(0, 10) };
const count = ++reqCount;
pendingReqs.push(count);
let p = broker.call("math.add", payload, { meta: { count } });
Expand Down
7 changes: 4 additions & 3 deletions dev/gossip-viz.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
const _ = require("lodash");
const kleur = require("kleur");
const ServiceBroker = require("../src/service-broker");
const { randomInt } = require("../src/utils");

const COUNT = process.argv[2] ? Number(process.argv[2]) : 20;
const nodePrefix = process.argv[3] || "node";
const namespace = "viz-" + Math.round(_.random(100));
const namespace = "viz-" + randomInt(100);

console.log(`Create ${COUNT} nodes...`);

Expand Down Expand Up @@ -55,8 +56,8 @@ Promise.all(brokers.map(({ broker }) => broker.start())).then(() => {
printStatuses();
}, 1000);

const timer = setInterval(() => {
const idx = _.random(brokers.length - 1);
setInterval(() => {
const idx = randomInt(brokers.length - 1);
const { nodeID, broker } = brokers[idx];

if (broker) {
Expand Down
9 changes: 3 additions & 6 deletions dev/server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"use strict";

const path = require("path");
const _ = require("lodash");
const kleur = require("kleur");
const ServiceBroker = require("../src/service-broker");
const { MoleculerError, MoleculerRetryableError } = require("../src/errors");
const Middlewares = require("../src/middlewares");
const { MoleculerRetryableError } = require("../src/errors");
const { randomInt } = require("../src/utils");

// Create broker
const broker = new ServiceBroker({
Expand Down Expand Up @@ -83,12 +81,11 @@ broker.createService({
//fallback: (ctx, err) => ({ count: ctx.params.count, res: 999, fake: true }),
//fallback: "fakeResult",
handler(ctx) {
const wait = _.random(500, 1500);
this.logger.info(
_.padEnd(`${ctx.meta.count}. Add ${ctx.params.a} + ${ctx.params.b}`, 20),
`(from: ${ctx.nodeID})`
);
if (_.random(100) > 80)
if (randomInt(100) > 80)
return this.Promise.reject(
new MoleculerRetryableError("Random error!", 510, "RANDOM_ERROR")
);
Expand Down
7 changes: 4 additions & 3 deletions dev/sharding.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const ServiceBroker = require("../src/service-broker");
const _ = require("lodash");
const ServiceBroker = require("../src/service-broker");
const { randomInt } = require("../src/utils");

function createBroker(opts) {
const broker = new ServiceBroker(
Expand Down Expand Up @@ -39,7 +40,7 @@ function createBroker(opts) {
},
handler(ctx) {
//this.logger.warn(`Called '${this.broker.nodeID}' with '${ctx.params.name}'`);
return 20 + _.random(60);
return 20 + randomInt(60);
}
}
}
Expand Down Expand Up @@ -69,7 +70,7 @@ async function start() {
const usernames = ["john", "bob", "adam", "steve", "mark"];

setInterval(async () => {
const name = usernames[_.random(usernames.length - 1)];
const name = usernames[randomInt(usernames.length - 1)];
await main.call("users.getAge", { name });
}, 1000);
}
Expand Down
10 changes: 5 additions & 5 deletions dev/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ServiceBroker = require("../src/service-broker");

const { MoleculerError } = require("../src/errors");
const _ = require("lodash");
const { inspect } = require("util");
const { randomInt } = require("../src/utils");

const THROW_ERR = false;

Expand Down Expand Up @@ -244,7 +244,7 @@ broker.createService({
},
async handler(ctx) {
const span1 = ctx.startSpan("Fake delay");
//await this.Promise.delay(10 + _.random(30));
// await this.Promise.delay(10 + randomInt(30));
ctx.finishSpan(span1);
return ctx.params.postID * 3;
}
Expand All @@ -265,7 +265,7 @@ broker.createService({
userID: ctx.params.userID
});

await this.Promise.delay(_.random(10));
await this.Promise.delay(randomInt(10));
return ctx.params.userID * 3;
}
}
Expand All @@ -280,7 +280,7 @@ broker.createService({
count: {
tracing: true,
async handler(ctx) {
//await this.Promise.delay(_.random(50));
// await this.Promise.delay(randomInt(50));
return Math.round(Math.random() * 10);
}
}
Expand All @@ -295,7 +295,7 @@ broker.createService({
count: {
tracing: true,
async handler(ctx) {
//await this.Promise.delay(_.random(50));
// await this.Promise.delay(randomInt(50));
return Math.round(Math.random() * 10);
}
}
Expand Down
4 changes: 3 additions & 1 deletion examples/client-server/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ let kleur = require("kleur");

let ServiceBroker = require("../../src/service-broker");

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

let transporter = process.env.TRANSPORTER || "TCP";

// Create broker
Expand Down Expand Up @@ -81,7 +83,7 @@ broker
pendingInfo = ` [${pendingReqs.join(",")}]`;
}

let payload = { a: _.random(0, 100), b: _.random(0, 100), count: ++reqCount };
let payload = { a: randomInt(0, 100), b: randomInt(0, 100), count: ++reqCount };
pendingReqs.push(reqCount);
let p = broker.call("math.add", payload);
if (p.ctx) {
Expand Down
4 changes: 1 addition & 3 deletions examples/client-server/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";

let _ = require("lodash");
let kleur = require("kleur");

let ServiceBroker = require("../../src/service-broker");
let { MoleculerError } = require("../../src/errors");

let transporter = process.env.TRANSPORTER || "TCP";

Expand All @@ -23,7 +21,7 @@ broker.createService({
name: "math",
actions: {
add(ctx) {
// if (_.random(100) > 90) {
// if (randomInt(100) > 90) {
// this.logger.info(kleur.bold.red("Throw random error..."));
// throw new MoleculerError("Random error!", 510);
// }
Expand Down
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
12 changes: 8 additions & 4 deletions examples/post.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
let _ = require("lodash");
let fakerator = require("fakerator")();

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

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

for (let i = 0; i < posts.length; i++) {
const post = posts[i];

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

return {
name: "posts",
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
7 changes: 4 additions & 3 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 @@ -67,7 +67,8 @@ class ShardStrategy extends BaseStrategy {
* @memberof ShardStrategy
*/
select(list, ctx) {
let key = this.getKeyFromContext(ctx);
const key = this.getKeyFromContext(ctx);

if (key != null) {
if (this.needRebuild) this.rebuild(list);

Expand All @@ -76,7 +77,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
Loading