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

[COR-68] Add registry of loaded RPC clients and expose discovery metadata #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- '6'
- lts/*
- "10"
- lts/*
deploy:
provider: npm
email: [email protected]
Expand Down
68 changes: 46 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ const IPC_MANIFESTS_FOLDER = "ipc_manifests";
const requestDuration = new Histogram({
name: "http_rpc_client_request_duration_seconds",
help: "Duration of rpc requests from the client",
labelNames: ["service", "method"]
labelNames: ["service", "method"],
});
const requestCount = new Counter({
name: "http_rpc_client_requests_total",
help: "The total number of rpc requests from the client",
labelNames: ["service", "method"]
labelNames: ["service", "method"],
});
const failureCount = new Counter({
name: "http_rpc_client_failures_total",
help: "The total number of rpc failures from the client",
labelNames: ["service", "method", "type", "status_code"]
labelNames: ["service", "method", "type", "status_code"],
});

class RpcResponseError {
Expand All @@ -31,7 +31,7 @@ class RpcResponseError {
configurable: true,
enumerable: false,
value: this.constructor.name,
writable: true
writable: true,
});

Object.assign(this, responseBody);
Expand All @@ -46,9 +46,9 @@ class RpcResponseError {
"\n" +
[...this.source]
.reverse()
.map(s => " via " + s)
.map((s) => " via " + s)
.join("\n"),
writable: true
writable: true,
});
}

Expand All @@ -61,12 +61,36 @@ class RpcResponseError {
}
}

exports.load = function(host, serviceName, options) {
const metaPath = getMetaPath(serviceName);
const client = new Client(host, options);
class Registry {
constructor() {
this._registeredServices = {};
}

load(host, serviceName, options) {
const metaPath = getMetaPath(serviceName);
const client = new Client(host, options);

const loadClient = client.load(metaPath);
this._registeredServices[serviceName] = loadClient;

return loadClient;
}

createWellKnownHandler() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think createWellKnownErrorHandler

return (req, res) => {
const services = Object.keys(this._registeredServices).map((s) => {
return { name: s };
});

res.json({
services,
});
};
}
}

return client.load(metaPath);
};
exports.registry = new Registry();
exports.Registry = Registry;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the other PR, this should export WELL_KNOWN_META_PATH which will be /client in this case, not /server


// exports.createClient = function (host, options) {
// return new Client(host, options);
Expand Down Expand Up @@ -100,7 +124,7 @@ class Client {
Object.assign(
this,
{
path: "/rpc"
path: "/rpc",
},
options
);
Expand Down Expand Up @@ -128,21 +152,21 @@ class Client {
.post(requestUrl, {
body: JSON.stringify(params),
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
},
json: true,
retries: 0,
timeout
timeout,
})
.then(res => res.body)
.catch(err => {
.then((res) => res.body)
.catch((err) => {
failureCount.inc(
Object.assign(
{
type:
(err.response && err.response.body && err.response.body.type) ||
undefined,
status_code: err.statusCode
status_code: err.statusCode,
},
requestMeta
)
Expand All @@ -160,16 +184,16 @@ class Client {
protocol: this.protocol,
host: this.host,
port: this.port,
pathname: pathname
pathname: pathname,
});
}

getMeta() {
const requestUrl = this.formatUrl();

return got(requestUrl, { json: true })
.then(res => res.body)
.catch(err => mapError(this.serviceName, "getMeta", err));
.then((res) => res.body)
.catch((err) => mapError(this.serviceName, "getMeta", err));
}

createInterface(meta) {
Expand All @@ -179,8 +203,8 @@ class Client {
const multiArg = meta.multiArg || false;
const self = this;

meta.interfaces.forEach(iface => {
rpcInterface[iface.methodName] = function() {
meta.interfaces.forEach((iface) => {
rpcInterface[iface.methodName] = function () {
const args = Array.prototype.slice.call(arguments);
const params = multiArg ? args : args[0];
if (!multiArg && params && typeof params !== "object")
Expand Down
Loading