This commit is contained in:
dahyeon committed 2026-08-13 01:34:54 +09:00
1 parent d2c03d155d
commit db60190e19
13 files changed
+505 -38

No files matched your search

+30 -4
View File
@@ -7,7 +7,12 @@ import {
type TimingTestRequest,
type TimingTestResult,
} from '../../src/type/reservation'
import { edgeBrowserHeaders, requestKeyescape } from './keyescape-http'
import {
disposeKeyescapeTransport,
edgeBrowserHeaders,
requestKeyescape,
setKeyescapeTransportMode,
} from './keyescape-http'
import { appendClockSample, computePlannedSendAtMs, estimateClock, observationToSample } from './server-clock-core'
const CLOCK_EVENT = 'reservation:clock-state'
@@ -35,6 +40,7 @@ export class ServerClockService {
private lastError?: string
private settings: ClockCalibrationSettings = { ...CLOCK_CALIBRATION_DEFAULTS }
private precisionBoundaryServerMs?: number
private quietUntilMs = 0
getEstimate(): ClockEstimate {
const estimate = estimateClock(this.samples, Date.now(), this.isRunning(), this.settings)
@@ -50,7 +56,10 @@ export class ServerClockService {
? Math.round(Math.min(maximum, Math.max(minimum, value)))
: fallback
const requiredBoundaryCount = integer(input.requiredBoundaryCount, 3, 12, CLOCK_CALIBRATION_DEFAULTS.requiredBoundaryCount)
const transportMode = input.transportMode === 'http2' ? 'http2' : 'http1'
const transportChanged = setKeyescapeTransportMode(transportMode)
this.settings = {
transportMode,
enabled: Boolean(input.enabled),
autoApply: Boolean(input.autoApply),
scanWindowMs: integer(input.scanWindowMs, 100, 450, CLOCK_CALIBRATION_DEFAULTS.scanWindowMs),
@@ -63,8 +72,9 @@ export class ServerClockService {
maintenanceIntervalSec: integer(input.maintenanceIntervalSec, 5, 120, CLOCK_CALIBRATION_DEFAULTS.maintenanceIntervalSec),
sampleWindowSize: integer(input.sampleWindowSize, 60, 600, CLOCK_CALIBRATION_DEFAULTS.sampleWindowSize),
}
this.samples = this.samples.slice(-this.settings.sampleWindowSize)
this.samples = transportChanged ? [] : this.samples.slice(-this.settings.sampleWindowSize)
this.precisionBoundaryServerMs = undefined
if (transportChanged) this.lastError = `${transportMode === 'http2' ? 'HTTP/2 단일 세션' : 'HTTP/1.1 keep-alive'} 모드로 전환해 시간을 다시 보정합니다.`
this.emit()
this.scheduleNext()
return this.getEstimate()
@@ -85,6 +95,9 @@ export class ServerClockService {
}
setAutoTarget(targetServerTimeMs?: number) {
if (this.autoTargetServerTimeMs !== undefined && targetServerTimeMs === undefined) {
this.quietUntilMs = Date.now() + 500
}
this.autoTargetServerTimeMs = targetServerTimeMs
if (targetServerTimeMs) void this.probeNow().catch(() => undefined)
this.scheduleNext()
@@ -121,6 +134,8 @@ export class ServerClockService {
serverDateHeader: headerValue(response.headers.date),
remoteIp: response.remoteIp,
reusedSocket: response.reusedSocket,
protocol: response.protocol,
connectionId: response.connectionId,
ageHeader: headerValue(response.headers.age),
viaHeader: headerValue(response.headers.via),
cacheHeader: headerValue(response.headers['x-cache']),
@@ -131,10 +146,17 @@ export class ServerClockService {
sample.rejectionReason = `설정된 최대 RTT ${this.settings.maxProbeRttMs}ms 초과`
}
const previousIp = this.getEstimate().remoteIp
const previousEstimate = this.getEstimate()
const previousIp = previousEstimate.remoteIp
const previousConnectionId = previousEstimate.connectionId
this.samples = appendClockSample(this.samples, sample, this.settings.sampleWindowSize)
if (sample.accepted && previousIp && sample.remoteIp !== previousIp) this.precisionBoundaryServerMs = undefined
this.lastError = undefined
else if (sample.accepted && response.protocol === 'h2' && previousConnectionId
&& response.connectionId !== previousConnectionId) {
this.precisionBoundaryServerMs = undefined
this.lastError = `HTTP/2 세션이 ${previousConnectionId}에서 ${response.connectionId}로 교체되어 같은 경로에서 재검증 중입니다.`
}
else this.lastError = undefined
this.emit()
return sample
} catch (error) {
@@ -172,6 +194,8 @@ export class ServerClockService {
uncertaintyMs: estimateAtSend.uncertaintyMs,
remoteIp: sample.remoteIp,
reusedSocket: sample.reusedSocket,
protocol: sample.protocol,
connectionId: sample.connectionId,
}
}
@@ -182,6 +206,7 @@ export class ServerClockService {
this.timer = undefined
this.samples = []
this.precisionBoundaryServerMs = undefined
disposeKeyescapeTransport()
}
private isRunning() {
@@ -226,6 +251,7 @@ export class ServerClockService {
}
private nextInterval() {
if (this.quietUntilMs > Date.now()) return this.quietUntilMs - Date.now()
return Math.min(this.regularInterval(), this.precisionInterval())
}