examples/load-balancer/lib/balancer.js
function score(backend, errorBasis) {
    if (typeof errorBasis !== "number" && !errorBasis)
        errorBasis = Date.now();
    const timeSinceError = (errorBasis - backend.lastError);
    const statuses = backend.statuses;
    const timeWeight = (backend.lastError === 0 && 0) ||
        ((timeSinceError < 1000) && 1) ||
        ((timeSinceError < 3000) && 0.8) ||
        ((timeSinceError < 5000) && 0.3) ||
        ((timeSinceError < 10000) && 0.1) ||
        0;
    if (statuses.length == 0)
        return 0;
    let requests = 0;
    let errors = 0;
    for (let i = 0; i < statuses.length; i++) {
        const status = statuses[i];
        if (status && !isNaN(status)) {
            requests += 1;
            if (status >= 500 && status < 600) {
                errors += 1;
            }
        }
    }
    const score = (1 - (timeWeight * (errors / requests)));
    backend.healthScore = score;
    backend.scoredRequestCount = backend.requestCount;
    return score;
}
function score(backend, errorBasis) {
    if (typeof errorBasis !== "number" && !errorBasis)
        errorBasis = Date.now();
    const timeSinceError = (errorBasis - backend.lastError);
    const statuses = backend.statuses;
    const timeWeight = (backend.lastError === 0 && 0) ||
        ((timeSinceError < 1000) && 1) ||
        ((timeSinceError < 3000) && 0.8) ||
        ((timeSinceError < 5000) && 0.3) ||
        ((timeSinceError < 10000) && 0.1) ||
        0;
    if (statuses.length == 0)
        return 0;
    let requests = 0;
    let errors = 0;
    for (let i = 0; i < statuses.length; i++) {
        const status = statuses[i];
        if (status && !isNaN(status)) {
            requests += 1;
            if (status >= 500 && status < 600) {
                errors += 1;
            }
        }
    }
    const score = (1 - (timeWeight * (errors / requests)));
    backend.healthScore = score;
    backend.scoredRequestCount = backend.requestCount;
    return score;
}
compute a backend health score with time + status codes