1import * as Effect from "effect/Effect";
2import * as Semaphore from "effect/Semaphore";
3
4// The testing account has the default quota of five VPCs per Region. Its
5// default VPC consumes one slot, and non-EC2 suites (EFS, ECS, RDS, etc.) may
6// need another while EC2 tests run. Limit EC2 to three concurrent custom VPCs
7// so the full AWS wave retains one spare slot instead of racing the hard quota.
8const ec2VpcCapacity = Semaphore.makeUnsafe(3);
9
10export const makeEc2VpcCapacityLease = (permits: 1 | 2 = 1) => {
11 let held = false;
12
13 return {
14 acquire: Effect.uninterruptibleMask((restore) =>
15 restore(ec2VpcCapacity.take(permits)).pipe(
16 Effect.tap(() =>
17 Effect.sync(() => {
18 held = true;
19 }),
20 ),
21 Effect.asVoid,
22 ),
23 ),
24 release: Effect.suspend(() => {
25 if (!held) return Effect.void;
26 held = false;
27 return ec2VpcCapacity.release(permits).pipe(Effect.asVoid);
28 }),
29 };
30};