Skip to content

Commit

Permalink
fly: rmv machines-heartbeat specific code
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Jun 13, 2023
1 parent 09d9a02 commit 964abc1
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 73 deletions.
5 changes: 0 additions & 5 deletions fly.tls.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ kill_timeout = "15s"
NODE_ENV = "production"
CLOUD_PLATFORM = "fly"
LOG_LEVEL = "info"
# fly can't start vms that scale down super quickly
# community.fly.io/t/6091/12
# node proc shuts itself after this threshold
# MACHINES_TIMEOUT_SEC = "100"
# for now, using auto_start_machines / auto_stop_machines

[experimental]
auto_rollback = true
Expand Down
5 changes: 0 additions & 5 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ kill_timeout = "15s"
DENO_ENV = "production"
LOG_LEVEL = "info"
NODE_ENV = "production"
# fly can't start vms that scale down super quickly
# community.fly.io/t/6091/12
# node proc shuts itself after this threshold
# MACHINES_TIMEOUT_SEC = "100"
# for now, using auto_start_machines / auto_stop_machines

# DNS over HTTPS
[[services]]
Expand Down
14 changes: 0 additions & 14 deletions src/commons/envutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@ export function onFly() {
return envManager.get("CLOUD_PLATFORM") === "fly";
}

export function machinesTimeoutMillis() {
if (!envManager) return -1;

return envManager.get("MACHINES_TIMEOUT_SEC") * 1000;
}

// only valid on Fly Machines
export function httpCheck() {
if (!envManager) return false;
if (!onFly()) return false;

return envManager.get("MACHINES_TIMEOUT_SEC") > 0;
}

export function onDenoDeploy() {
if (!envManager) return false;

Expand Down
5 changes: 0 additions & 5 deletions src/core/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ const defaults = new Map(
// also ref: EnvManager.mostLikelyCloudPlatform()
default: "local",
},
// timeout for proc running on fly machines
MACHINES_TIMEOUT_SEC: {
type: "number",
default: -1,
},
// download blocklist files to disk, if any, and quit
BLOCKLIST_DOWNLOAD_ONLY: {
type: "boolean",
Expand Down
26 changes: 1 addition & 25 deletions src/core/svc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import * as util from "../commons/util.js";
// proc up since
let readytime = 0;
let endtimer = null;
// unix timestamp of the latest recorded heartbeat
let latestHeartbeat = 0;

export const services = {
/** @type {Boolean} ready */
Expand Down Expand Up @@ -97,28 +95,6 @@ export function stopAfter(ms = 0) {
} else {
log.d("stopAfter", ms);
}
const now = Date.now();
// 30% of the upcoming wait-time
const p30 = (ms * 0.3) | 0;
const when = now - latestHeartbeat;
// was the previous heartbeat recent enough?
const recent = when <= p30;
// is the incoming wait (ms) 2x the pending wait?
const pending = when - ms;
const nearer = pending > 2 * ms;
// if pending wait not too high and the prev heartbeat too recent
if (!nearer && recent) {
log.d("skip heartbeat; prev heartbeat was", when, "ms ago; lt", p30);
return;
}
clearEndTimer();
if (!util.emptyObj(endtimer)) clearTimeout(endtimer);
endtimer = util.timeout(ms, stopProc);
log.d("n?", nearer, "r?", recent, "pending", pending, "extend ttl", ms);
latestHeartbeat = now;
}

function clearEndTimer() {
if (util.emptyObj(endtimer)) return false;
clearTimeout(endtimer);
return true;
}
28 changes: 9 additions & 19 deletions src/server-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,14 @@ function systemUp() {
});
}

if (envutil.httpCheck()) {
const portcheck = envutil.httpCheckPort();
const hcheck = h2c.createServer(serve200).listen(portcheck, () => {
up("http-check", hcheck.address());
trapServerEvents(hcheck);
});
}
const portcheck = envutil.httpCheckPort();
const hcheck = h2c.createServer(serve200).listen(portcheck, () => {
up("http-check", hcheck.address());
trapServerEvents(hcheck);
});

// if (envutil.measureHeap()) heapdiff = new memwatch.HeapDiff();
machinesHeartbeat();
heartbeat();
}

/**
Expand Down Expand Up @@ -829,7 +827,7 @@ function handleTCPData(socket, chunk, sb, host, flag) {
* @param {String} flag
*/
async function handleTCPQuery(q, socket, host, flag) {
machinesHeartbeat();
heartbeat();

let ok = true;
if (bufutil.emptyBuf(q) || !tcpOkay(socket)) return;
Expand Down Expand Up @@ -967,7 +965,7 @@ async function serveHTTPS(req, res) {
* @param {Http2ServerResponse} res
*/
async function handleHTTPRequest(b, req, res) {
machinesHeartbeat();
heartbeat();

const rxid = util.xid();
const t = log.startTime("handle-http-req-" + rxid);
Expand Down Expand Up @@ -1050,7 +1048,7 @@ function trapRequestResponseEvents(req, res) {
});
}

function machinesHeartbeat() {
function heartbeat() {
const maxc = envutil.maxconns();
const minc = envutil.minconns();
const measureHeap = envutil.measureHeap();
Expand All @@ -1070,14 +1068,6 @@ function machinesHeartbeat() {
if (stats.noreqs % (minc * 2) === 0) {
log.i(stats.str(), "in", (uptime() / 60000) | 0, "mins");
}

// nothing to do, if not on fly
if (!envutil.onFly()) return;
// if a fly machine app, figure out ttl
const t = envutil.machinesTimeoutMillis();
log.d("mach: extend-machines-ttl by", t);
if (t >= 0) stopAfter(t);
// else: not on machines
}

function adjustMaxConns(n) {
Expand Down

0 comments on commit 964abc1

Please sign in to comment.