diff --git a/package.json b/package.json index e1013b7..575db46 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/providers/simple-grpc-client.ts b/src/providers/simple-grpc-client.ts index 65a157d..8897f92 100644 --- a/src/providers/simple-grpc-client.ts +++ b/src/providers/simple-grpc-client.ts @@ -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 './..' @@ -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); @@ -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); }); }