Skip to content

Commit

Permalink
Adds retryable calls to gRPC client
Browse files Browse the repository at this point in the history
  • Loading branch information
mishok13 committed Oct 2, 2017
1 parent 0ab10a7 commit bf2a211
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
27 changes: 14 additions & 13 deletions package.json
Expand Up @@ -46,32 +46,33 @@
"@types/chai": "^4.0.4",
"@types/express": "^4.0.37",
"@types/http-status": "^0.2.30",
"@types/mocha": "^2.2.42",
"@types/node": "^8.0.26",
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.31",
"@types/reflect-metadata": "0.0.5",
"@types/sinon": "^2.3.3",
"@types/sinon": "^2.3.5",
"@types/sinon-chai": "^2.7.29",
"chai": "^4.1.2",
"coveralls": "^2.13.1",
"mocha": "^3.5.0",
"coveralls": "^2.13.3",
"mocha": "^3.5.3",
"mock-express-request": "^0.2.0",
"nodemon": "^1.12.0",
"nodemon": "^1.12.1",
"nyc": "^11.2.1",
"sinon": "^2.4.1",
"sinon-chai": "^2.13.0",
"source-map-support": "^0.4.17",
"sinon-chai": "^2.14.0",
"source-map-support": "^0.4.18",
"ts-node": "^3.3.0",
"tslint": "^5.7.0",
"typescript": "^2.5.2"
"typescript": "^2.5.3"
},
"dependencies": {
"body-parser": "^1.17.2",
"async": "^2.5.0",
"body-parser": "^1.18.2",
"connect-timeout": "^1.9.0",
"deepmerge": "^1.5.1",
"express": "^4.15.4",
"deepmerge": "^1.5.2",
"express": "^4.16.1",
"grpc": "1.4.1",
"http-status": "^1.0.1",
"http-status-codes": "^1.2.0",
"http-status-codes": "^1.3.0",
"inversify": "^4.3.0",
"minimist": "^1.2.0",
"mysql": "^2.14.1",
Expand Down
13 changes: 9 additions & 4 deletions src/providers/simple-grpc-client.ts
Expand Up @@ -2,6 +2,7 @@ import { injectable, inject } from 'inversify';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Config } from './../config';
import * as grpcExt from 'grpc/src/node/src/grpc_extension';
import * as async from 'async';
const connectivityState = grpcExt.connectivityState;

import { Context, Logger, ProtoConfig, HealthManager } from './..'
Expand Down Expand Up @@ -75,11 +76,9 @@ export class SimpleGrpcClient {
}

const meta = context ? this.transformContext(context) : this.grpc.Metadata();
const now = new Date();
const deadline = now.setSeconds(now.getSeconds() + this.callTimeout);

return new Promise((resolve, reject) => {
method(message, meta, { deadline: deadline }, (error, response) => {
const methodCallback = (error, response) => {
if (error) {
this.logger.error(`Call ${method} on ${this.protoConfig.service} failed with error: `, error);
console.error(error);
Expand All @@ -88,7 +87,13 @@ export class SimpleGrpcClient {
this.logger.debug(`Call ${method} on ${this.protoConfig.service} responded with: `, response);
resolve(response);
}
});
};
const call = (callback) => {
const now = new Date();
const deadline = now.setSeconds(now.getSeconds() + this.callTimeout);
method(message, meta, { deadline: deadline }, callback);
};
async.retry(3, call, methodCallback);
});
}

Expand Down

0 comments on commit bf2a211

Please sign in to comment.