CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
105 lines
3.1 KiB
JavaScript
105 lines
3.1 KiB
JavaScript
const INTERVAL = 2000;
|
|
const BUCKETS = 90;
|
|
|
|
const empty = () => ({
|
|
buckets: [],
|
|
totals: { in: 0, out: 0, requests: 0 },
|
|
pendingRequests: 0,
|
|
lastSent: 0,
|
|
lastReceived: 0,
|
|
session: null,
|
|
});
|
|
|
|
class Stats {
|
|
constructor(registry, quotas, { interval = INTERVAL, buckets = BUCKETS } = {}) {
|
|
this.registry = registry;
|
|
this.quotas = quotas;
|
|
this.interval = interval;
|
|
this.limit = buckets;
|
|
this.series = new Map();
|
|
this.timer = null;
|
|
}
|
|
|
|
start() {
|
|
if (this.timer) return;
|
|
this.timer = setInterval(() => this.sample(), this.interval);
|
|
this.timer.unref();
|
|
}
|
|
|
|
stop() {
|
|
if (this.timer) clearInterval(this.timer);
|
|
this.timer = null;
|
|
}
|
|
|
|
_for(tunnelId) {
|
|
let entry = this.series.get(tunnelId);
|
|
if (!entry) {
|
|
entry = empty();
|
|
this.series.set(tunnelId, entry);
|
|
}
|
|
return entry;
|
|
}
|
|
|
|
recordRequest(tunnelId) {
|
|
this._for(tunnelId).pendingRequests += 1;
|
|
}
|
|
|
|
sample() {
|
|
const now = Date.now();
|
|
for (const tunnel of this.registry.tunnels.values()) {
|
|
const entry = this._for(tunnel.id);
|
|
const session = tunnel.session;
|
|
|
|
let inBytes = 0;
|
|
let outBytes = 0;
|
|
if (session && !session.closed) {
|
|
if (entry.session !== session) {
|
|
entry.session = session;
|
|
entry.lastSent = 0;
|
|
entry.lastReceived = 0;
|
|
}
|
|
inBytes = Math.max(0, session.bytesSent - entry.lastSent);
|
|
outBytes = Math.max(0, session.bytesReceived - entry.lastReceived);
|
|
entry.lastSent = session.bytesSent;
|
|
entry.lastReceived = session.bytesReceived;
|
|
} else {
|
|
entry.session = null;
|
|
}
|
|
|
|
const requests = entry.pendingRequests;
|
|
entry.pendingRequests = 0;
|
|
entry.totals.in += inBytes;
|
|
entry.totals.out += outBytes;
|
|
entry.totals.requests += requests;
|
|
this.quotas.record(tunnel.accountId, inBytes, outBytes, requests);
|
|
|
|
entry.buckets.push({ t: now, in: inBytes, out: outBytes, requests });
|
|
if (entry.buckets.length > this.limit) entry.buckets.splice(0, entry.buckets.length - this.limit);
|
|
}
|
|
}
|
|
|
|
read(tunnelId) {
|
|
const entry = this.series.get(tunnelId);
|
|
const seconds = this.interval / 1000;
|
|
if (!entry) return { interval: seconds, buckets: [], totals: { in: 0, out: 0, requests: 0 }, rates: { in: 0, out: 0 } };
|
|
|
|
const recent = entry.buckets.slice(-5);
|
|
const average = key => recent.length
|
|
? recent.reduce((sum, bucket) => sum + bucket[key], 0) / (recent.length * seconds)
|
|
: 0;
|
|
|
|
return {
|
|
interval: seconds,
|
|
buckets: entry.buckets,
|
|
totals: { ...entry.totals },
|
|
rates: { in: Math.round(average("in")), out: Math.round(average("out")) },
|
|
};
|
|
}
|
|
|
|
forget(tunnelId) {
|
|
this.series.delete(tunnelId);
|
|
}
|
|
}
|
|
|
|
module.exports = { Stats };
|