mirror of
https://github.com/morluto/rea.git
synced 2026-11-03 21:36:13 +00:00
Share the in-flight release for one exact owner, preserve failed attempts for retry, and notify dependent errors only after confirmed completion. Validation: concurrency control fails on3fcba0796 with two cleanup attempts; isolated foundation passes38 selected lease/scope/resource tests. Authored lifecycle controls, no native provider qualification.
143 lines
4.3 KiB
TypeScript
143 lines
4.3 KiB
TypeScript
import type {
|
|
AnalysisError,
|
|
AnalysisCleanupResourceOwner,
|
|
} from "../domain/analysisErrorBase.js";
|
|
|
|
/** Link one resource's confirmed release to its originating analysis error. */
|
|
export class AnalysisCleanupLease {
|
|
#released = false;
|
|
#releasePromise: Promise<void> | undefined;
|
|
#errors: WeakRef<AnalysisError>[] = [];
|
|
readonly #groups = new Set<AnalysisCleanupLeaseGroup>();
|
|
|
|
/** Whether this exact owner has confirmed release. */
|
|
get released(): boolean {
|
|
return this.#released;
|
|
}
|
|
|
|
readonly #resourceOwner: AnalysisCleanupResourceOwner | undefined;
|
|
readonly #resources: readonly string[] | undefined;
|
|
|
|
get cleanupResources(): readonly string[] | undefined {
|
|
return this.#resourceOwner === undefined
|
|
? this.#resources
|
|
: this.#resourceOwner.cleanupResources;
|
|
}
|
|
|
|
/** Adopt exact close authority while retaining its live labels across retries. */
|
|
static forCloseOwner(
|
|
owner: {
|
|
close(): Promise<void>;
|
|
readonly cleanupResources?: readonly string[] | undefined;
|
|
},
|
|
resource: string,
|
|
): AnalysisCleanupLease {
|
|
return new AnalysisCleanupLease(
|
|
() => owner.close(),
|
|
!("cleanupResources" in owner)
|
|
? [resource]
|
|
: {
|
|
get cleanupResources() {
|
|
return owner.cleanupResources;
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
constructor(
|
|
private readonly cleanup: () => Promise<void>,
|
|
resources?: readonly string[] | AnalysisCleanupResourceOwner,
|
|
) {
|
|
if (resources !== undefined && "cleanupResources" in resources) {
|
|
this.#resourceOwner = resources;
|
|
this.#resources = undefined;
|
|
} else {
|
|
this.#resourceOwner = undefined;
|
|
this.#resources = resources === undefined ? undefined : [...resources];
|
|
}
|
|
}
|
|
|
|
release(): Promise<void> {
|
|
if (this.#released) return Promise.resolve();
|
|
if (this.#releasePromise !== undefined) return this.#releasePromise;
|
|
const attempt = Promise.resolve()
|
|
.then(async () => {
|
|
await this.cleanup();
|
|
this.#released = true;
|
|
this.#resolveBoundErrors();
|
|
for (const group of this.#groups) group.ownerReleased(this);
|
|
this.#groups.clear();
|
|
})
|
|
.finally(() => {
|
|
if (this.#releasePromise === attempt) this.#releasePromise = undefined;
|
|
});
|
|
this.#releasePromise = attempt;
|
|
return attempt;
|
|
}
|
|
|
|
/** Binding can follow a concurrent retry that already confirmed release. */
|
|
bindCleanupError(error: AnalysisError): void {
|
|
if (this.#released) {
|
|
error.resolveCleanup();
|
|
return;
|
|
}
|
|
this.#errors = [
|
|
...this.#errors.filter((reference) => reference.deref() !== undefined),
|
|
new WeakRef(error),
|
|
];
|
|
}
|
|
|
|
/** Bind one aggregate error to every exact lease in a failed retry set. */
|
|
bindCleanupGroup(group: AnalysisCleanupLeaseGroup): void {
|
|
if (this.#released) {
|
|
group.ownerReleased(this);
|
|
return;
|
|
}
|
|
for (const retained of this.#groups)
|
|
if (!retained.hasLiveError) this.#groups.delete(retained);
|
|
this.#groups.add(group);
|
|
}
|
|
|
|
#resolveBoundErrors(): void {
|
|
for (const reference of this.#errors) reference.deref()?.resolveCleanup();
|
|
this.#errors = [];
|
|
}
|
|
}
|
|
|
|
/** Conjunctive cleanup binding for an error covering multiple exact owners. */
|
|
export class AnalysisCleanupLeaseGroup {
|
|
readonly #error: WeakRef<AnalysisError>;
|
|
readonly #pending: Set<AnalysisCleanupLease>;
|
|
|
|
constructor(error: AnalysisError, owners: readonly AnalysisCleanupLease[]) {
|
|
this.#error = new WeakRef(error);
|
|
this.#pending = new Set(owners);
|
|
if (this.#pending.size === 0) return;
|
|
error.bindCleanupResourceOwner(this);
|
|
for (const owner of this.#pending) owner.bindCleanupGroup(this);
|
|
}
|
|
|
|
/** Unlabelled owners keep attribution unknown; never match resources by text. */
|
|
get cleanupResources(): readonly string[] | undefined {
|
|
if (
|
|
[...this.#pending].some((owner) => owner.cleanupResources === undefined)
|
|
)
|
|
return undefined;
|
|
return [
|
|
...new Set(
|
|
[...this.#pending].flatMap((owner) => owner.cleanupResources ?? []),
|
|
),
|
|
];
|
|
}
|
|
|
|
get hasLiveError(): boolean {
|
|
return this.#error.deref() !== undefined;
|
|
}
|
|
|
|
ownerReleased(owner: AnalysisCleanupLease): void {
|
|
if (!this.#pending.delete(owner) || this.#pending.size > 0) return;
|
|
this.#error.deref()?.resolveCleanup();
|
|
this.#pending.clear();
|
|
}
|
|
}
|