UNPKG

eosplayer

Version:

eosplayer is the glue layer of eosjs, which is packaged based on eosjs and provides better usability for the application layer. It can be used on browsers already installed scatter or in Dapp wallets.

122 lines 5.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var log_1 = require("../../utils/log"); var log = log_1.createLogger('signPlayer:nodeStat'); var NodeStat = /** @class */ (function () { function NodeStat() { this.error_counts = 0; // 失败次数 this.total_counts = 1; // 总调用次数 this.continuous_failure = 0; // 连续失败次数 this.enabled = true; // 开关状态 this.response_interval = 0; // 响应间隔 this.revival_time = (new Date()).getTime(); // 下次熔断恢复时间 this.cleaning_time = (new Date()).getTime(); // 下次清理状态时间 this.record_total_counts = 0; this.record_total_success = 0; this.record_total_fuse = 0; this.record_total_failed = 0; } return NodeStat; }()); exports.NodeStat = NodeStat; var NodeStatMgr = /** @class */ (function () { function NodeStatMgr(_nodeConfigs, _options) { this._nodeConfigs = _nodeConfigs; this._options = _options; this._nodeStatus = []; this._currentNodeIndex = 0; this.initNodeStatus(); } NodeStatMgr.prototype.initNodeStatus = function () { for (var i = 0; i < this._nodeConfigs.length; i++) { this._nodeStatus.push(new NodeStat()); } // log.info("initiated", this._nodeStatus, this._nodeConfigs) this.setTheBestNodeToCurrent(); }; NodeStatMgr.prototype.setTheBestNodeToCurrent = function () { var min_node_idx = Math.floor(Math.random() * this._nodeStatus.length); var timestamp = (new Date()).getTime(); for (var i = 0; i !== this._nodeStatus.length; i++) { var node = this.tryClean(i); var error_rate = node.error_counts / node.total_counts; var revival = timestamp >= node.revival_time; if (node.enabled && (error_rate >= (this._options.maxFailureRate || 0.5) || node.continuous_failure >= (this._options.maxContinuousFailure || 5))) { node.enabled = false; node.revival_time = timestamp + (this._options.revivalTimeInterval || 600000); node.error_counts = 0; node.total_counts = 1; node.continuous_failure = 0; node.record_total_fuse = (node.record_total_fuse || 0) + 1; log.info("[eos_call_util] fuse " + this._nodeConfigs[i].httpEndpoint); } if (!node.enabled && !revival) { continue; } var min_node = this._nodeStatus[min_node_idx]; var min_error_rate = min_node.error_counts / min_node.total_counts; if (min_node.continuous_failure > node.continuous_failure // 选出节点的连续失败次数更小 || min_error_rate > error_rate + (this._options.failureRateThreshold || 0.1) // 选出节点的失败率更低 || min_node.response_interval > node.response_interval + (this._options.responseIntervalThreshold || 1000) // 选出节点的相应时间权值更快 ) { min_node_idx = i; } } this._currentNodeIndex = min_node_idx; }; NodeStatMgr.prototype.getNodeConf = function (index) { return this._nodeConfigs[index]; }; NodeStatMgr.prototype.getNodeStat = function (index) { return this._nodeStatus[index]; }; NodeStatMgr.prototype.tryClean = function (index) { var node = this.getNodeStat(index); var timestamp = (new Date()).getTime(); if (node.cleaning_time > timestamp) return node; log.verbose('execute clean ', node); node.error_counts = Math.max(0, node.error_counts - 1); node.total_counts = Math.max(1, node.total_counts - 1); node.continuous_failure = Math.max(0, node.continuous_failure - 1); node.response_interval = Math.max(0, node.response_interval - (this._options.responseIntervalDecline || 1000)); node.cleaning_time = timestamp + (this._options.cleaningTimeInterval || 60000); return node; }; NodeStatMgr.prototype.getCurNodeConf = function () { return this.getNodeConf(this._currentNodeIndex); }; NodeStatMgr.prototype.getCurNodeStat = function () { return this.getNodeStat(this._currentNodeIndex); }; NodeStatMgr.prototype.markSendSuccess = function (startTimestamp) { var node = this.getCurNodeStat(); var now = (new Date()).getTime(); var timeDelta = now - startTimestamp; node.response_interval = (node.response_interval + timeDelta) / 2; // 相应时间公式: a[i] = (t[i] + a[i-1]) / 2 node.continuous_failure = 0; node.enabled = true; node.cleaning_time = now + (this._options.cleaningTimeInterval || 180000); node.total_counts += 1; node.record_total_success = (node.record_total_success || 0) + 1; node.record_total_counts = (node.record_total_counts || 0) + 1; log.verbose('send succeed > ', node.continuous_failure, node.error_counts, node.total_counts); return node; }; NodeStatMgr.prototype.markSendFailed = function (startTimestamp) { var node = this.getCurNodeStat(); node.error_counts += 1; node.continuous_failure += 1; node.revival_time = startTimestamp + (this._options.revivalTimeInterval || 180000); node.total_counts += 1; node.record_total_failed = (node.record_total_failed || 0) + 1; node.record_total_counts = (node.record_total_counts || 0) + 1; log.verbose('send failed > ', node.continuous_failure, node.error_counts, node.total_counts); return node; }; return NodeStatMgr; }()); exports.NodeStatMgr = NodeStatMgr; //# sourceMappingURL=nodeStat.js.map