From 9d6952105484ccae2c066eeb846f1b767d5226cc Mon Sep 17 00:00:00 2001 From: Victor Elias Date: Wed, 9 Oct 2024 22:43:42 +0100 Subject: [PATCH] stream-info-service: Avoid creating indexes and reduce pool size (#2321) Realized that every stream-info-service is trying to create all the DB tables and indexes on startup. Theoretically they return quickly as they already exist, but it still means we open up to 10 connections from each instance everytime a broadcaster pod starts (or the stream-info restarts). This is hopefully what's been causing connection issues with Postgres just now. --- packages/api/src/app/stream-info/parse-cli.ts | 11 +++++++ .../src/app/stream-info/stream-info-app.ts | 29 ++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/api/src/app/stream-info/parse-cli.ts b/packages/api/src/app/stream-info/parse-cli.ts index 22f2c5f1d..a45906c52 100644 --- a/packages/api/src/app/stream-info/parse-cli.ts +++ b/packages/api/src/app/stream-info/parse-cli.ts @@ -37,6 +37,17 @@ export function parseCli() { type: "string", demandOption: true, }, + "postgres-create-tables": { + describe: + "create tables and indexes on the database if they don't exist", + type: "boolean", + default: false, + }, + "postgres-conn-pool-size": { + describe: "size of the postgres connection pool", + type: "number", + default: 5, + }, }) .help() .parse(); diff --git a/packages/api/src/app/stream-info/stream-info-app.ts b/packages/api/src/app/stream-info/stream-info-app.ts index 5f6240ecb..2f9c02a9d 100644 --- a/packages/api/src/app/stream-info/stream-info-app.ts +++ b/packages/api/src/app/stream-info/stream-info-app.ts @@ -1,19 +1,19 @@ import express, { Router } from "express"; import "express-async-errors"; // it monkeypatches, i guess import morgan from "morgan"; -import { db } from "../../store"; -import { healthCheck } from "../../middleware"; -import logger from "../../logger"; -import { Stream } from "../../schema/types"; import fetch from "node-fetch"; import { hostname } from "os"; +import logger from "../../logger"; +import { healthCheck } from "../../middleware"; +import { Stream } from "../../schema/types"; +import { db } from "../../store"; +import { DBStream } from "../../store/stream-table"; import { - StatusResponse, MasterPlaylist, MasterPlaylistDictionary, + StatusResponse, } from "./livepeer-types"; -import { DBStream } from "../../store/stream-table"; import { CliArgs } from "./parse-cli"; const pollInterval = 2 * 1000; // 2s @@ -377,9 +377,22 @@ class statusPoller { } export default async function makeApp(params: CliArgs) { - const { port, postgresUrl, ownRegion, listen = true, broadcaster } = params; + const { + port, + postgresUrl, + ownRegion, + listen = true, + broadcaster, + postgresCreateTables, + postgresConnPoolSize, + } = params; // Storage init - await db.start({ postgresUrl, appName: "stream-info" }); + await db.start({ + postgresUrl, + appName: "stream-info", + createTablesOnDb: postgresCreateTables, + poolMaxSize: postgresConnPoolSize, + }); const { router } = await makeRouter(params); const app = express();