Skip to content

Commit

Permalink
add meta to Context
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Apr 11, 2017
1 parent f631912 commit 057bc00
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 104 deletions.
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,39 @@ let broker = new ServiceBroker({
});
```

## Context meta data
Added `meta` prop to `Context`. The `meta` will be merged if has parent context.
In case of remote call the metadata will be transfered to target node.

**Usage**
```js
// Create new context with params & meta
let ctx = new Context({
broker,
action,
params: {
a: 5
},
meta: {
user: "John"
}
})
```

```js
// Broker call with meta data
broker.call("user.create", { name: "Adam", status: true}, {
timeout: 1000,
meta: {
// Send logged in user data with request to the service
loggedInUser: {
userID: 45,
roles: [ "admin" ]
}
}
})
```

# Changes

## Update benchmarkify
Expand All @@ -49,6 +82,14 @@ Bench-bot is a benchmark runner. If a new Pull Request opened, bench-bot will ru
- Remove `wrapContentAction`
- In case of call error, Node will be unavailable, if the error code >= `500`

## Context changes
- Removed `createSubContext`
- Removed `ctx.parent` and added `ctx.parentID`






<a name="0.6.0"></a>
# 0.6.0 (2017-03-31)
Expand Down
2 changes: 1 addition & 1 deletion benchmark/perf-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let [b1, b2] = createBrokers(Transporters.Fake);
let count = 0;
function doRequest() {
count++;
return b2.call("echo.reply", { a: count }).then(res => {
return b1.call("echo.reply", { a: count }).then(res => {
if (count % 10000) {
// Fast cycle
doRequest();
Expand Down
5 changes: 0 additions & 5 deletions benchmark/suites/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ let bench1 = benchmark.createSuite("Context constructor");
metrics: true
});
});

let ctx = new Context();
bench1.add("create subContext", () => {
return ctx.createSubContext(action, params);
});
})();

bench1.run();
Expand Down
48 changes: 15 additions & 33 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"use strict";

//const Promise = require("bluebird");
const _ = require("lodash");
const utils = require("./utils");

/**
Expand All @@ -28,21 +29,27 @@ class Context {
this.broker = opts.broker;
this.action = opts.action;
this.nodeID = opts.nodeID;
this.user = opts.user;
this.parent = opts.parent;
this.parentID = opts.parent ? opts.parent.id : null;

this.metrics = opts.metrics || false;
this.level = opts.parent && opts.parent.level ? opts.parent.level + 1 : 1;
this.level = opts.level || (opts.parent && opts.parent.level ? opts.parent.level + 1 : 1);

this.setParams(opts.params);

if (opts.parent && opts.parent.meta) {
// Merge metadata
this.meta = _.assign({}, opts.parent.meta, opts.meta);
} else {
this.meta = opts.meta || {};
}

// Generate ID for context
if (this.nodeID || opts.metrics)
this.id = utils.generateToken();

// Initialize metrics properties
if (this.metrics) {
this.requestID = opts.requestID;
this.requestID = opts.requestID || (opts.parent && opts.parent.requestID ? opts.parent.requestID : undefined);

this.startTime = null;
this.startHrTime = null;
Expand All @@ -54,31 +61,6 @@ class Context {
this.cachedResult = false;
}

/**
* Create a sub-context from this context
*
* @param {any} action
* @param {any} params
* @returns
*
* @memberOf Context
*/
createSubContext(action, params, nodeID) {
let ContextClass = this.broker ? this.broker.ContextFactory : Context;
let ctx = new ContextClass({
parent: this,
requestID: this.requestID,
broker: this.broker,
action: action || this.action,
nodeID,
user: this.user,
metrics: this.metrics,
params
});

return ctx;
}

/**
* Set params of context
*
Expand Down Expand Up @@ -142,8 +124,8 @@ class Context {
name: this.action.name
};
}
if (this.parent)
payload.parent = this.parent.id;
if (this.parentID)
payload.parent = this.parentID;

this.broker.emit("metrics.trace.span.start", payload);

Expand Down Expand Up @@ -174,8 +156,8 @@ class Context {
name: this.action.name
};
}
if (this.parent)
payload.parent = this.parent.id;
if (this.parentID)
payload.parent = this.parentID;

if (error) {
payload.error = {
Expand Down
18 changes: 11 additions & 7 deletions src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,22 +648,26 @@ class ServiceBroker {

// Create context
let ctx;
let reusedCtx = false;
if (opts.ctx) {
// Reused context
ctx = opts.ctx;
ctx.nodeID = nodeID;
reusedCtx = true;
} else if (opts.parentCtx) {
// Sub context
ctx = opts.parentCtx.createSubContext(action, params, nodeID);
} else {
// New root context
ctx = new this.ContextFactory({ broker: this, action, params, nodeID, requestID: opts.requestID, metrics: this.shouldMetric() });
ctx = new this.ContextFactory({
broker: this,
action,
params,
nodeID,
requestID: opts.requestID,
metrics: this.shouldMetric(),
parent: opts.parentCtx,
meta: opts.meta
});
}

// Add metrics start
if (/*!reusedCtx && */ctx.metrics)
if (ctx.metrics)
ctx._metricStart();

// Call handler or transfer request
Expand Down
15 changes: 11 additions & 4 deletions test/integration/broker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,23 @@ describe("Test local call", () => {
params: {
a: 5,
b: 2
},
meta: {
user: "John",
roles: ["user"]
}
});
let params = { a: 1 };
let meta = {
user: "Jane",
roles: ["admin"]
};

return broker.call("posts.find", params, { parentCtx }).then(ctx => {
return broker.call("posts.find", params, { parentCtx, meta }).then(ctx => {
expect(ctx.params).toBe(params);
expect(ctx.params.a).toBe(1);
expect(ctx.params.b).not.toBeDefined();
expect(ctx.meta).toEqual({ user: "Jane", roles: ["admin"] });
expect(ctx.level).toBe(2);
expect(ctx.parent).toBe(parentCtx);
expect(ctx.parentID).toBe(parentCtx.id);
});

});
Expand Down
82 changes: 33 additions & 49 deletions test/unit/context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Test Context", () => {
expect(ctx.action).not.toBeDefined();
expect(ctx.nodeID).not.toBeDefined();
expect(ctx.user).not.toBeDefined();
expect(ctx.parent).not.toBeDefined();
expect(ctx.parentID).toBeNull();
expect(ctx.level).toBe(1);

expect(ctx.params).toEqual({});
Expand Down Expand Up @@ -51,8 +51,8 @@ describe("Test Context", () => {
b: 5
},
nodeID: "node-1",
user: {
id: 1
meta: {
user: 1
}
};
let ctx = new Context(opts);
Expand All @@ -61,8 +61,8 @@ describe("Test Context", () => {
expect(ctx.broker).toBeDefined();
expect(ctx.action).toBeDefined();
expect(ctx.nodeID).toBeDefined();
expect(ctx.user).toEqual({ id: 1 });
expect(ctx.parent).toBeDefined();
expect(ctx.meta).toEqual({ user: 1 });
expect(ctx.parentID).toBeDefined();
expect(ctx.level).toBe(2);

expect(ctx.params).toEqual({ b: 5 });
Expand All @@ -87,11 +87,14 @@ describe("Test Context", () => {

let opts = {
parent: {
id: "parent123"
id: "parent123",
level: 1,
requestID: "111"
},
broker,
action: {},
requestID: "123",
level: 3,
metrics: true
};
let ctx = new Context(opts);
Expand All @@ -101,6 +104,7 @@ describe("Test Context", () => {

expect(ctx.id).toBeDefined();

expect(ctx.level).toBe(3);
expect(ctx.metrics).toBe(true);
expect(ctx.requestID).toBe("123");

Expand All @@ -111,53 +115,33 @@ describe("Test Context", () => {

expect(ctx.cachedResult).toBe(false);
});
});


describe("Test createSubContext", () => {
it("test with metas", () => {

let broker = new ServiceBroker();
let broker = new ServiceBroker({ metrics: true });

let opts = {
id: 123,
parent: {},
broker,
action: {},
user: { id: 5 },
params: {
b: 5
}
};
let ctx = new Context(opts);

it("test with empty params", () => {
let subCtx = ctx.createSubContext();
expect(subCtx).not.toBe(ctx);
expect(subCtx.id).not.toBeDefined();
expect(subCtx.requestID).toBe(ctx.requestID);
expect(subCtx.parent).toBe(ctx);
expect(subCtx.broker).toBe(ctx.broker);
expect(subCtx.action).toBe(ctx.action);
expect(subCtx.nodeID).toBeUndefined();
expect(subCtx.level).toBe(2);
expect(subCtx.params).toEqual({});
expect(subCtx.user).toEqual({ id: 5 });
});
let opts = {
parent: {
id: "parent123",
meta: {
a: 5,
b: "Hello"
}
},
broker,
action: {},
meta: {
b: "Hi",
c: 100
}
};
let ctx = new Context(opts);

it("test with params", () => {
let action2 = {};
let params2 = { a: 11 };
let node2 = "node-2";

let subCtx = ctx.createSubContext(action2, params2, node2);
expect(subCtx).not.toBe(ctx);
expect(subCtx.id).not.toBe(ctx.id);
expect(subCtx.requestID).toBe(ctx.requestID);
expect(subCtx.parent).toBe(ctx);
expect(subCtx.broker).toBe(ctx.broker);
expect(subCtx.action).toBe(action2);
expect(subCtx.nodeID).toBe(node2);
expect(subCtx.params).toEqual(params2);
expect(ctx.meta).toEqual({
a: 5,
b: "Hi",
c: 100
});
});
});

Expand Down
Loading

0 comments on commit 057bc00

Please sign in to comment.