feat(lib): support consumer ready timeout (#108)

closes https://github.com/sofastack/sofa-rpc-node/issues/107

1.invoke 若未 ready 时按照 responseTimeout 来计算超时
2.ready 失败的清除数组内的回调函数(除了实例化时的回调)

Co-authored-by: zhangjianye1 <zhangjianye1@joyy.com>
This commit is contained in:
KenyeeCheung 2022-12-05 14:58:06 +08:00 committed by GitHub
parent 73000c20c4
commit 178cb6168b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View File

@ -189,7 +189,11 @@ class RpcConsumer extends Base {
async invoke(method, args, options = {}) {
if (!this._isReady) {
await this.ready();
try {
await this.readyOrTimeout(options.responseTimeout || this.options.responseTimeout)
} catch (err) {
throw new Error('[RpcConsumer] Consumer ready error: ' + err.message)
}
}
const req = this.createRequest(method, args, options);
this.emit('request', req);

View File

@ -45,7 +45,7 @@
"koa-compose": "^4.1.0",
"mz-modules": "^2.1.0",
"pump": "^3.0.0",
"sdk-base": "^3.6.0",
"sdk-base": "^4.0.0",
"sofa-bolt-node": "^2.0.1",
"urlencode": "^1.1.0",
"utility": "^1.16.3",

View File

@ -506,6 +506,31 @@ describe('test/client/consumer.test.js', () => {
assert(rpcContext.req.meta.resultCode === '01');
});
it('should throw ready timeout error when wait more than responseTimeout', async () => {
const consumer = new RpcConsumer({
interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
loadbalancerClass: 'random',
connectionManager,
connectionOpts: {
protocol,
},
registry,
logger,
responseTimeout: 10
});
await consumer.ready()
consumer._isReady = false
consumer.ready(false)
try {
await consumer.invoke('test', [{}])
assert(false);
} catch (err) {
assert(err && err.message.includes('[RpcConsumer] Consumer ready error: Promise timed out after 10 milliseconds'));
}
})
describe('should filter invalid address', () => {
class CustomRegistry extends Base {
constructor() {