Skip to content

Commit

Permalink
Request & Response classes
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Mar 24, 2017
1 parent a7eaf4c commit b25f591
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
"type": "node",
"request": "launch",
"name": "Launch perf",
"program": "${workspaceRoot}\\perf-runner.js",
"cwd": "${workspaceRoot}"
"program": "${workspaceRoot}\\benchmark\\perf-runner.js",
"cwd": "${workspaceRoot}\\benchmark"
},
{
"type": "node",
Expand Down
20 changes: 10 additions & 10 deletions benchmark/suites/transporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ Platform info:
Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz × 8
Suite: Transport with 10bytes
√ Fake x 17,563 ops/sec ±1.39% (82 runs sampled)
√ NATS x 4,655 ops/sec ±1.18% (87 runs sampled)
√ Redis x 4,865 ops/sec ±1.34% (83 runs sampled)
√ MQTT x 4,427 ops/sec ±1.16% (84 runs sampled)
Fake 0.00% (17,563 ops/sec)
NATS -73.49% (4,655 ops/sec)
Redis -72.30% (4,865 ops/sec)
MQTT -74.79% (4,427 ops/sec)
-----------------------------------------------------------------------
√ Fake x 26,667 ops/sec ±2.16% (85 runs sampled)
√ NATS x 5,011 ops/sec ±1.40% (84 runs sampled)
√ Redis x 5,001 ops/sec ±1.31% (83 runs sampled)
√ MQTT x 4,581 ops/sec ±1.12% (87 runs sampled)
Fake 0.00% (26,667 ops/sec)
NATS -81.21% (5,011 ops/sec)
Redis -81.25% (5,001 ops/sec)
MQTT -82.82% (4,581 ops/sec)
-------------------------------------------------------------------------
*/
67 changes: 49 additions & 18 deletions src/transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,44 @@ const TOPIC_INFO = "INFO";
const TOPIC_DISCONNECT = "DISCONNECT";
const TOPIC_HEARTBEAT = "HEARTBEAT";

class Request {
constructor(nodeID, requestID, action, params) {
this.nodeID = nodeID;
this.requestID = requestID;
this.action = action;
this.params = params;
}

toString() {
//return `{"nodeID":"${this.nodeID}", "requestID":"${this.requestID}", "action":"${this.action}", "params": ${utils.json2String(this.params)} }`;
//return `{"nodeID":"${this.nodeID}", "requestID":"${this.requestID}", "action":"${this.action}", "params": { "a": 1 } }`;
return utils.json2String(this);
}
}

class Response {
constructor(nodeID, requestID, data, err) {
this.nodeID = nodeID;
this.requestID = requestID;
this.data = data;
if (err) {
this.error = {
name: err.name,
message: err.message,
code: err.code,
data: err.data
};
}
this.success = err == null;
}

toString() {
//return `{"nodeID":"${this.nodeID}", "requestID":"${this.requestID}", "success":"${this.success}", "data": ${utils.json2String(this.data)} }`;
//return `{"nodeID":"${this.nodeID}", "requestID":"${this.requestID}", "success":"${this.success}", "data": { "a": 1 } }`;
return utils.json2String(this);
}
}

/**
* Transit class
*
Expand Down Expand Up @@ -292,12 +330,13 @@ class Transit {
timer: null
};

const payload = {
const payload = new Request(this.nodeID, ctx.id, ctx.action.name, ctx.params);
/*const payload = {
nodeID: this.nodeID,
requestID: ctx.id,
action: ctx.action.name,
params: ctx.params,
};
};*/

this.logger.info(`Call '${ctx.action.name}' action on '${ctx.nodeID}' node...`/*, payload*/);

Expand Down Expand Up @@ -338,24 +377,11 @@ class Transit {
* @memberOf Transit
*/
sendResponse(nodeID, requestID, data, err) {
let payload = {
success: err == null,
nodeID: this.nodeID,
requestID,
data
};
if (err != null) {
payload.error = {
name: err.name,
message: err.message,
code: err.code,
data: err.data
};
}
const packet = new Response(this.nodeID, requestID, data, err);
this.logger.debug(`Send response back to '${nodeID}'`);

// Publish the response
return this.publish([TOPIC_RES, nodeID], payload);
return this.publish([TOPIC_RES, nodeID], packet);
}

/**
Expand Down Expand Up @@ -418,7 +444,12 @@ class Transit {
* @memberOf NatsTransporter
*/
publish(topic, message) {
const packet = utils.json2String(message);
let packet;
if (message instanceof Response || message instanceof Request)
packet = message.toString();
else
packet = utils.json2String(message);

//const packet = message;
return this.tx.publish(topic, packet);
}
Expand Down

0 comments on commit b25f591

Please sign in to comment.