fix(server): T3 Connect backs off from a failing relay instead of hammering it #22108
Mergedjuliusmarmingemerged 7 commits into
main fromrefs/pull/22108/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge added labelsmacroscope-review
- apps/server/src/cloud/managedTunnelStartup.ts line 63Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -38,3 +40,19 @@38403941 const MANAGED_TUNNEL_REGISTRATION_RETRY_WINDOW = Duration.minutes(10);404243+// Every host retries against the same relay, so while it is failing the44+// retries themselves are load. Backing off to minutes, and never sooner than45+// the relay's Retry-After, lets it recover instead of absorbing the storm.46+const RELAY_RETRY_MAX_DELAY = Duration.minutes(5);47+48+export const relayRetrySchedule = Schedule.exponential("1 second").pipe(49+ Schedule.modifyDelay(({ duration }) =>50+ Effect.succeed(Duration.min(duration, RELAY_RETRY_MAX_DELAY)),51+ ),52+ Schedule.jittered,53+ Schedule.modifyDelay(({ duration, input }) => {54+ const retryAfterMillis = relayRetryAfterMillis(input);55+ return Effect.succeed(56+ retryAfterMillis === undefined57+ ? duration58+ : Duration.max(duration, Duration.millis(retryAfterMillis)),apps/server/src/server.ts line 873OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -856,12 +857,17 @@856857 const localOrigin = `http://127.0.0.1:${address.port}`;857858 const endpointRuntime = yield* CloudManagedEndpointRuntime.CloudManagedEndpointRuntime;858859 const recoveryLock = yield* Semaphore.make(1);859- let lastRecoveryAtMillis = 0;860+ let lastRecoveryAtMillis = Number.NEGATIVE_INFINITY;861+ let recoveryCooldown = MANAGED_TUNNEL_RECOVERY_COOLDOWN;860862 const recoverManagedTunnel = (config: RelayManagedEndpointRuntimeConfig) =>861863 recoveryLock.withPermits(1)(862864 Effect.gen(function* () {863865 const elapsed = (yield* Clock.currentTimeMillis) - lastRecoveryAtMillis;864- const wait = Duration.toMillis(MANAGED_TUNNEL_RECOVERY_COOLDOWN) - elapsed;866+ recoveryCooldown = nextManagedTunnelRecoveryCooldown({867+ current: recoveryCooldown,868+ sinceLastRecovery: Duration.millis(elapsed),869+ });870+ const wait = Duration.toMillis(recoveryCooldown) - elapsed;865871 if (wait > 0) yield* Effect.sleep(Duration.millis(wait));866872 lastRecoveryAtMillis = yield* Clock.currentTimeMillis;867873 }).pipe(
- apps/server/src/cloud/managedTunnelStartup.ts lines 63 to 68OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -41,11 +54,15 @@54+ const retryAfterMillis = relayRetryAfterMillis(input);55+ return Effect.succeed(56+ retryAfterMillis === undefined57+ ? duration58+ : Duration.max(duration, Duration.millis(retryAfterMillis)),59+ );60+ }),61+);62+4163 export const retryManagedTunnelRegistration = <A, E, R>(4264 registration: Effect.Effect<A, E, R>,4365 isRetryable: (error: E) => boolean,4466 onRetryWindowExhausted?: Effect.Effect<void, never, R>,4567 ) => {46- const schedule = Schedule.exponential("1 second").pipe(47- Schedule.modifyDelay(({ duration }) =>48- Effect.succeed(Duration.min(duration, Duration.seconds(30))),49- ),50- Schedule.jittered,51- );68+ const schedule = relayRetrySchedule;@@ -88,4 +111,9 @@88111 ) {89112 return yield* new RelayRequestError({ rejection: "rejected", description });90113 }91- return yield* new RelayRequestError({ rejection: "unavailable", description });114+ const retryAfterMillis = parseRetryAfterMillis(115+ response.headers["retry-after"],116+ yield* Clock.currentTimeMillis,117+ );118+ return yield* new RelayRequestError({119+ rejection: "unavailable",@@ -856,9 +857,14 @@856857 const localOrigin = `http://127.0.0.1:${address.port}`;857858 const endpointRuntime = yield* CloudManagedEndpointRuntime.CloudManagedEndpointRuntime;858859 const recoveryLock = yield* Semaphore.make(1);859- let lastRecoveryAtMillis = 0;860+ let lastRecoveryAtMillis = Number.NEGATIVE_INFINITY;861+ let recoveryCooldown = MANAGED_TUNNEL_RECOVERY_COOLDOWN;860862 const recoverManagedTunnel = (config: RelayManagedEndpointRuntimeConfig) =>861863 recoveryLock.withPermits(1)(862864 Effect.gen(function* () {863865 const elapsed = (yield* Clock.currentTimeMillis) - lastRecoveryAtMillis;864- const wait = Duration.toMillis(MANAGED_TUNNEL_RECOVERY_COOLDOWN) - elapsed;866+ recoveryCooldown = nextManagedTunnelRecoveryCooldown({867+ current: recoveryCooldown,868+ sinceLastRecovery: Duration.millis(elapsed),869+ });870+ const wait = Duration.toMillis(recoveryCooldown) - elapsed;
- apps/server/src/cloud/managedTunnelStartup.ts line 136OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -70,4 +116,21 @@116+ reconcile: Effect.Effect<A, E, R>,117+ isRetryable: (error: E) => boolean,118+ onRetryWindowExhausted: (error: E) => Effect.Effect<void, never, R>,119+) =>120+ retryWithinWindow(reconcile, isRetryable).pipe(121+ Effect.catchIf(isRelayUnavailable, (error) =>122+ onRetryWindowExhausted(error).pipe(123+ Effect.andThen(124+ reconcile.pipe(Effect.retry({ while: isRelayUnavailable, schedule: relayRetrySchedule })),125+ ),126+ ),127+ ),128+ );129+70130 // Every managed host shares one relay, so a host stuck in a bad loop must not71131 // turn into a fleet-wide request storm.72132 export const MANAGED_TUNNEL_RECOVERY_COOLDOWN = Duration.minutes(2);73133134+// Retries inside one recovery, while the relay keeps failing, stay at least a135+// cooldown apart. They do not grow the cooldown: only a new request does.136+export const managedTunnelRecoveryRetrySchedule = relayRetrySchedule.pipe(
juliusmarminge merged thiscd8a986
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed