Skip to content

Commit 722c2ea

Browse files
author
Shaun Hawk
committed
refactor(muse): follow Effect function conventions for usage reports
1 parent e7d1bc3 commit 722c2ea

1 file changed

Lines changed: 46 additions & 47 deletions

File tree

‎packages/provider-muse/src/server/driver.ts‎

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,26 @@ export const MuseDriver: ProviderDriver<
131131
// Reports and status checks take turns, so an older report cannot publish after a
132132
// newer one, and none publishes for a generation a check has just replaced.
133133
const usagePermit = yield* Semaphore.make(1);
134-
const withUsageLimits = (provider: ServerProviderDraft) =>
135-
usagePermit.withPermits(1)(
136-
Effect.gen(function* () {
137-
const { generation } = yield* Ref.updateAndGet(usageAccount, (account) =>
138-
nextMuseUsageAccount(account, provider.auth),
139-
);
140-
const { retained, dropped } = yield* Ref.modify(latestUsage, (current) => {
141-
const drop = current !== undefined && current.generation !== generation;
142-
const kept = drop ? undefined : current;
143-
return [{ retained: kept, dropped: drop }, kept] as const;
144-
});
145-
const usageLimits = museStatusUsageLimits({
146-
auth: provider.auth,
147-
enabled: provider.enabled,
148-
observation: retained?.usage,
149-
dropped,
150-
nowMs: DateTime.toEpochMillis(yield* DateTime.now),
151-
});
152-
return usageLimits ? { ...provider, usageLimits } : provider;
153-
}),
134+
const withUsageLimits = Effect.fn("MuseDriver.withUsageLimits")(function* (
135+
provider: ServerProviderDraft,
136+
) {
137+
const { generation } = yield* Ref.updateAndGet(usageAccount, (account) =>
138+
nextMuseUsageAccount(account, provider.auth),
154139
);
140+
const { retained, dropped } = yield* Ref.modify(latestUsage, (current) => {
141+
const drop = current !== undefined && current.generation !== generation;
142+
const kept = drop ? undefined : current;
143+
return [{ retained: kept, dropped: drop }, kept] as const;
144+
});
145+
const usageLimits = museStatusUsageLimits({
146+
auth: provider.auth,
147+
enabled: provider.enabled,
148+
observation: retained?.usage,
149+
dropped,
150+
nowMs: DateTime.toEpochMillis(yield* DateTime.now),
151+
});
152+
return usageLimits ? { ...provider, usageLimits } : provider;
153+
}, usagePermit.withPermits(1));
155154
const resolveInstallation = yield* makeCachedProviderMaintenanceResolution(
156155
resolveProviderMaintenanceCapabilitiesEffect(museMaintenance, {
157156
binaryPath: effectiveConfig.binaryPath,
@@ -233,33 +232,33 @@ export const MuseDriver: ProviderDriver<
233232
usageAccountGeneration: Ref.get(usageAccount).pipe(
234233
Effect.map((account) => account.generation),
235234
),
236-
onSubscriptionUsage: (usage, hostGeneration) =>
237-
usagePermit.withPermits(1)(
238-
Effect.gen(function* () {
239-
const account = yield* Ref.get(usageAccount);
240-
// A host from before a logout or another login reports for an account
241-
// that is gone, and signed out there is no account to report for.
242-
if (hostGeneration !== account.generation || account.identity === null) return;
243-
// Every session's host reports; an older report arriving late must not win.
244-
const newer = yield* Ref.modify(latestUsage, (previous) =>
245-
previous && previous.usage.observedAtMs >= usage.observedAtMs
246-
? [false, previous]
247-
: [true, { usage, generation: hostGeneration }],
248-
);
249-
const { auth } = yield* snapshot.getSnapshot;
250-
// The probe may have advanced the generation before publishing its new account.
251-
if (!newer || auth.type === "apiKey" || museUsageAccount(auth) !== account.identity)
252-
return;
253-
const now = yield* DateTime.now;
254-
yield* snapshot.applyUsageLimits({
255-
windows: museUsageWindows(usage, DateTime.toEpochMillis(now)),
256-
checkedAt: DateTime.formatIso(DateTime.makeUnsafe(usage.observedAtMs)),
257-
// A report is the account's whole state, so a window that has reset
258-
// since loses its old reset time instead of keeping it.
259-
replace: true,
260-
});
261-
}),
262-
),
235+
// Per-notification work stays within the adapter's notification handling.
236+
onSubscriptionUsage: Effect.fnUntraced(function* (
237+
usage: MuseSubscriptionUsage,
238+
hostGeneration: number,
239+
) {
240+
const account = yield* Ref.get(usageAccount);
241+
// A host from before a logout or another login reports for an account
242+
// that is gone, and signed out there is no account to report for.
243+
if (hostGeneration !== account.generation || account.identity === null) return;
244+
// Every session's host reports; an older report arriving late must not win.
245+
const newer = yield* Ref.modify(latestUsage, (previous) =>
246+
previous && previous.usage.observedAtMs >= usage.observedAtMs
247+
? [false, previous]
248+
: [true, { usage, generation: hostGeneration }],
249+
);
250+
const { auth } = yield* snapshot.getSnapshot;
251+
// The probe may have advanced the generation before publishing its new account.
252+
if (!newer || auth.type === "apiKey" || museUsageAccount(auth) !== account.identity) return;
253+
const now = yield* DateTime.now;
254+
yield* snapshot.applyUsageLimits({
255+
windows: museUsageWindows(usage, DateTime.toEpochMillis(now)),
256+
checkedAt: DateTime.formatIso(DateTime.makeUnsafe(usage.observedAtMs)),
257+
// A report is the account's whole state, so a window that has reset
258+
// since loses its old reset time instead of keeping it.
259+
replace: true,
260+
});
261+
}, usagePermit.withPermits(1)),
263262
...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
264263
continuationRequests,
265264
});

0 commit comments

Comments
 (0)