Skip to content

Commit

Permalink
feat: createConsumer support cache control (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
mansonchor.github.com authored Mar 12, 2020
1 parent fad88e9 commit 6c16373
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ class RpcClient extends Base {
options = Object.assign({
middlewares: this._middlewares,
connectionManager: this.connectionManager,
cache: true,
}, this.options, options);
const key = this.formatKey(options);
let consumer = this._consumerCache.get(key);
if (!consumer) {
if (!options.cache || !consumer) {
debug('create consumer for %s', key);
consumerClass = consumerClass || this.consumerClass;
consumer = new consumerClass(options);
Expand Down
43 changes: 43 additions & 0 deletions test/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,47 @@ describe('test/client/client.test.js', () => {

await client.close();
});

it('should createConsumer no cache', async function() {
const client = new RpcClient({
registry,
protocol,
logger,
});
client.consumerClass = RpcConsumer;

const options = {
interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
targetAppName: 'pb',
};

const consumer1 = client.createConsumer(options);
await consumer1.ready();

const consumer2 = client.createConsumer(options);
await consumer2.ready();

const consumer3 = client.createConsumer(Object.assign({}, options, { cache: false }));
await consumer3.ready();

// consumer1 close,in fact consumer2 will close too
consumer1.close();

// consumer3 will not effect because createConsumer with cache: false
const args = [{
name: 'Peter',
group: 'A',
}];
const ctx = { foo: 'bar' };
const res = await consumer3.invoke('echoObj', args, { ctx });
assert.deepEqual(res, { code: 200, message: 'hello Peter, you are in A' });


try {
await consumer2.invoke('echoObj', args, { ctx });
assert(false);
} catch (err) {
assert(err.message === 'No provider of com.alipay.sofa.rpc.test.ProtoService:1.0@SOFA:echoObj() found!');
}
});
});

0 comments on commit 6c16373

Please sign in to comment.