diff --git a/.env.development b/.env.development index 5cde68a8..4aa4763f 100644 --- a/.env.development +++ b/.env.development @@ -92,6 +92,9 @@ OPENSEARCH_MODEL_ID= # prisma db url DATABASE_URL="postgresql://sn:password@db:5432/stackernews?schema=public" +DB_APP_CONNECTION_LIMIT=2 +DB_WORKER_CONNECTION_LIMIT=2 +DB_TRANSACTION_TIMEOUT=5000 # polling intervals NEXT_PUBLIC_FAST_POLL_INTERVAL=1000 diff --git a/.env.production b/.env.production index 72ab1011..ca5c5bb5 100644 --- a/.env.production +++ b/.env.production @@ -18,4 +18,7 @@ NEXT_PUBLIC_EXTRA_LONG_POLL_INTERVAL=300000 NEXT_PUBLIC_URL=https://stacker.news TOR_PROXY=http://127.0.0.1:7050/ PRISMA_SLOW_LOGS_MS=50 -GRAPHQL_SLOW_LOGS_MS=50 \ No newline at end of file +GRAPHQL_SLOW_LOGS_MS=50 +DB_APP_CONNECTION_LIMIT=4 +DB_WORKER_CONNECTION_LIMIT=2 +DB_TRANSACTION_TIMEOUT=10000 \ No newline at end of file diff --git a/api/models/index.js b/api/models/index.js index df098bda..d050e0a2 100644 --- a/api/models/index.js +++ b/api/models/index.js @@ -1,18 +1,12 @@ -import { PrismaClient } from '@prisma/client' +import createPrisma from '@/lib/create-prisma' const prisma = global.prisma || (() => { console.log('initing prisma') - const prisma = new PrismaClient({ - log: [{ level: 'query', emit: 'event' }, 'warn', 'error'] - }) - prisma.$on('query', (e) => { - if (process.env.PRISMA_SLOW_LOGS_MS && e.duration > process.env.PRISMA_SLOW_LOGS_MS) { - console.log('Query: ' + e.query) - console.log('Params: ' + e.params) - console.log('Duration: ' + e.duration + 'ms') + return createPrisma({ + connectionParams: { + connection_limit: process.env.DB_APP_CONNECTION_LIMIT } }) - return prisma })() if (process.env.NODE_ENV === 'development') global.prisma = prisma diff --git a/lib/create-prisma.js b/lib/create-prisma.js new file mode 100644 index 00000000..d5d3af27 --- /dev/null +++ b/lib/create-prisma.js @@ -0,0 +1,28 @@ +import { PrismaClient } from '@prisma/client' + +export default function newPrismaClient ({ options = {}, connectionParams = {} }) { + const url = new URL(process.env.DATABASE_URL) + for (const [key, value] of Object.entries(connectionParams)) { + if (value === undefined) continue + url.searchParams.set(key, value) + } + + const prisma = new PrismaClient({ + datasourceUrl: url.toString(), + ...options, + transactionOptions: { + timeout: process.env.DB_TRANSACTION_TIMEOUT ? parseInt(process.env.DB_TRANSACTION_TIMEOUT) : undefined, + ...options.transactionOptions + } + }) + + prisma.$on('query', (e) => { + if (process.env.PRISMA_SLOW_LOGS_MS && e.duration > process.env.PRISMA_SLOW_LOGS_MS) { + console.log('Query: ' + e.query) + console.log('Params: ' + e.params) + console.log('Duration: ' + e.duration + 'ms') + } + }) + + return prisma +} diff --git a/worker/earn.js b/worker/earn.js index ed98e489..52ef63dd 100644 --- a/worker/earn.js +++ b/worker/earn.js @@ -1,5 +1,5 @@ import { notifyEarner } from '@/lib/webPush.js' -import { PrismaClient } from '@prisma/client' +import createPrisma from '@/lib/create-prisma.js' import { proportions } from '@/lib/madness.js' import { SN_NO_REWARDS_IDS } from '@/lib/constants.js' @@ -7,7 +7,7 @@ const TOTAL_UPPER_BOUND_MSATS = 1_000_000_000 export async function earn ({ name }) { // grab a greedy connection - const models = new PrismaClient() + const models = createPrisma({ connectionParams: { connection_limit: 1 } }) try { // compute how much sn earned yesterday diff --git a/worker/index.js b/worker/index.js index c601ad04..738ac5a6 100644 --- a/worker/index.js +++ b/worker/index.js @@ -1,6 +1,6 @@ import PgBoss from 'pg-boss' import nextEnv from '@next/env' -import { PrismaClient } from '@prisma/client' +import createPrisma from '@/lib/create-prisma.js' import { autoDropBolt11s, checkInvoice, checkPendingDeposits, checkPendingWithdrawals, finalizeHodlInvoice, subscribeToWallet @@ -34,7 +34,9 @@ loadEnvConfig('.', process.env.NODE_ENV === 'development') async function work () { const boss = new PgBoss(process.env.DATABASE_URL) - const models = new PrismaClient() + const models = createPrisma({ + connectionParams: { connection_limit: process.env.DB_WORKER_CONNECTION_LIMIT } + }) const apollo = new ApolloClient({ link: new HttpLink({ diff --git a/worker/views.js b/worker/views.js index 99ce219d..d9acf252 100644 --- a/worker/views.js +++ b/worker/views.js @@ -1,4 +1,4 @@ -import { PrismaClient } from '@prisma/client' +import createPrisma from '@/lib/create-prisma.js' const viewPrefixes = ['reg_growth', 'spender_growth', 'item_growth', 'spending_growth', 'stackers_growth', 'stacking_growth', 'user_stats', 'sub_stats'] @@ -6,7 +6,7 @@ const viewPrefixes = ['reg_growth', 'spender_growth', 'item_growth', 'spending_g // this is intended to be run everyday after midnight CT export async function views ({ data: { period } = { period: 'days' } }) { // grab a greedy connection - const models = new PrismaClient() + const models = createPrisma({ connectionParams: { connection_limit: 1 } }) try { // these views are bespoke so we can't use the loop @@ -29,7 +29,7 @@ export async function views ({ data: { period } = { period: 'days' } }) { // this should be run regularly ... like, every 5 minutes export async function rankViews () { // grab a greedy connection - const models = new PrismaClient() + const models = createPrisma({ connectionParams: { connection_limit: 1 } }) try { for (const view of ['zap_rank_personal_view']) {