Skip to content

Commit 8455b57

Browse files
committed
fix(webapp): build the idempotency shard-client map on first use
Building it at module scope dereferenced the db.server run-ops handles at import time. This module is imported by triggerTask, so any test that mocks `~/db.server` without those two exports failed to collect — runEngineBatchTriggerStoreRouting and routesBatchGetReplicaLag both did. Before this change those handles were only read inside the two methods that use them. Restore that: resolve the map on first use and memoise it, since the trigger path is the hottest in the system. No test behaviour changes; this is the inert-by-default property the change was supposed to have in the first place.
1 parent b32aafa commit 8455b57

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

apps/webapp/app/runEngine/concerns/idempotencyKeys.server.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,21 @@ const MAX_CLEARED_WINNER_REACQUIRES = 5;
3535

3636
// Every run-ops store keyed by shard key. Both idempotency call sites resolve through this
3737
// one map, so they cannot disagree about which store owns an id.
38-
const idempotencyShardClients: ReadonlyMap<ShardKey, PrismaClientOrTransaction> = new Map<
39-
ShardKey,
40-
PrismaClientOrTransaction
41-
>([
42-
["legacy", runOpsLegacyPrisma],
43-
["new", runOpsNewPrisma],
44-
...[...runOpsShardWriters.entries()].map(
45-
([key, writer]) => [key, writer as PrismaClientOrTransaction] as const
46-
),
47-
]);
38+
//
39+
// Built on first use, not at import: dereferencing the db.server handles at module scope
40+
// breaks any test that mocks `~/db.server` without them, and this module is imported by
41+
// triggerTask. Memoised because the trigger path is the hottest in the system.
42+
let cachedShardClients: ReadonlyMap<ShardKey, PrismaClientOrTransaction> | undefined;
43+
44+
function idempotencyShardClients(): ReadonlyMap<ShardKey, PrismaClientOrTransaction> {
45+
return (cachedShardClients ??= new Map<ShardKey, PrismaClientOrTransaction>([
46+
["legacy", runOpsLegacyPrisma],
47+
["new", runOpsNewPrisma],
48+
...[...runOpsShardWriters.entries()].map(
49+
([key, writer]) => [key, writer as PrismaClientOrTransaction] as const
50+
),
51+
]));
52+
}
4853

4954
// Claim ownership context returned to the caller when the
5055
// IdempotencyKeyConcern won a pre-gate claim. Caller MUST publish the
@@ -186,7 +191,7 @@ export class IdempotencyKeyConcern {
186191
{
187192
isSplitEnabled,
188193
fallbackClient: this.prisma,
189-
clients: idempotencyShardClients,
194+
clients: idempotencyShardClients(),
190195
resolveMintKind: resolveRunIdMintKind,
191196
logger,
192197
}
@@ -656,7 +661,7 @@ export class IdempotencyKeyConcern {
656661
// call sites in agreement and stops this reading as gen-2-unaware.
657662
const client = clientForShardKey(
658663
resolveShard(internalId),
659-
idempotencyShardClients,
664+
idempotencyShardClients(),
660665
this.prisma,
661666
logger
662667
);

0 commit comments

Comments
 (0)