allow db connection limit and timeout configuration
This commit is contained in:
parent
dd4806c1a3
commit
c6581b2cb1
|
@ -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
|
||||
|
|
|
@ -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
|
||||
GRAPHQL_SLOW_LOGS_MS=50
|
||||
DB_APP_CONNECTION_LIMIT=4
|
||||
DB_WORKER_CONNECTION_LIMIT=2
|
||||
DB_TRANSACTION_TIMEOUT=10000
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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({
|
||||
|
|
|
@ -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']) {
|
||||
|
|
Loading…
Reference in New Issue