perf(relay): avoid repeated activity decoding (#9708)
This commit is contained in:
1 parent
dffb4cd3b1
commit
c75299ee20
4 files changed
+44
-25
No files matched your search
@@ -517,7 +517,7 @@ describe("AgentActivityPublisher", () => {
|
||||
});
|
||||
|
||||
it.effect(
|
||||
"does not build Live Activity aggregates for links with Live Activities disabled",
|
||||
"delivers notifications without querying activity rows when Live Activities are disabled",
|
||||
() => {
|
||||
const notificationState: RelayAgentActivityState = {
|
||||
...state,
|
||||
@@ -549,13 +549,7 @@ describe("AgentActivityPublisher", () => {
|
||||
AgentActivityRows.AgentActivityRows,
|
||||
makeAgentActivityRows({
|
||||
listForUser: () =>
|
||||
Effect.succeed([
|
||||
{
|
||||
...state,
|
||||
environmentId: "other-env" as RelayAgentActivityState["environmentId"],
|
||||
threadId: "other-thread" as RelayAgentActivityState["threadId"],
|
||||
},
|
||||
]),
|
||||
Effect.die("Notification-only delivery must not read rows"),
|
||||
}),
|
||||
),
|
||||
Layer.succeed(
|
||||
|
||||
@@ -58,7 +58,9 @@ export const make = Effect.gen(function* () {
|
||||
readonly state: RelayAgentActivityState | null;
|
||||
readonly nowMs: number;
|
||||
}) {
|
||||
const activeStates = yield* rows.listForUser({ userId: input.deliveryUser.userId });
|
||||
const activeStates = input.deliveryUser.liveActivitiesEnabled
|
||||
? yield* rows.listForUser({ userId: input.deliveryUser.userId })
|
||||
: [];
|
||||
const liveActivityAggregate = input.deliveryUser.liveActivitiesEnabled
|
||||
? makeAggregateState({
|
||||
activeStates,
|
||||
|
||||
@@ -19,6 +19,42 @@ const state: RelayAgentActivityState = {
|
||||
};
|
||||
|
||||
describe("AgentActivityRows", () => {
|
||||
it.effect("validates database JSON objects and ignores invalid activity rows", () => {
|
||||
const queryRows = [
|
||||
{ stateJson: null },
|
||||
{ stateJson: JSON.stringify(state) },
|
||||
{ stateJson: { ...state, phase: "invalid" } },
|
||||
{ stateJson: { ...state, unrelated: "ignored" } },
|
||||
];
|
||||
const db = {
|
||||
select: () => ({
|
||||
from: () => ({
|
||||
innerJoin: () => ({
|
||||
where: () => ({
|
||||
orderBy: () => Effect.succeed(queryRows),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
} as unknown as RelayDb.RelayDb["Service"];
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const rows = yield* AgentActivityRows.AgentActivityRows;
|
||||
expect(yield* rows.listForUser({ userId: "user-2" })).toEqual([state]);
|
||||
expect(
|
||||
yield* rows.getForUserThread({
|
||||
userId: "user-2",
|
||||
environmentId: "env-1",
|
||||
threadId: "thread-1",
|
||||
}),
|
||||
).toEqual(state);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
AgentActivityRows.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, db))),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("preserves activity context on persistence failures", () => {
|
||||
const cause = new Error("database unavailable");
|
||||
const failingDb = {
|
||||
|
||||
@@ -92,15 +92,12 @@ export class AgentActivityRows extends Context.Service<
|
||||
>()("t3code-relay/agentActivity/AgentActivityRows") {}
|
||||
|
||||
const decodeJsonString = Schema.decodeEffect(Schema.fromJsonString(Schema.Unknown));
|
||||
const encodeJsonValue = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
|
||||
|
||||
const encodeRelayAgentActivityStateJson = Schema.encodeEffect(
|
||||
Schema.fromJsonString(RelayAgentActivityStateSchema),
|
||||
);
|
||||
|
||||
const decodeRelayAgentActivityStateJson = Schema.decodeUnknownOption(
|
||||
Schema.fromJsonString(RelayAgentActivityStateSchema),
|
||||
);
|
||||
const decodeRelayAgentActivityState = Schema.decodeUnknownOption(RelayAgentActivityStateSchema);
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const db = yield* RelayDb.RelayDb;
|
||||
@@ -229,13 +226,8 @@ export const make = Effect.gen(function* () {
|
||||
)
|
||||
.orderBy(desc(relayAgentActivityRows.updatedAt))
|
||||
.pipe(
|
||||
Effect.flatMap((rows) =>
|
||||
Effect.forEach(rows, (row) => encodeJsonValue(row.stateJson), {
|
||||
concurrency: "unbounded",
|
||||
}),
|
||||
),
|
||||
Effect.map((rows) =>
|
||||
rows.flatMap((row) => Option.toArray(decodeRelayAgentActivityStateJson(row))),
|
||||
rows.flatMap((row) => Option.toArray(decodeRelayAgentActivityState(row.stateJson))),
|
||||
),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
@@ -271,14 +263,9 @@ export const make = Effect.gen(function* () {
|
||||
)
|
||||
.orderBy(desc(relayAgentActivityRows.updatedAt))
|
||||
.pipe(
|
||||
Effect.flatMap((rows) =>
|
||||
Effect.forEach(rows, (row) => encodeJsonValue(row.stateJson), {
|
||||
concurrency: "unbounded",
|
||||
}),
|
||||
),
|
||||
Effect.map((rows) => {
|
||||
for (const row of rows) {
|
||||
const decoded = decodeRelayAgentActivityStateJson(row);
|
||||
const decoded = decodeRelayAgentActivityState(row.stateJson);
|
||||
if (Option.isSome(decoded)) {
|
||||
return decoded.value;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user