Skip to content

Commit

Permalink
🐛 FIX: ClusterClient use class style
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 22, 2022
1 parent dd4f03c commit e7b229f
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions lib/cluster.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@

const Base = require('sdk-base');
const util = require('util');
const copy = require('copy-to');
const currentIP = require('address').ip();

const RR = 'roundRobin';
const MS = 'masterSlave';

module.exports = function(OssClient) {
function Client(options) {
if (!(this instanceof Client)) {
return new Client(options);
}
class Client extends Base {
constructor(options) {
if (!options || !Array.isArray(options.cluster)) {
throw new Error('require options.cluster to be an array');
}
super(options);

if (!options || !Array.isArray(options.cluster)) {
throw new Error('require options.cluster to be an array');
}
this.clients = [];
this.availables = {};

Base.call(this);
for (let i = 0; i < options.cluster.length; i++) {
const opt = options.cluster[i];
copy(options).pick('timeout', 'agent', 'urllib').to(opt);
this.clients.push(new OssClient(opt));
this.availables[i] = true;
}

this.clients = [];
this.availables = {};
this.schedule = options.schedule || RR;
// only read from master, default is false
this.masterOnly = !!options.masterOnly;
this.index = 0;

for (let i = 0; i < options.cluster.length; i++) {
const opt = options.cluster[i];
copy(options).pick('timeout', 'agent', 'urllib').to(opt);
this.clients.push(new OssClient(opt));
this.availables[i] = true;
const heartbeatInterval = options.heartbeatInterval || 10000;
this._checkAvailableLock = false;
this._timerId = this._deferInterval(this._checkAvailable.bind(this, true), heartbeatInterval);
this._ignoreStatusFile = options.ignoreStatusFile || false;
this._currentIP = require('address').ip();
this._init();
}

this.schedule = options.schedule || RR;
// only read from master, default is false
this.masterOnly = !!options.masterOnly;
this.index = 0;

const heartbeatInterval = options.heartbeatInterval || 10000;
this._checkAvailableLock = false;
this._timerId = this._deferInterval(this._checkAvailable.bind(this, true), heartbeatInterval);
this._ignoreStatusFile = options.ignoreStatusFile || false;
this._init();
}

util.inherits(Client, Base);
const proto = Client.prototype;

const GET_METHODS = [
Expand Down Expand Up @@ -127,7 +121,7 @@ module.exports = function(OssClient) {
};

proto._checkAvailable = async function _checkAvailable(ignoreStatusFile) {
const name = `._ali-oss/check.status.${currentIP}.txt`;
const name = `._ali-oss/check.status.${this._currentIP}.txt`;
if (!ignoreStatusFile) {
// only start will try to write the file
await this.put(name, Buffer.from(`check available started at ${Date()}`));
Expand Down

0 comments on commit e7b229f

Please sign in to comment.