Skip to content

Commit b3bca0c

Browse files
authored
Merge pull request #18 from typetorch/feat/reserve-server
api:reserveServer: the game's own reserved servers boot this server's branch (0.5.9)
2 parents a08897f + c00abab commit b3bca0c

6 files changed

Lines changed: 93 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use `:` (roblox-ts calls interface methods that way). Details of each are in the
6969
`sendUnreliable`, `broadcastUnreliable`, `status`, `logs`, `branches`, `reload`, `rollback`, `switchBranch`,
7070
`artifacts`, `pinArtifact`, `newServer`, `pinned`, `onPending`, `onDevChanged`, `requestReload`, `experiment`,
7171
`unpin`, `keys`, `reportError`, `onClose`, `fleetStatus`, `onDeployReport`, `deployReports`, `accessCode`,
72+
`reserveServer`,
7273
`settings`, `settingsStatus`, `onSettingsChanged`, `messagingSubscribe`, `messagingPublish`, `messagingStatus`,
7374
`onRollCall`, `runDetached`, `setAnalyticsId`, `budgetCount`, `budget`, `onRemoteDebug`, `debugDeclare`,
7475
`onDebugCommand`, `onDebugColumn`, `onDebugStream`, `debugColumnsChanged`.
@@ -403,6 +404,18 @@ under "Owners and owner switches" below.
403404
`<key>`. A 404 (an older backend) stops bundles for the server's lifetime; no answer, 429 and 5xx are retried.
404405
`status().bundles` has the counters (never the URL or the key). Studio and servers without backend settings send
405406
nothing.
407+
- The game's own reserved servers (0.5.9): `api:reserveServer(options?)` reserves a server
408+
(`TeleportService:ReserveServer`) and records it in the private branch registry (`private/<PrivateServerId>`, with
409+
the access code, no pin, `setBy` 0) on THIS server's branch, so it boots this branch's head the way a `newServer`
410+
server does, instead of the universe's default branch (a game-made reserved server has no record otherwise, so a
411+
dev branch's players would land on the prod build). It does NOT teleport: the game teleports with its own
412+
`TeleportOptions` (`ReservedServerAccessCode`, `SetTeleportData`). No dev check (it is the game's own server code),
413+
but never another branch: prod game code must not be able to start dev builds, so `options` is ignored for now. At
414+
most `RESERVE_PER_MINUTE` (20) calls per sliding minute per server (each is a ReserveServer and a DataStore
415+
UpdateAsync). Yields. Returns `{ ok, accessCode?, privateServerId?, branch?, error? }`, `error` = `"studio"`,
416+
`"rate limited"`, `"no branch"`, `"no artifact on branch <b>"`, `"ReserveServer: ..."` or `"could not save the
417+
branch: ..."` (the code is never handed out without its record). The access code is server-only data. Its presence
418+
marks a 0.5.9 kernel (the framework's `TypeTorch.reserveServer`).
406419

407420
## Development
408421

‎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.8",
3+
"version": "0.5.9",
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,6 +2597,8 @@ if STUDIO_MODE then
25972597
local first = status().generation.name
25982598
local reloaded = SMOKE.kernel:reload(owner)
25992599
check("studio: Reload remounts a fresh clone", reloaded.ok == true and isLocalId(running()) and status().generation.name ~= first and #SMOKE.boots == 2 and #SMOKE.stops == 1 and localSource.Parent == devFolder)
2600+
local studioReserve = SMOKE.kernel:reserveServer()
2601+
check("studio: reserveServer refuses (error \"studio\"), nothing reserved", studioReserve.ok == false and studioReserve.error == "studio" and #reservedServers == 0)
26002602
local sneaky = newInstance("Script", "Sneaky")
26012603
sneaky.Parent = localSource.Shared
26022604
local before = status().generation.name
@@ -3192,6 +3194,29 @@ if HEALTH_MODE then
31923194
return clients.ok == 1 and clients.failed == 0 and clients.behind == 0
31933195
end))
31943196

3197+
-- 0.5.9: api:reserveServer(): a reserved server recorded on this server's branch, no teleport, a per-minute cap.
3198+
local teleportsBefore, reservedBefore = #teleports, #reservedServers
3199+
local reserved = SMOKE.kernel:reserveServer()
3200+
local n = reservedBefore + 1
3201+
check(
3202+
"reserveServer: ok, the access code and id, this server's branch (dev), no teleport",
3203+
reserved.ok == true and reserved.accessCode == `code-{n}` and reserved.privateServerId == `ps-new-{n}` and reserved.branch == "dev" and reserved.error == nil and #teleports == teleportsBefore
3204+
)
3205+
check("reserveServer: the private record says branch dev, no pin, with the code (it boots the dev head)", (function()
3206+
local record = dataStore.data[`private/ps-new-{n}`]
3207+
return record ~= nil and record.branch == "dev" and record.code == `code-{n}` and record.pin == nil and record.setBy == 0
3208+
end)())
3209+
local other = SMOKE.kernel:reserveServer({ branch = "main" })
3210+
check("reserveServer: a branch option is ignored (always this server's branch)", other.ok == true and other.branch == "dev" and dataStore.data[`private/{other.privateServerId}`].branch == "dev")
3211+
local last
3212+
for _ = 3, Constants.RESERVE_PER_MINUTE + 1 do
3213+
last = SMOKE.kernel:reserveServer()
3214+
end
3215+
check(
3216+
`reserveServer: at most {Constants.RESERVE_PER_MINUTE} a minute (then "rate limited", nothing reserved)`,
3217+
last.ok == false and last.error == "rate limited" and last.accessCode == nil and #reservedServers == reservedBefore + Constants.RESERVE_PER_MINUTE
3218+
)
3219+
31953220
-- onClose: the running generation's handler runs from BindToClose; the heartbeat entry goes; no swaps after.
31963221
local closeDone = false
31973222
task.spawn(function()

‎src/server/Api.luau‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ local entries: { [Script]: any } = {}
1111
-- 0.3.8: detached jobs (api:runDetached): counters and the running ones ({ generation name, started }), server-wide.
1212
local detached = { running = 0, started = 0, finished = 0, failed = 0, slow = 0, nextId = 0, jobs = {}, lastError = nil }
1313
local services = nil -- set by the kernel: { Access, Transport, Logs, status, branchList, artifacts, reload, rollback,
14-
-- switchBranch, pinArtifact, newServer, requestReload, unpin, experiment, KERNEL_BUILD, keys, reportError, fleetStatus,
14+
-- switchBranch, pinArtifact, newServer, reserveServer (0.5.9), requestReload, unpin, experiment, KERNEL_BUILD, keys, reportError, fleetStatus,
1515
-- addReportListener, deployReports, accessCode }. 0.3.2: reload, rollback, switchBranch, pinArtifact and unpin run
1616
-- on a kernel thread (the caller waits).
1717

@@ -279,6 +279,17 @@ function Api.forEntry(entry: Script)
279279
function api:accessCode()
280280
return services.accessCode()
281281
end
282+
-- Kernel 0.5.9 (additive): the game's own reserved servers. Reserves a server (TeleportService:ReserveServer) and
283+
-- records it in the private branch registry on THIS server's branch, so it boots this branch's head like a newServer
284+
-- server instead of the universe's default branch. It does NOT teleport: the game teleports with its own
285+
-- TeleportOptions (ReservedServerAccessCode = accessCode, SetTeleportData). Never another branch (prod game code
286+
-- must not start dev builds); `options` is reserved for later and ignored. No dev check; at most RESERVE_PER_MINUTE
287+
-- (20) calls a minute per server. Yields. Returns { ok, accessCode?, privateServerId?, branch?, error? }; error =
288+
-- "studio" | "rate limited" | "no branch" | "no artifact on branch <b>" | "ReserveServer: ..." | "could not save the
289+
-- branch: ...". The access code is server-only data: never send it to a client. Its presence marks a 0.5.9 kernel.
290+
function api:reserveServer(options: any?)
291+
return services.reserveServer(options)
292+
end
282293

283294
-- Kernel 0.3.8 (additive): the signed settings record (plans/20; Settings.luau). SERVER ONLY: it holds tokens (fleet,
284295
-- analytics); never send any of it to a client. `settings` doubles as the feature test.

‎src/server/Kernel.server.luau‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,6 +3577,44 @@ Api.init({
35773577
accessCode = function()
35783578
return if serverType() == "reserved" then ownAccessCode else nil
35793579
end,
3580+
-- 0.5.9: api:reserveServer(options?): reserves a server for the GAME's own teleport and records it in the private
3581+
-- branch registry on THIS server's branch (as newServer does, with no pin), so it boots this branch's head instead
3582+
-- of the universe's default branch. Never another branch: prod game code must not be able to start dev builds.
3583+
-- No teleport, no dev check (the caller is the game's server code), at most RESERVE_PER_MINUTE a minute per server.
3584+
-- Returns { ok, accessCode?, privateServerId?, branch?, error? }; the access code is server-only data.
3585+
reserveServer = (function()
3586+
local calls: { number } = {}
3587+
return function(_options: any?)
3588+
if RunService:IsStudio() then
3589+
return { ok = false, error = "studio" }
3590+
end
3591+
local branch = myBranch
3592+
if typeof(branch) ~= "string" or branch == "" then
3593+
return { ok = false, error = "no branch" }
3594+
end
3595+
local now = os.clock()
3596+
while calls[1] and now - calls[1] >= 60 do
3597+
table.remove(calls, 1)
3598+
end
3599+
if #calls >= Constants.RESERVE_PER_MINUTE then
3600+
return { ok = false, error = "rate limited", branch = branch }
3601+
end
3602+
table.insert(calls, now)
3603+
if not Registry.branchHead(branch) and not Registry.refreshStoredHead(branch, 3) then
3604+
return { ok = false, error = `no artifact on branch {branch}`, branch = branch }
3605+
end
3606+
local ok, code, privateServerId = pcall(TeleportService.ReserveServer, TeleportService, game.PlaceId)
3607+
if not ok or typeof(code) ~= "string" or typeof(privateServerId) ~= "string" then
3608+
return { ok = false, error = `ReserveServer: {if ok then "no access code" else tostring(code)}`, branch = branch }
3609+
end
3610+
local saved, err = Registry.setPrivateBranch(privateServerId, branch, 0, nil, code)
3611+
if not saved then
3612+
-- Without the record the server would boot the default branch: don't hand out the code.
3613+
return { ok = false, error = `could not save the branch: {err}`, branch = branch }
3614+
end
3615+
return { ok = true, accessCode = code, privateServerId = privateServerId, branch = branch }
3616+
end
3617+
end)(),
35803618
})
35813619

35823620
-- 0.5.0 REMOTE DEBUG (plans/25; RemoteDebug.luau) ---------------------------------------------------------------------

‎src/shared/Constants.luau‎

Lines changed: 4 additions & 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.8",
10+
KERNEL_VERSION = "0.5.9",
1111

1212
-- The kernel's DataStore: durable heads, deployments, private records, events, and (0.3.8) the signed settings.
1313
DATASTORE = "TypeTorch",
@@ -132,6 +132,9 @@ return {
132132
FLEET_QUEUE_MAX = 100,
133133
-- At most this many requests per minute (burst 10), whatever happens: HttpService's 500/min is the game's too.
134134
FLEET_RATE_PER_MINUTE = 30,
135+
-- 0.5.9: api:reserveServer() (game code reserving a server on this server's branch) at most this many times per
136+
-- sliding minute per server: each call is a ReserveServer plus a DataStore UpdateAsync (the private/<id> record).
137+
RESERVE_PER_MINUTE = 20,
135138
-- One alert per code per this many seconds (an error spike: 5 minutes).
136139
FLEET_ALERT_GAP = 60,
137140
FLEET_SPIKE_GAP = 300,

0 commit comments

Comments
 (0)