Skip to content

Commit

Permalink
fix: remove singleton instance after consumer close (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored Mar 6, 2019
1 parent 5081e81 commit 3c97e0e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ class RpcClient extends Base {
debug('create consumer for %s', key);
consumerClass = consumerClass || this.consumerClass;
consumer = new consumerClass(options);
this._consumerCache.set(key, consumer);
// delegate consumer's error to client
consumer.on('error', err => { this.emit('error', err); });
consumer.on('request', req => { this.emit('request', req); });
consumer.on('response', info => { this.emit('response', info); });
this._consumerCache.set(key, consumer);
consumer.once('close', () => {
this._consumerCache.delete(key);
});
}
return consumer;
}
Expand Down
1 change: 1 addition & 0 deletions lib/client/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class RpcConsumer extends Base {
}
this.removeAllListeners('request');
this.removeAllListeners('response');
this.emit('close');
}
}

Expand Down
13 changes: 11 additions & 2 deletions test/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('test/client/client.test.js', () => {
});
client.consumerClass = RpcConsumer;

const consumer = client.createConsumer({
let consumer = client.createConsumer({
interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
targetAppName: 'pb',
loadbalancerClass: 'consistentHash',
Expand All @@ -58,12 +58,21 @@ describe('test/client/client.test.js', () => {
group: 'A',
}];
const ctx = { foo: 'bar' };
const res = await consumer.invoke('echoObj', args, { ctx });
let res = await consumer.invoke('echoObj', args, { ctx });
assert.deepEqual(res, { code: 200, message: 'hello Peter, you are in A' });

assert(req && req.targetAppName === 'pb');
assert(req.ctx === ctx);

consumer.close();

consumer = client.createConsumer({
interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
targetAppName: 'pb',
});
res = await consumer.invoke('echoObj', args, { ctx });
assert.deepEqual(res, { code: 200, message: 'hello Peter, you are in A' });

await client.close();
});

Expand Down

0 comments on commit 3c97e0e

Please sign in to comment.