1import * as Effect from "effect/Effect";
2import * as Semaphore from "effect/Semaphore";
3
4// AppRegistry's account/Region control plane has a low write TPS ceiling.
5// alchemy-test runs files concurrently in one process, so hold this lease for
6// each file's full lifecycle. This preserves concurrency across AWS services
7// while preventing AppRegistry's own create/associate/replace/delete bursts
8// from throttling one another.
9const appRegistryTestLock = Semaphore.makeUnsafe(1);
10
11export const makeAppRegistryTestLease = () => {
12 let held = false;
13
14 return {
15 acquire: Effect.uninterruptibleMask((restore) =>
16 restore(appRegistryTestLock.take(1)).pipe(
17 Effect.tap(() =>
18 Effect.sync(() => {
19 held = true;
20 }),
21 ),
22 Effect.asVoid,
23 ),
24 ),
25 release: Effect.suspend(() => {
26 if (!held) return Effect.void;
27 held = false;
28 return appRegistryTestLock.release(1).pipe(Effect.asVoid);
29 }),
30 };
31};