first commit

This commit is contained in:
2023-05-19 00:42:48 +08:00
commit 53de9c6c51
243 changed files with 39485 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
'use strict';
function FixedCapacity(capacity) {
this.capacity = Math.max(1, capacity);
}
module.exports = FixedCapacity;
FixedCapacity.prototype.getCapacity = function(callback) {
return callback(null, this.capacity);
};

View File

@@ -0,0 +1,32 @@
'use strict';
var util = require('util');
var debug = require('../../util/debug')('capacity-http-load');
var HttpSimpleCapacity = require('./http-simple');
function HttpLoadCapacity(host, capacityEndpoint) {
HttpSimpleCapacity.call(this, host, capacityEndpoint);
}
util.inherits(HttpLoadCapacity, HttpSimpleCapacity);
module.exports = HttpLoadCapacity;
HttpLoadCapacity.prototype.getCapacity = function(callback) {
this.getResponse(function(err, values) {
var capacity = 1;
if (err) {
return callback(null, capacity);
}
var cores = parseInt(values.cores, 10);
var relativeLoad = parseFloat(values.relative_load);
capacity = Math.max(1, Math.floor(((1 - relativeLoad) * cores) - 1));
capacity = Number.isFinite(capacity) ? capacity : 1;
debug('host=%s, capacity=%s', this.host, capacity);
return callback(null, capacity);
}.bind(this));
};

View File

@@ -0,0 +1,62 @@
'use strict';
var request = require('request');
var debug = require('../../util/debug')('capacity-http-simple');
function HttpSimpleCapacity(host, capacityEndpoint) {
this.host = host;
this.capacityEndpoint = capacityEndpoint;
this.lastResponse = null;
this.lastResponseTime = 0;
}
module.exports = HttpSimpleCapacity;
HttpSimpleCapacity.prototype.getCapacity = function(callback) {
this.getResponse(function(err, values) {
var capacity = 1;
if (err) {
return callback(null, capacity);
}
var availableCores = parseInt(values.available_cores, 10);
capacity = Math.max(availableCores, 1);
capacity = Number.isFinite(capacity) ? capacity : 1;
debug('host=%s, capacity=%s', this.host, capacity);
return callback(null, capacity);
}.bind(this));
};
HttpSimpleCapacity.prototype.getResponse = function(callback) {
var requestParams = {
method: 'POST',
url: this.capacityEndpoint,
timeout: 2000,
json: true
};
debug('getCapacity(%s)', this.host);
// throttle requests for 500 ms
var now = Date.now();
if (this.lastResponse !== null && ((now - this.lastResponseTime) < 500)) {
return callback(null, this.lastResponse);
}
request.post(requestParams, function(err, res, jsonRes) {
if (err) {
return callback(err);
}
if (jsonRes && jsonRes.retcode === 0) {
this.lastResponse = jsonRes.return_values || {};
// We could go more aggressive by updating lastResponseTime on failures.
this.lastResponseTime = now;
return callback(null, this.lastResponse);
}
return callback(new Error('Could not retrieve information from endpoint'));
}.bind(this));
};