mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
relay: renew the relay token right after wake when it fell due during sleep
Node timers stop while the machine sleeps, so a renewal due during a sleep fired that much later after wake, after the token's wall-clock expiry. With the host-sent keepalive the control survives the sleep, so the relay closed the live control 4401 a few minutes after wake (cutting any phone session started since). The renewal and its retries now use a wall-clock timer that runs a due task on system resume.
This commit is contained in:
@@ -19,6 +19,7 @@ import { relayIdentityKey } from './relay-auth-identity'
|
||||
import { RelayOriginPool } from './relay-origin-pool'
|
||||
import { RelayRegionRefresh } from './relay-region-refresh'
|
||||
import { relayRenewalDelayMs } from './relay-renewal-jitter'
|
||||
import { RelayWallClockTimer } from './relay-wall-clock-timer'
|
||||
|
||||
export class RelaySessionBroker {
|
||||
private readonly options: RelaySessionBrokerOptions
|
||||
@@ -26,11 +27,12 @@ export class RelaySessionBroker {
|
||||
private readonly originPool: RelayOriginPool
|
||||
private readonly regionRefresh: RelayRegionRefresh | null
|
||||
private authorization: RelayAuthorization | null = null
|
||||
private refreshTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private readonly refreshTimer: RelayWallClockTimer
|
||||
private closed = false
|
||||
|
||||
private constructor(options: RelaySessionBrokerOptions) {
|
||||
this.options = options
|
||||
this.refreshTimer = new RelayWallClockTimer(options.now)
|
||||
this.relayHostId = deriveRelayHostId(options.keypair.publicKey)
|
||||
this.originPool = new RelayOriginPool({
|
||||
directorUrl: options.authConfig.relayDirectorUrl,
|
||||
@@ -199,10 +201,7 @@ export class RelaySessionBroker {
|
||||
}
|
||||
const publishOffline = this.options.isCurrent()
|
||||
this.closed = true
|
||||
if (this.refreshTimer) {
|
||||
clearTimeout(this.refreshTimer)
|
||||
this.refreshTimer = null
|
||||
}
|
||||
this.refreshTimer.cancel()
|
||||
this.originPool.closeNow(hostCloseReason)
|
||||
this.regionRefresh?.close()
|
||||
if (publishOffline) {
|
||||
@@ -259,11 +258,10 @@ export class RelaySessionBroker {
|
||||
const now = (this.options.now ?? Date.now)()
|
||||
const random = this.options.random ?? Math.random
|
||||
const delay = relayRenewalDelayMs(authorization.expiresAt, now, random)
|
||||
this.refreshTimer = setTimeout(() => void this.refreshAuthorization(), delay)
|
||||
this.refreshTimer.schedule(delay, () => void this.refreshAuthorization())
|
||||
}
|
||||
|
||||
private async refreshAuthorization(): Promise<void> {
|
||||
this.refreshTimer = null
|
||||
try {
|
||||
const refresh = await this.options.refreshAccessToken()
|
||||
this.assertCurrent()
|
||||
@@ -287,9 +285,9 @@ export class RelaySessionBroker {
|
||||
const now = (this.options.now ?? Date.now)()
|
||||
if (!this.closed && this.options.isCurrent() && now <= expiry + 60_000) {
|
||||
const random = this.options.random ?? Math.random
|
||||
this.refreshTimer = setTimeout(
|
||||
() => void this.refreshAuthorization(),
|
||||
5_000 + Math.floor(random() * 10_001)
|
||||
this.refreshTimer.schedule(
|
||||
5_000 + Math.floor(random() * 10_001),
|
||||
() => void this.refreshAuthorization()
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { RelayWallClockTimer } from './relay-wall-clock-timer'
|
||||
|
||||
function resumeSource(): {
|
||||
subscribe: (onResume: () => void) => () => void
|
||||
resume: () => void
|
||||
count: () => number
|
||||
} {
|
||||
const listeners = new Set<() => void>()
|
||||
return {
|
||||
subscribe: (onResume) => {
|
||||
listeners.add(onResume)
|
||||
onResume()
|
||||
return () => listeners.delete(onResume)
|
||||
},
|
||||
resume: () => {
|
||||
for (const listener of Array.from(listeners)) {
|
||||
listener()
|
||||
}
|
||||
},
|
||||
count: () => listeners.size
|
||||
}
|
||||
}
|
||||
|
||||
describe('RelayWallClockTimer', () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('runs a task that fell due during a sleep as soon as the machine resumes', () => {
|
||||
vi.useFakeTimers()
|
||||
let wall = 1_000_000
|
||||
const source = resumeSource()
|
||||
const timer = new RelayWallClockTimer(() => wall, source.subscribe)
|
||||
const task = vi.fn()
|
||||
timer.schedule(180_000, task)
|
||||
// Asleep: the wall clock moves, the timer clock does not.
|
||||
wall += 200_000
|
||||
source.resume()
|
||||
expect(task).toHaveBeenCalledTimes(1)
|
||||
vi.advanceTimersByTime(180_000)
|
||||
expect(task).toHaveBeenCalledTimes(1)
|
||||
expect(source.count()).toBe(0)
|
||||
})
|
||||
|
||||
it('keeps waiting when the machine resumes before the task is due', () => {
|
||||
vi.useFakeTimers()
|
||||
let wall = 1_000_000
|
||||
const source = resumeSource()
|
||||
const timer = new RelayWallClockTimer(() => wall, source.subscribe)
|
||||
const task = vi.fn()
|
||||
timer.schedule(180_000, task)
|
||||
wall += 60_000
|
||||
source.resume()
|
||||
expect(task).not.toHaveBeenCalled()
|
||||
vi.advanceTimersByTime(180_000)
|
||||
expect(task).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('does not run a due task on the state report at subscribe, and never after cancel', () => {
|
||||
vi.useFakeTimers()
|
||||
const source = resumeSource()
|
||||
const timer = new RelayWallClockTimer(() => 0, source.subscribe)
|
||||
const task = vi.fn()
|
||||
timer.schedule(0, task)
|
||||
expect(task).not.toHaveBeenCalled()
|
||||
timer.cancel()
|
||||
source.resume()
|
||||
vi.advanceTimersByTime(1_000)
|
||||
expect(task).not.toHaveBeenCalled()
|
||||
expect(source.count()).toBe(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,53 @@
|
||||
import { subscribeSystemPowerLifecycle } from '../../power/system-power-lifecycle'
|
||||
|
||||
type SubscribeResume = (onResume: () => void) => () => void
|
||||
|
||||
const subscribeResume: SubscribeResume = (onResume) =>
|
||||
subscribeSystemPowerLifecycle({ onSuspend: () => {}, onResume })
|
||||
|
||||
// A one-shot timer due at a wall-clock time. Node timers run on a clock that stops while the
|
||||
// machine sleeps, so a relay token renewal due during a sleep would fire that much later after
|
||||
// wake, after the token's wall-clock expiry: the relay then closes the still-live control 4401.
|
||||
// On resume, a task already due runs at once.
|
||||
export class RelayWallClockTimer {
|
||||
private timer: ReturnType<typeof setTimeout> | null = null
|
||||
private unsubscribe: (() => void) | null = null
|
||||
private dueAt = 0
|
||||
private task: (() => void) | null = null
|
||||
|
||||
constructor(
|
||||
private readonly now: () => number = Date.now,
|
||||
private readonly onResume: SubscribeResume = subscribeResume
|
||||
) {}
|
||||
|
||||
schedule(delayMs: number, task: () => void): void {
|
||||
this.cancel()
|
||||
this.dueAt = this.now() + delayMs
|
||||
this.task = task
|
||||
this.timer = setTimeout(() => this.fire(), delayMs)
|
||||
// The lifecycle reports the current state on subscribe; only later resumes count.
|
||||
let subscribed = false
|
||||
this.unsubscribe = this.onResume(() => {
|
||||
if (subscribed && this.task === task && this.now() >= this.dueAt) {
|
||||
this.fire()
|
||||
}
|
||||
})
|
||||
subscribed = true
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
if (this.timer) {
|
||||
clearTimeout(this.timer)
|
||||
this.timer = null
|
||||
}
|
||||
this.unsubscribe?.()
|
||||
this.unsubscribe = null
|
||||
this.task = null
|
||||
}
|
||||
|
||||
private fire(): void {
|
||||
const task = this.task
|
||||
this.cancel()
|
||||
task?.()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user