Skip to content

Commit 85259a8

Browse files
thymikeeclaude
andcommitted
refactor(provision-kit): split the lease table's start and teardown steps
Fallow's cognitive-complexity bar flagged runStart and runTeardown. Each step now has its own method (call the start, record its answer, name a failure, end a failed start; name a rental, finish a teardown), with no change in behavior. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 572f802 commit 85259a8

1 file changed

Lines changed: 106 additions & 61 deletions

File tree

‎packages/provision-kit/src/provider-lease-table.ts‎

Lines changed: 106 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ type Rental<Handle> =
156156

157157
type StopFailure = Readonly<{ cause: unknown }>;
158158

159+
type StartAnswer<Session, Handle> =
160+
| ProviderLeaseStart<Session, Handle>
161+
| Readonly<{ status: 'rejected'; error: unknown }>;
162+
159163
type Ended<Session, Handle> =
160164
| Readonly<{ status: 'stopped'; handle: Handle; session: Session | undefined }>
161165
| Readonly<{ status: 'nothing-rented' }>
@@ -342,47 +346,55 @@ class LeaseTable<Session, Handle, Request> {
342346
caller: ProviderLeaseBudget,
343347
): Promise<Outcome<Session>> {
344348
const startedAt = Date.now();
345-
let started: ProviderLeaseStart<Session, Handle> | undefined;
346-
let rejection: unknown;
349+
const answer = await this.callStart(entry, request, caller);
350+
recordAnswer(entry, answer);
351+
if (answer.status === 'ready' && !entry.releasing && entry.waiters > 0) {
352+
entry.session = answer.session;
353+
return { status: 'ready', session: answer.session };
354+
}
355+
return await this.endFailedStart(entry, this.startFailure(entry, answer, caller, startedAt));
356+
}
357+
358+
/** The start's answer, with a rejection read as an answer that may have rented. */
359+
private async callStart(
360+
entry: LeaseEntry<Session, Handle, Request>,
361+
request: Request,
362+
caller: ProviderLeaseBudget,
363+
): Promise<StartAnswer<Session, Handle>> {
347364
try {
348-
started = await this.driver.start(entry.lease, request, {
365+
return await this.driver.start(entry.lease, request, {
349366
caller,
350367
deadline: entry.deadline,
351368
rented: (handle) => {
352369
if (!entry.answered) entry.rental = { kind: 'held', handle };
353370
},
354371
});
355372
} catch (error) {
356-
rejection = error;
373+
return { status: 'rejected', error };
357374
}
358-
if (started === undefined) {
359-
if (entry.rental.kind !== 'held') entry.rental = { kind: 'unknown' };
360-
} else if (started.status === 'ready') {
361-
entry.rental = { kind: 'held', handle: started.handle };
362-
} else {
363-
entry.rental =
364-
started.rented === undefined ? { kind: 'none' } : { kind: 'held', handle: started.rented };
365-
}
366-
entry.answered = true;
367-
entry.markAnswered();
368-
let error: unknown;
369-
if (started?.status === 'ready') {
370-
if (!entry.releasing && entry.waiters > 0) {
371-
entry.session = started.session;
372-
return { status: 'ready', session: started.session };
373-
}
374-
error = entry.releasing
375-
? this.driver.releasing(entry.lease)
376-
: this.driver.gaveUp(
377-
entry.lease,
378-
caller,
379-
Math.max(0, entry.deadline - startedAt),
380-
started.handle,
381-
);
382-
} else {
383-
error = started === undefined ? rejection : started.error;
384-
}
385-
// A release, recovery, or drain already owns the teardown, and it waits on this answer.
375+
}
376+
377+
/** Why a start that hands out no session failed, for the callers still waiting on it. */
378+
private startFailure(
379+
entry: LeaseEntry<Session, Handle, Request>,
380+
answer: StartAnswer<Session, Handle>,
381+
caller: ProviderLeaseBudget,
382+
startedAt: number,
383+
): unknown {
384+
if (answer.status !== 'ready') return answer.error;
385+
if (entry.releasing) return this.driver.releasing(entry.lease);
386+
const budgetMs = Math.max(0, entry.deadline - startedAt);
387+
return this.driver.gaveUp(entry.lease, caller, budgetMs, answer.handle);
388+
}
389+
390+
/**
391+
* Tears down a start that hands out no session. A release, recovery, or drain that already owns the
392+
* teardown waits on this answer, so the start only nudges it.
393+
*/
394+
private async endFailedStart(
395+
entry: LeaseEntry<Session, Handle, Request>,
396+
error: unknown,
397+
): Promise<Outcome<Session>> {
386398
if (entry.releasing) {
387399
void this.teardown(entry).catch(() => undefined);
388400
return { status: 'failed', error };
@@ -459,39 +471,51 @@ class LeaseTable<Session, Handle, Request> {
459471
): Promise<Ended<Session, Handle>> {
460472
for (;;) {
461473
await this.untilAnswered(entry);
462-
if (entry.rental.kind === 'unknown') {
463-
try {
464-
const handle = await this.driver.findByLease(entry.lease);
465-
if (entry.rental.kind === 'unknown') entry.rental = { kind: 'held', handle };
466-
} catch (error) {
467-
return { status: 'pending', handle: undefined, cause: error };
468-
}
474+
const unnamed = await this.nameRental(entry);
475+
if (unnamed !== undefined) return unnamed;
476+
const handle = entry.rental.kind === 'held' ? entry.rental.handle : undefined;
477+
if (handle === undefined || entry.stopped === this.driver.handleKey(handle)) {
478+
return this.finishTeardown(entry, handle);
469479
}
470-
const rental = entry.rental;
471-
if (rental.kind === 'held' && entry.stopped !== this.driver.handleKey(rental.handle)) {
472-
const failure = await this.stop(entry, rental.handle);
473-
if (failure !== undefined) {
474-
return { status: 'pending', handle: rental.handle, cause: failure.cause };
475-
}
476-
// The start may have answered, or named another rental, while the stop ran.
477-
continue;
478-
}
479-
const handle = rental.kind === 'held' ? rental.handle : undefined;
480-
if (!entry.answered) {
481-
// The start may still rent something, so the entry stays for the teardown its answer starts.
482-
return {
483-
status: 'pending',
484-
handle,
485-
cause: new Error('The provider start did not answer within its start timeout.'),
486-
};
487-
}
488-
if (this.entries.get(entry.lease.leaseId) === entry) this.entries.delete(entry.lease.leaseId);
489-
return handle === undefined
490-
? { status: 'nothing-rented' }
491-
: { status: 'stopped', handle, session: entry.session };
480+
const failure = await this.stop(entry, handle);
481+
if (failure !== undefined) return { status: 'pending', handle, cause: failure.cause };
482+
// The start may have answered, or named another rental, while the stop ran.
492483
}
493484
}
494485

486+
/** Asks the provider for a rental this process may hold but cannot name. */
487+
private async nameRental(
488+
entry: LeaseEntry<Session, Handle, Request>,
489+
): Promise<Ended<Session, Handle> | undefined> {
490+
if (entry.rental.kind !== 'unknown') return undefined;
491+
try {
492+
const handle = await this.driver.findByLease(entry.lease);
493+
if (entry.rental.kind === 'unknown') entry.rental = { kind: 'held', handle };
494+
return undefined;
495+
} catch (error) {
496+
return { status: 'pending', handle: undefined, cause: error };
497+
}
498+
}
499+
500+
/** Forgets an entry whose rentals are all stopped, unless its start may still rent something. */
501+
private finishTeardown(
502+
entry: LeaseEntry<Session, Handle, Request>,
503+
handle: Handle | undefined,
504+
): Ended<Session, Handle> {
505+
if (!entry.answered) {
506+
// The entry stays for the teardown that the start's answer starts.
507+
return {
508+
status: 'pending',
509+
handle,
510+
cause: new Error('The provider start did not answer within its start timeout.'),
511+
};
512+
}
513+
if (this.entries.get(entry.lease.leaseId) === entry) this.entries.delete(entry.lease.leaseId);
514+
return handle === undefined
515+
? { status: 'nothing-rented' }
516+
: { status: 'stopped', handle, session: entry.session };
517+
}
518+
495519
/** Waits for the start's answer, at most `startTimeoutMs` after it was sent. */
496520
private async untilAnswered(entry: LeaseEntry<Session, Handle, Request>): Promise<void> {
497521
if (entry.answered) return;
@@ -559,3 +583,24 @@ class LeaseTable<Session, Handle, Request> {
559583
);
560584
}
561585
}
586+
587+
/** Records what the start's answer says it rented, and wakes any teardown waiting on it. */
588+
function recordAnswer<Session, Handle, Request>(
589+
entry: LeaseEntry<Session, Handle, Request>,
590+
answer: StartAnswer<Session, Handle>,
591+
): void {
592+
switch (answer.status) {
593+
case 'ready':
594+
entry.rental = { kind: 'held', handle: answer.handle };
595+
break;
596+
case 'failed':
597+
entry.rental =
598+
answer.rented === undefined ? { kind: 'none' } : { kind: 'held', handle: answer.rented };
599+
break;
600+
case 'rejected':
601+
if (entry.rental.kind !== 'held') entry.rental = { kind: 'unknown' };
602+
break;
603+
}
604+
entry.answered = true;
605+
entry.markAnswered();
606+
}

0 commit comments

Comments
 (0)