mirror of
https://github.com/sofastack/sofa-rpc-node.git
synced 2026-02-01 16:56:23 +00:00
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:
parent
73000c20c4
commit
178cb6168b
@ -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);
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user