Repository navigation
perf(server,web,client-runtime): [NBR] cut provider config wire bytes by 70-90% and stop redundant broadcasts #48446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,7 +17,6 @@ import * as Context from "effect/Context"; | |||||||||||||||||||||||||||||||||||
| import * as DateTime from "effect/DateTime"; | ||||||||||||||||||||||||||||||||||||
| import * as Duration from "effect/Duration"; | ||||||||||||||||||||||||||||||||||||
| import * as Effect from "effect/Effect"; | ||||||||||||||||||||||||||||||||||||
| import * as Equal from "effect/Equal"; | ||||||||||||||||||||||||||||||||||||
| import * as Layer from "effect/Layer"; | ||||||||||||||||||||||||||||||||||||
| import * as PubSub from "effect/PubSub"; | ||||||||||||||||||||||||||||||||||||
| import * as Ref from "effect/Ref"; | ||||||||||||||||||||||||||||||||||||
|
|
@@ -121,6 +120,62 @@ function keepLastGoodReadings( | |||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| function samePlainValue(left: unknown, right: unknown, ignoreCheckedAt: boolean): boolean { | ||||||||||||||||||||||||||||||||||||
| if (Object.is(left, right)) return true; | ||||||||||||||||||||||||||||||||||||
| if (typeof left !== "object" || typeof right !== "object" || left === null || right === null) { | ||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if (Array.isArray(left) || Array.isArray(right)) { | ||||||||||||||||||||||||||||||||||||
| if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) { | ||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| for (let index = 0; index < left.length; index += 1) { | ||||||||||||||||||||||||||||||||||||
| if (!samePlainValue(left[index], right[index], ignoreCheckedAt)) return false; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| const before = left as Record<string, unknown>; | ||||||||||||||||||||||||||||||||||||
| const after = right as Record<string, unknown>; | ||||||||||||||||||||||||||||||||||||
| for (const key of Object.keys(before)) { | ||||||||||||||||||||||||||||||||||||
| if (ignoreCheckedAt && key === "checkedAt") continue; | ||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||
| !samePlainValue( | ||||||||||||||||||||||||||||||||||||
| before[key], | ||||||||||||||||||||||||||||||||||||
| Object.hasOwn(after, key) ? after[key] : undefined, | ||||||||||||||||||||||||||||||||||||
| ignoreCheckedAt, | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| for (const key of Object.keys(after)) { | ||||||||||||||||||||||||||||||||||||
| if (ignoreCheckedAt && key === "checkedAt") continue; | ||||||||||||||||||||||||||||||||||||
| if (!Object.hasOwn(before, key) && after[key] !== undefined) { | ||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| function sameUsageLimitSourcesIgnoringCheckedAt( | ||||||||||||||||||||||||||||||||||||
| previous: ReadonlyArray<UsageLimitSourceSnapshot>, | ||||||||||||||||||||||||||||||||||||
| next: ReadonlyArray<UsageLimitSourceSnapshot>, | ||||||||||||||||||||||||||||||||||||
| ): boolean { | ||||||||||||||||||||||||||||||||||||
| return samePlainValue(previous, next, true); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| function watchedUsageLimitSettings( | ||||||||||||||||||||||||||||||||||||
| settings: Pick<ServerSettings, "usageLimitSources" | "secretProviders">, | ||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||
| const hasEnabledSecretSource = Object.values(settings.usageLimitSources).some( | ||||||||||||||||||||||||||||||||||||
| (config) => config.enabled && typeof config.managementKey !== "string", | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||
| usageLimitSources: settings.usageLimitSources, | ||||||||||||||||||||||||||||||||||||
| ...(hasEnabledSecretSource ? { secretProviders: settings.secretProviders } : {}), | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| function missingManagementKey(config: UsageLimitSourceConfig): string { | ||||||||||||||||||||||||||||||||||||
| return typeof config.managementKey === "string" | ||||||||||||||||||||||||||||||||||||
| ? "No management key configured." | ||||||||||||||||||||||||||||||||||||
|
|
@@ -214,7 +269,7 @@ export const make = Effect.gen(function* () { | |||||||||||||||||||||||||||||||||||
| next: ReadonlyArray<UsageLimitSourceSnapshot>, | ||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||
| const changed = yield* Ref.modify(stateRef, (previous) => | ||||||||||||||||||||||||||||||||||||
| Equal.equals(previous, next) ? [false, previous] : [true, next], | ||||||||||||||||||||||||||||||||||||
| sameUsageLimitSourcesIgnoringCheckedAt(previous, next) ? [false, previous] : [true, next], | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| if (changed) yield* PubSub.publish(changes, next); | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
|
|
@@ -265,9 +320,20 @@ export const make = Effect.gen(function* () { | |||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||
| }, refreshLock.withPermits(1)); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| yield* settingsService.streamChanges.pipe( | ||||||||||||||||||||||||||||||||||||
| Stream.map((settings) => settings.usageLimitSources), | ||||||||||||||||||||||||||||||||||||
| Stream.changes, | ||||||||||||||||||||||||||||||||||||
| const emptyWatchedSettings = { | ||||||||||||||||||||||||||||||||||||
| usageLimitSources: {} as ServerSettings["usageLimitSources"], | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
| yield* Stream.concat( | ||||||||||||||||||||||||||||||||||||
| Stream.fromEffect( | ||||||||||||||||||||||||||||||||||||
| settingsService.getSettings.pipe( | ||||||||||||||||||||||||||||||||||||
| Effect.map(watchedUsageLimitSettings), | ||||||||||||||||||||||||||||||||||||
| Effect.orElseSucceed(() => emptyWatchedSettings), | ||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||
| settingsService.streamChanges.pipe(Stream.map(watchedUsageLimitSettings)), | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+326
to
+333
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Settings edits can be missed between the initial read and the change subscription. The watcher reads the current settings with Subscribe before reading the snapshot. Use Suggested fix
Committable suggestion
|
||||||||||||||||||||||||||||||||||||
| ).pipe( | ||||||||||||||||||||||||||||||||||||
| Stream.changesWith((previous, next) => samePlainValue(previous, next, false)), | ||||||||||||||||||||||||||||||||||||
| Stream.drop(1), | ||||||||||||||||||||||||||||||||||||
| Stream.runForEach(() => refresh), | ||||||||||||||||||||||||||||||||||||
| Effect.forkScoped, | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
|
|
@@ -304,7 +370,7 @@ export const make = Effect.gen(function* () { | |||||||||||||||||||||||||||||||||||
| const subscription = yield* PubSub.subscribe(changes); | ||||||||||||||||||||||||||||||||||||
| const snapshot = yield* Ref.get(stateRef); | ||||||||||||||||||||||||||||||||||||
| return Stream.concat(Stream.make(snapshot), Stream.fromSubscription(subscription)).pipe( | ||||||||||||||||||||||||||||||||||||
| Stream.changes, | ||||||||||||||||||||||||||||||||||||
| Stream.changesWith(sameUsageLimitSourcesIgnoringCheckedAt), | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
usage/UsageLimitSources.ts:272Successful polls with unchanged balances leave
checkedAtat the old time, socollectLimitAccountstreats the hub reading as stale and the pooled display keeps choosing another environment's newer reading.publishretains the previous snapshot whensameUsageLimitSourcesIgnoringCheckedAtmatches, and the timestamp-insensitive stream filters also hide freshness updates; preserve and deliver the newcheckedAtvalues to freshness-based consumers.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: