pandora-metrics
Version:
## Overview
34 lines • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const MetricSet_1 = require("../common/MetricSet");
const Mutex_1 = require("../util/Mutex");
class CachedMetricSet extends MetricSet_1.MetricSet {
constructor(dataTTL) {
super();
this.mutex = new Mutex_1.Mutex();
this.dataTTL = dataTTL || CachedMetricSet.DEFAULT_DATA_TTL;
}
/**
* Do not collect data if our cached copy of data is valid.
* The purpose is to minimize the cost to collect system metric.
*/
async refreshIfNecessary() {
let current = Date.now();
if (!this.lastCollectTime || current - this.lastCollectTime > this.dataTTL * 1000) {
if (this.mutex.tryLock(3000)) {
await this.getValueInternal();
this.mutex.unlock();
}
else {
await new Promise((resolve) => {
this.mutex.wait(resolve);
});
}
// update the last collect time stamp
this.lastCollectTime = current;
}
}
}
CachedMetricSet.DEFAULT_DATA_TTL = 60;
exports.CachedMetricSet = CachedMetricSet;
//# sourceMappingURL=CachedMetricSet.js.map