Skip to content

Commit

Permalink
remove transactionality from non-critical services and read-only oper…
Browse files Browse the repository at this point in the history
…ations (#2165)
  • Loading branch information
ichub authored Nov 12, 2024
1 parent 82b9f6f commit 5ac3788
Show file tree
Hide file tree
Showing 21 changed files with 822 additions and 901 deletions.
12 changes: 12 additions & 0 deletions apps/passport-server/src/database/sqlQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ export function sqlTransaction<T>(
);
}

export async function sqlQueryWithPool<T>(
pool: Pool,
func: (client: PoolClient) => Promise<T>
): Promise<T> {
const client = await pool.connect();
try {
return await func(client);
} finally {
client.release();
}
}

/**
* Executes a given function inside a transaction against the database, and
* traces its performance. Retries queries that fail due to a connection error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import path from "path";
import * as QRCode from "qrcode";
import urljoin from "url-join";
import { PipelineCheckinDB } from "../../database/queries/pipelineCheckinDB";
import { namedSqlTransaction, sqlTransaction } from "../../database/sqlQuery";
import { namedSqlTransaction, sqlQueryWithPool } from "../../database/sqlQuery";
import {
getAllGenericIssuanceHTTPQuery,
getAllGenericIssuanceQuery,
Expand Down Expand Up @@ -74,7 +74,7 @@ export function initGenericIssuanceRoutes(
*/
app.post("/generic-issuance/api/self", async (req, res) => {
checkExistsForRoute(genericIssuanceService);
const user = await sqlTransaction(context.dbPool, (client) =>
const user = await sqlQueryWithPool(context.dbPool, (client) =>
genericIssuanceService.authSession(client, req)
);

Expand Down Expand Up @@ -110,7 +110,7 @@ export function initGenericIssuanceRoutes(

const checkinDb = new PipelineCheckinDB();
const tickets = await pipeline.getAllTickets();
const checkins = await sqlTransaction(context.dbPool, (client) =>
const checkins = await sqlQueryWithPool(context.dbPool, (client) =>
checkinDb.getByPipelineId(client, pragueId)
);

Expand Down Expand Up @@ -499,7 +499,7 @@ export function initGenericIssuanceRoutes(
"/generic-issuance/api/fetch-pretix-products",
async (req: express.Request, res: express.Response) => {
checkExistsForRoute(genericIssuanceService);
const user = await sqlTransaction(context.dbPool, (client) =>
const user = await sqlQueryWithPool(context.dbPool, (client) =>
genericIssuanceService.authSession(client, req)
);
traceUser(user);
Expand Down
4 changes: 2 additions & 2 deletions apps/passport-server/src/routing/routes/miscRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { bigintToPseudonymNumber, emailToBigint } from "@pcd/util";
import express, { Request, Response } from "express";
import { kvGetByPrefix } from "../../database/queries/kv";
import { sqlTransaction } from "../../database/sqlQuery";
import { sqlQueryWithPool } from "../../database/sqlQuery";
import { ApplicationContext, GlobalServices } from "../../types";
import { logger } from "../../util/logger";

Expand All @@ -15,7 +15,7 @@ export function initMiscRoutes(
app.get(
"/misc/protocol-worlds-scoreboard",
async (req: Request, res: Response) => {
const scores = (await sqlTransaction(context.dbPool, (client) =>
const scores = (await sqlQueryWithPool(context.dbPool, (client) =>
kvGetByPrefix(client, "protocol_worlds_score:")
)) as Array<{ email: string; score: number }>;

Expand Down
8 changes: 4 additions & 4 deletions apps/passport-server/src/routing/routes/semaphoreRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
serializeSemaphoreGroup
} from "@pcd/semaphore-group-pcd";
import express, { Request, Response } from "express";
import { sqlTransaction } from "../../database/sqlQuery";
import { sqlQueryWithPool } from "../../database/sqlQuery";
import { ApplicationContext, GlobalServices } from "../../types";
import { logger } from "../../util/logger";
import { checkExistsForRoute } from "../../util/util";
Expand Down Expand Up @@ -40,7 +40,7 @@ export function initSemaphoreRoutes(
const groupId = checkUrlParam(req, "id");
const roothash = checkUrlParam(req, "root");

const historicGroupValid = await sqlTransaction(
const historicGroupValid = await sqlQueryWithPool(
context.dbPool,
(client) =>
semaphoreService.getHistoricSemaphoreGroupValid(
Expand Down Expand Up @@ -70,7 +70,7 @@ export function initSemaphoreRoutes(
"/semaphore/historic/:id/:root",
async (req: Request, res: Response) => {
checkExistsForRoute(semaphoreService);
const historicGroup = await sqlTransaction(context.dbPool, (client) =>
const historicGroup = await sqlQueryWithPool(context.dbPool, (client) =>
semaphoreService.getHistoricSemaphoreGroup(
client,
checkUrlParam(req, "id"),
Expand Down Expand Up @@ -99,7 +99,7 @@ export function initSemaphoreRoutes(
app.get("/semaphore/latest-root/:id", async (req: Request, res: Response) => {
checkExistsForRoute(semaphoreService);
const id = checkUrlParam(req, "id");
const latestGroups = await sqlTransaction(context.dbPool, (client) =>
const latestGroups = await sqlQueryWithPool(context.dbPool, (client) =>
semaphoreService.getLatestSemaphoreGroups(client)
);
const matchingGroup = latestGroups.find((g) => g.groupId.toString() === id);
Expand Down
68 changes: 29 additions & 39 deletions apps/passport-server/src/routing/routes/telegramRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AnonWebAppPayload, PayloadType } from "@pcd/passport-interface";
import express, { Request, Response } from "express";
import { namedSqlTransaction, sqlTransaction } from "../../database/sqlQuery";
import { sqlQueryWithPool } from "../../database/sqlQuery";
import { startTelegramService } from "../../services/telegramService";
import { ApplicationContext, GlobalServices } from "../../types";
import { logger } from "../../util/logger";
Expand Down Expand Up @@ -66,17 +66,14 @@ export function initTelegramRoutes(
if (!telegramService) {
throw new Error("Telegram service not initialized");
}
await namedSqlTransaction(
context.dbPool,
"/telegram/verify",
(client) =>
telegramService.handleVerification(
client,
proof,
parseInt(telegram_user_id),
telegram_chat_id,
telegram_username
)
await sqlQueryWithPool(context.dbPool, (client) =>
telegramService.handleVerification(
client,
proof,
parseInt(telegram_user_id),
telegram_chat_id,
telegram_username
)
);
logger(
`[TELEGRAM] Redirecting to telegram for user id ${telegram_user_id}` +
Expand Down Expand Up @@ -214,7 +211,7 @@ export function initTelegramRoutes(
}

case PayloadType.ReactData: {
const proofUrl = await sqlTransaction(context.dbPool, (client) =>
const proofUrl = await sqlQueryWithPool(context.dbPool, (client) =>
telegramService.handleRequestReactProofLink(client, anonPayload)
);
res.redirect(proofUrl);
Expand Down Expand Up @@ -248,21 +245,17 @@ export function initTelegramRoutes(
"nullifierHash field needs to be a string and be non-empty"
);
}
await namedSqlTransaction(
context.dbPool,
"/telegram/anonget/:nullifier",
async (client) => {
const messages = await telegramService.handleGetAnonMessages(
client,
nullifierHash
);
const totalKarma = await telegramService.handleGetAnonTotalKarma(
client,
nullifierHash
);
res.json({ messages, totalKarma });
}
);
await sqlQueryWithPool(context.dbPool, async (client) => {
const messages = await telegramService.handleGetAnonMessages(
client,
nullifierHash
);
const totalKarma = await telegramService.handleGetAnonTotalKarma(
client,
nullifierHash
);
res.json({ messages, totalKarma });
});
} catch (e) {
logger("[TELEGRAM] failed to get posts", e);
}
Expand All @@ -280,17 +273,14 @@ export function initTelegramRoutes(
throw new Error("Telegram service not initialized");
}

await namedSqlTransaction(
context.dbPool,
"/telegram/anonreact",
(client) =>
telegramService.handleReactAnonymousMessage(
client,
proof,
chatId,
anonMessageId,
reaction
)
await sqlQueryWithPool(context.dbPool, (client) =>
telegramService.handleReactAnonymousMessage(
client,
proof,
chatId,
anonMessageId,
reaction
)
);

res.setHeader("Content-Type", "text/html");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from "../../../../database/queries/pipelineAtomDB";
import { IPipelineConsumerDB } from "../../../../database/queries/pipelineConsumerDB";
import { IPipelineSemaphoreHistoryDB } from "../../../../database/queries/pipelineSemaphoreHistoryDB";
import { sqlTransaction } from "../../../../database/sqlQuery";
import { sqlQueryWithPool } from "../../../../database/sqlQuery";
import { ApplicationContext } from "../../../../types";
import { logger } from "../../../../util/logger";
import { setError, traced } from "../../../telemetryService";
Expand Down Expand Up @@ -110,7 +110,7 @@ export class CSVPipeline implements BasePipeline {
getSerializedLatestGroup: async (
groupId: string
): Promise<SerializedSemaphoreGroup | undefined> => {
return sqlTransaction(
return sqlQueryWithPool(
this.context.dbPool,
async (client) =>
this.semaphoreGroupProvider?.getSerializedLatestGroup(
Expand All @@ -122,7 +122,7 @@ export class CSVPipeline implements BasePipeline {
getLatestGroupRoot: async (
groupId: string
): Promise<string | undefined> => {
return sqlTransaction(
return sqlQueryWithPool(
this.context.dbPool,
async (client) =>
this.semaphoreGroupProvider?.getLatestGroupRoot(client, groupId)
Expand All @@ -132,7 +132,7 @@ export class CSVPipeline implements BasePipeline {
groupId: string,
rootHash: string
): Promise<SerializedSemaphoreGroup | undefined> => {
return sqlTransaction(
return sqlQueryWithPool(
this.context.dbPool,
async (client) =>
this.semaphoreGroupProvider?.getSerializedHistoricalGroup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from "../../../../database/queries/pipelineAtomDB";
import { IPipelineConsumerDB } from "../../../../database/queries/pipelineConsumerDB";
import { IPipelineSemaphoreHistoryDB } from "../../../../database/queries/pipelineSemaphoreHistoryDB";
import { sqlTransaction } from "../../../../database/sqlQuery";
import { sqlQueryWithPool } from "../../../../database/sqlQuery";
import { ApplicationContext } from "../../../../types";
import { logger } from "../../../../util/logger";
import { PersistentCacheService } from "../../../persistentCacheService";
Expand Down Expand Up @@ -133,7 +133,7 @@ export class CSVTicketPipeline implements BasePipeline {
getSerializedLatestGroup: async (
groupId: string
): Promise<SerializedSemaphoreGroup | undefined> => {
return sqlTransaction(
return sqlQueryWithPool(
this.context.dbPool,
async (client) =>
this.semaphoreGroupProvider?.getSerializedLatestGroup(
Expand All @@ -145,7 +145,7 @@ export class CSVTicketPipeline implements BasePipeline {
getLatestGroupRoot: async (
groupId: string
): Promise<string | undefined> => {
return sqlTransaction(
return sqlQueryWithPool(
this.context.dbPool,
async (client) =>
this.semaphoreGroupProvider?.getLatestGroupRoot(client, groupId)
Expand All @@ -155,7 +155,7 @@ export class CSVTicketPipeline implements BasePipeline {
groupId: string,
rootHash: string
): Promise<SerializedSemaphoreGroup | undefined> => {
return sqlTransaction(
return sqlQueryWithPool(
this.context.dbPool,
async (client) =>
this.semaphoreGroupProvider?.getSerializedHistoricalGroup(
Expand Down
Loading

0 comments on commit 5ac3788

Please sign in to comment.