Skip to content

Commit a08897f

Browse files
phasenullclaude
andauthored
Fleet heartbeats carry region: game.ServerRegion, the data center's IATA code (0.5.8) (#17)
Every heartbeat (and the closing notice) sends a top-level `region` when game.ServerRegion, read in a pcall and trimmed, matches ^[A-Z]{3}$; else the field is left out (Studio, an engine without the property). Re-read each heartbeat until valid, then kept. The backend prefers it over IP geolocation. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3fc76cd commit a08897f

7 files changed

Lines changed: 112 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ under "Owners and owner switches" below.
195195
every retry is jittered (+-25%), and after a request's retries all failed the sender backs off (30 s doubling to
196196
300 s); a 401/403/404 holds it off about 5 minutes; one success resets it. One warning line a minute at most, with
197197
the reason, and one "heartbeats work again" line after an outage; `status().fleet` adds `failures` (failed
198-
requests in a row) and `retryIn`.
198+
requests in a row) and `retryIn`. 0.5.8: every heartbeat (and the closing notice) carries `region` at the top
199+
level: `game.ServerRegion`, the data center's IATA airport code (`"IAD"`, `"SYD"`, `"LHR"`; open-ended, not checked
200+
against a list), read in a pcall, trimmed and sent only when it matches `^[A-Z]{3}$` (left out in Studio, before
201+
the engine filled it in, or on an engine without the property); re-read each heartbeat until valid, then kept.
202+
The backend shows it in place of its IP geolocation. `status().fleet.region` has it too.
199203
- **Boot budget:** a new server runs a playable generation within 15 s of start in every path (about 2 s
200204
normally): the boot reads run in parallel behind one 3 s gate, the key asset gets 3 s, and boot attempts get short
201205
load and ready caps; a head that runs out of boot time is retried in the background and swapped to when it works.
@@ -409,7 +413,7 @@ under "Owners and owner switches" below.
409413
- `lune run scripts/test-ed25519.luau` (RFC 8032 vectors, SHA-512/256, and the signing rules in
410414
`scripts/test-signing.luau`), `lune run scripts/test-access.luau`.
411415
- `lune run scripts/test-fleet.luau` (0.3.9: the fleet sender's backoff, hold and log lines on a fake clock; 0.4.2: a
412-
`seq` heartbeat goes before queued reports).
416+
`seq` heartbeat goes before queued reports; 0.5.8: `region` from game.ServerRegion, its checks and re-reads).
413417
- `lune run scripts/test-errors.luau` (0.4.0: templating, fingerprints, per-minute counts, limits, batches, the
414418
sender, client batches, the dedup), `lune run scripts/test-budget.luau` (0.4.0: proxies, limits, the sliding minute,
415419
callers, the summary; 0.4.2: TPS windows, the slowest second, physics FPS, the Lua heap fallback).

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@typetorch/kernel",
3-
"version": "0.5.7",
3+
"version": "0.5.8",
44
"description": "TypeTorch kernel: the small Luau loader baked into the place (boot, branches, hot swaps, stable remotes, /tt).",
55
"license": "MIT",
66
"repository": {

‎scripts/smoke-kernel.luau‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ local GAME_ROOT_PROPS = {
794794
JobId = "smoke-job-0001",
795795
PlaceId = 42,
796796
PlaceVersion = 7,
797+
ServerRegion = " IAD", -- 0.5.8: the heartbeat's `region` (trimmed)
797798
PrivateServerId = "",
798799
PrivateServerOwnerId = 0,
799800
CreatorType = Enum.CreatorType.User,
@@ -4126,6 +4127,10 @@ if MODES38.errors then
41264127
local body = list[#list].body
41274128
return body.bu ~= nil and body.bu.ds.lr == 60 + 40 * #fakePlayers and body.bu.h.l == 500 and body.bu.by.k ~= nil
41284129
end))
4130+
check("errors: heartbeats carry region (0.5.8: game.ServerRegion, trimmed) at the top level", (function()
4131+
local list = posts("/v1/fleet/heartbeat")
4132+
return #list > 0 and list[#list].body.region == "IAD" and status().fleet.region == "IAD"
4133+
end)())
41294134
local rider = joinPlayer(424242424, "Rider", nil, false)
41304135
rider.DisplayName = "Speedy"
41314136
services.Players.PlayerAdded:Fire(rider)

‎scripts/test-fleet.luau‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,62 @@ Fleet.configure({
323323
Fleet.refreshSettings()
324324
check("a non-https backend is refused (no target)", Fleet.info().settings == "invalid" and Fleet.target() == nil and said("settings.backend) ignored") == 1)
325325

326+
-- 0.5.8: `region` (game.ServerRegion): only a trimmed uppercase 3-letter code, read in a pcall, re-read until valid.
327+
do
328+
check("region: IAD kept", Fleet.regionCode("IAD") == "IAD")
329+
check("region: trimmed", Fleet.regionCode(" SYD\n") == "SYD")
330+
check("region: empty / lowercase / wrong length / digits / non-strings refused", Fleet.regionCode("") == nil
331+
and Fleet.regionCode("iad") == nil and Fleet.regionCode("IA") == nil and Fleet.regionCode("IADX") == nil
332+
and Fleet.regionCode("I4D") == nil and Fleet.regionCode(nil) == nil and Fleet.regionCode(7) == nil
333+
and Fleet.regionCode("I AD") == nil)
334+
Fleet.configure({
335+
readSettings = function()
336+
return true, { url = "https://fleet.example.test", token = TOKEN }, "fleet"
337+
end,
338+
})
339+
Fleet.refreshSettings()
340+
answer = "ok"
341+
advance(40)
342+
local raw: any = nil
343+
local reads = 0
344+
local function lastHeartbeat()
345+
local beat = nil
346+
for index = #requests, 1, -1 do
347+
if requests[index].path == "/v1/fleet/heartbeat" then
348+
beat = serde.decode("json", requests[index].body)
349+
break
350+
end
351+
end
352+
return beat
353+
end
354+
local function beatNow()
355+
advance(40)
356+
Fleet.heartbeat()
357+
advance(1)
358+
return lastHeartbeat()
359+
end
360+
Fleet.configure({ serverRegion = function()
361+
reads += 1
362+
if raw == "error" then
363+
error("ServerRegion is not a valid member of DataModel")
364+
end
365+
return raw
366+
end })
367+
raw = "error"
368+
local beat = beatNow()
369+
check("region: a missing property (error) leaves the field out, the heartbeat still goes", beat ~= nil and beat.region == nil and beat.j == "job-1")
370+
raw = ""
371+
beat = beatNow()
372+
check("region: \"\" (Studio, not filled in yet) leaves the field out", beat ~= nil and beat.region == nil)
373+
raw = " LHR "
374+
beat = beatNow()
375+
check("region: filled in later: the next heartbeat carries it at the top level", beat ~= nil and beat.region == "LHR" and Fleet.info().region == "LHR")
376+
local readsBefore = reads
377+
raw = "error"
378+
beat = beatNow()
379+
check("region: kept once valid (not read again)", beat ~= nil and beat.region == "LHR" and reads == readsBefore)
380+
end
381+
326382
-- 0.5.7: the health watch (Fleet.healthWatch): no false critical health_failed during a swap or a slow boot.
327383
do
328384
local function run(ticks: { { string | boolean } }, need: number?)

‎src/server/Fleet.luau‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
--
88
-- Requests (HttpService:RequestAsync, POST, JSON, `Authorization: Bearer <token>`); contract in plans/01 "Fleet API":
99
-- /v1/fleet/heartbeat the fleet status { t, b, c?, a?, n, m, s, u, p, pv, x?, xh?, xd?, cv?, v, q, g, h, e?, sv = 2 } plus j = JobId
10+
-- and region? (0.5.8, game.ServerRegion)
1011
-- (never `k`, the reserved access code), every FLEET_HEARTBEAT_SECONDS and right after a change
1112
-- /v1/fleet/report each deploy report { s, b, a, j, r, e?, d?, t, g, k, p }, at once
1213
-- /v1/fleet/alert { level = "critical" | "warning", code, message (<= 300 bytes), j, b, a, s, t, g, k }, at once
@@ -38,6 +39,12 @@
3839
-- the first after a change, and the next one after a reply asked (`xq = 1`). Fleet.heartbeat("columns") (the game's
3940
-- values changed, the declarations changed, or `xq`) is coalesced: at most one per FLEET_COLUMNS_GAP s. The closing
4041
-- request carries none of them.
42+
-- 0.5.8: every heartbeat (and the closing request) carries `region` at the top level: the Roblox data center this
43+
-- server runs in, game.ServerRegion (an uppercase 3-letter IATA airport code such as "IAD", "SYD", "LHR"; open-ended,
44+
-- so not checked against a list). Read through deps.serverRegion in a pcall (an engine without the property must not
45+
-- break a heartbeat), trimmed, sent only when it matches ^[A-Z]{3}$ (else the field is left out: "" in Studio, before
46+
-- the engine filled it in, or on an older engine). Re-read on every heartbeat until a valid code was seen, then kept.
47+
-- The backend prefers it over its IP geolocation of the heartbeat.
4148

4249
local Constants = require(game:GetService("ReplicatedStorage"):WaitForChild("TypeTorchKernelShared"):WaitForChild("Constants"))
4350

@@ -70,6 +77,10 @@ local deps = {
7077
columnValues = function()
7178
return nil
7279
end,
80+
-- 0.5.8: () -> any: game.ServerRegion (may error: an older engine without the property).
81+
serverRegion = function()
82+
return nil
83+
end,
7384
encode = nil, -- (table) -> string
7485
decode = nil, -- (string) -> any
7586
warn = warn,
@@ -91,6 +102,7 @@ local heartbeat = { due = false, urgent = false, attempts = 0, nextAt = 0, lastA
91102
local declarationsSent: string? = nil
92103
local declarationsAsked = false
93104
local started = false
105+
local regionCode: string? = nil -- 0.5.8: the first valid game.ServerRegion read (it never changes for a server)
94106
local closed = false
95107
local tokens, tokensAt = 10, os.clock()
96108
local stats = { sent = 0, failed = 0, dropped = 0, lastOkAt = nil, lastError = nil, lastErrorAt = nil, lastStatus = nil, alerts = 0 }
@@ -333,13 +345,37 @@ local function post(path: string, body): (boolean, boolean, number?, string?, st
333345
return false, retry, if typeof(code) == "number" then code else nil, stats.lastError
334346
end
335347

348+
-- 0.5.8: a game.ServerRegion value as the heartbeat's `region`: trimmed, and only an uppercase 3-letter code; else nil.
349+
function Fleet.regionCode(raw: any): string?
350+
if typeof(raw) ~= "string" or #raw > 64 then
351+
return nil
352+
end
353+
local trimmed = string.match(raw, "^%s*(.-)%s*$")
354+
if trimmed and string.match(trimmed, "^[A-Z][A-Z][A-Z]$") then
355+
return trimmed
356+
end
357+
return nil
358+
end
359+
360+
-- 0.5.8: this server's region code, or nil (read again next time until one is valid).
361+
local function serverRegion(): string?
362+
if regionCode == nil then
363+
local ok, raw = pcall(deps.serverRegion)
364+
if ok then
365+
regionCode = Fleet.regionCode(raw)
366+
end
367+
end
368+
return regionCode
369+
end
370+
336371
-- The heartbeat body: the fleet status without `k` (a reserved server's access code stays in the universe), plus j.
337372
-- 0.5.7: plus the live debug fields (xh, xd?, cv?) unless `closing`.
338373
local function heartbeatBody(closing: boolean?)
339374
local ok, status = pcall(deps.status)
340375
local body = if ok and typeof(status) == "table" then table.clone(status) else { sv = 2 }
341376
body.k = nil
342377
body.j = deps.jobId
378+
body.region = serverRegion() -- 0.5.8: nil (left out) until game.ServerRegion holds a valid code
343379
-- 0.4.0: the budget summary (`bu`) and anything else the kernel adds; only on what goes to the backend (the roll
344380
-- call and the analytics engine use api:fleetStatus() without it).
345381
local extraOk, extra = pcall(deps.heartbeatExtra)
@@ -639,6 +675,8 @@ function Fleet.info()
639675
source = settingsSource,
640676
-- 0.5.7: the declarations' hash the backend acknowledged (nil: none declared, or not sent yet).
641677
declarationsSent = declarationsSent,
678+
-- 0.5.8: the `region` heartbeats carry (game.ServerRegion), nil while unknown.
679+
region = regionCode,
642680
}
643681
end
644682

‎src/server/Kernel.server.luau‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,11 @@ Fleet.configure({
14811481
heartbeatExtra = function()
14821482
return { bu = Budget.summary(), pf = Budget.perfWindow() }
14831483
end,
1484+
-- 0.5.8: the data center's IATA code ("IAD"; "" in Studio). Fleet.luau reads it in a pcall (older engines lack it)
1485+
-- and sends it as the heartbeat's `region`.
1486+
serverRegion = function()
1487+
return (game :: any).ServerRegion
1488+
end,
14841489
encode = function(value)
14851490
return HttpService:JSONEncode(value)
14861491
end,

‎src/shared/Constants.luau‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ return {
77
-- The single version source: package.json "version" must match (typetorch kernel deploy checks). The build (git
88
-- commit) and content hash are stamped by kernel deploy as attributes on ServerScriptService.TypeTorchKernel
99
-- (KernelCommit, KernelHash); see status().kernelBuild.
10-
KERNEL_VERSION = "0.5.7",
10+
KERNEL_VERSION = "0.5.8",
1111

1212
-- The kernel's DataStore: durable heads, deployments, private records, events, and (0.3.8) the signed settings.
1313
DATASTORE = "TypeTorch",

0 commit comments

Comments
 (0)