Skip to content

Commit

Permalink
Don't create new instance of postgres connection per each query (#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave authored Jun 23, 2021
1 parent fc8c49d commit c288c39
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ const connect = async (
secretsTableName = secretsTableName ?? 'secrets'
subscribersTableName = subscribersTableName ?? 'subscribers'

const connection = new Postgres({
keepAlive: false,
connectionTimeoutMillis: 45000,
idle_in_transaction_session_timeout: 45000,
query_timeout: 45000,
statement_timeout: 45000,
...connectionOptions,
})

await connection.connect()

Object.assign<
AdapterPoolPrimal,
Partial<PostgresqlAdapterPoolConnectedProps>
Expand All @@ -53,6 +64,7 @@ const connect = async (
executeStatement: executeStatement.bind(null, pool as AdapterPool),
escapeId,
escape,
connection,
})

if (pool.executeStatement != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { getLog } from './get-log'
import { AdapterPool } from './types'

const dispose = async (): Promise<any> => {
const dispose = async ({ connection }: AdapterPool): Promise<any> => {
const log = getLog(`dispose`)
log.debug(`disposing the adapter`)
log.debug(`disconnecting the event store adapter`)
await connection.end()
log.debug(`event store adapter disconnected`)
}

export default dispose
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@ import { AdapterPool } from './types'
const executeStatement = async (pool: AdapterPool, sql: any): Promise<any> => {
const errors: any[] = []
let rows = null
const connection = new pool.Postgres({
keepAlive: false,
connectionTimeoutMillis: 45000,
idle_in_transaction_session_timeout: 45000,
query_timeout: 45000,
statement_timeout: 45000,
...pool.connectionOptions,
})

try {
await connection.connect()
const result = await connection.query(sql)
const result = await pool.connection.query(sql)

if (result != null && Array.isArray(result.rows)) {
rows = JSON.parse(JSON.stringify(result.rows))
Expand All @@ -24,8 +15,6 @@ const executeStatement = async (pool: AdapterPool, sql: any): Promise<any> => {
return rows
} catch (error) {
errors.push(error)
} finally {
await connection.end()
}

if (errors.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type PostgresqlAdapterPoolConnectedProps = AdapterPoolConnectedProps & {
executeStatement: (sql: string) => Promise<any[]>
escapeId: EscapeFunction
escape: EscapeFunction
connection: Postgres
}

export type PostgresqlAdapterConfig = AdapterConfig & {
Expand Down

0 comments on commit c288c39

Please sign in to comment.