Skip to content

Commit 6357340

Browse files
fengmk2PeterRao
authored andcommitted
feat: only read from master (ali-sdk#623)
set options.masterOnly = true to enable the feature pick from ali-sdk#622
1 parent 655ee3d commit 6357340

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/cluster.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ module.exports = function (OssClient) {
3131
}
3232

3333
this.schedule = options.schedule || RR;
34+
// only read from master, default is false
35+
this.masterOnly = !!options.masterOnly;
3436
this.index = 0;
3537

3638
const heartbeatInterval = options.heartbeatInterval || 10000;
@@ -180,6 +182,10 @@ module.exports = function (OssClient) {
180182

181183
proto.chooseAvailable = function chooseAvailable() {
182184
if (this.schedule === MS) {
185+
// only read from master
186+
if (this.masterOnly) {
187+
return this.clients[0];
188+
}
183189
for (let i = 0; i < this.clients.length; i++) {
184190
if (this.availables[i]) {
185191
return this.clients[i];

test/node/cluster.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ describe('test/cluster.test.js', () => {
230230
this.store.removeListener('error', onerror);
231231
});
232232

233+
it('should MS always get from clients[0] when masterOnly === true', async function () {
234+
mm(this.store, 'schedule', 'masterSlave');
235+
mm(this.store, 'masterOnly', true);
236+
mm(this.store.clients[1], 'get', 'mock error');
237+
function onerror() {
238+
throw new Error('should not emit error event');
239+
}
240+
this.store.on('error', onerror);
241+
242+
let res = await this.store.get(this.name);
243+
res.res.status.should.equal(200);
244+
res = await this.store.get(this.name);
245+
res.res.status.should.equal(200);
246+
247+
this.store.removeListener('error', onerror);
248+
});
249+
233250
it('should get from clients[0] when clients[0] response 4xx ok', async function () {
234251
mm(this.store, 'schedule', 'masterSlave');
235252
mm.error(this.store.clients[0], 'get', 'mock error', { status: 403 });

0 commit comments

Comments
 (0)