@@ -2,7 +2,6 @@ import pg from 'pg'
2
2
import { Knex , knex } from 'knex'
3
3
import { JwtPayload } from 'jsonwebtoken'
4
4
import { getConfig } from '../config'
5
- import { logger } from '../monitoring'
6
5
import { DbActiveConnection , DbActivePool } from '../monitoring/metrics'
7
6
8
7
// https://github.com/knex/knex/issues/387#issuecomment-51554522
@@ -30,6 +29,7 @@ export interface User {
30
29
}
31
30
32
31
export const connections = new Map < string , Knex > ( )
32
+ const searchPath = [ 'storage' , 'public' , 'extensions' ]
33
33
34
34
export class TenantConnection {
35
35
public readonly role : string
@@ -63,13 +63,13 @@ export class TenantConnection {
63
63
64
64
knexPool = knex ( {
65
65
client : 'pg' ,
66
- searchPath : [ 'public' , 'storage' , 'extensions' ] ,
66
+ searchPath : isExternalPool ? undefined : searchPath ,
67
67
pool : {
68
68
min : 0 ,
69
69
max : isExternalPool ? 1 : options . maxConnections || databaseMaxConnections ,
70
70
propagateCreateError : false ,
71
71
acquireTimeoutMillis : databaseConnectionTimeout ,
72
- idleTimeoutMillis : isExternalPool ? 100 : undefined ,
72
+ idleTimeoutMillis : isExternalPool ? 100 : databaseFreePoolAfterInactivity ,
73
73
reapIntervalMillis : isExternalPool ? 110 : undefined ,
74
74
} ,
75
75
connection : connectionString ,
@@ -97,38 +97,12 @@ export class TenantConnection {
97
97
} )
98
98
99
99
if ( ! isExternalPool ) {
100
- let freePoolIntervalFn : NodeJS . Timeout | undefined
101
-
102
100
knexPool . client . pool . on ( 'poolDestroySuccess' , ( ) => {
103
- if ( freePoolIntervalFn ) {
104
- clearTimeout ( freePoolIntervalFn )
105
- }
106
-
107
101
if ( connections . get ( connectionString ) === knexPool ) {
108
102
connections . delete ( connectionString )
109
103
}
110
104
} )
111
105
112
- knexPool . client . pool . on ( 'stopReaping' , ( ) => {
113
- if ( freePoolIntervalFn ) {
114
- clearTimeout ( freePoolIntervalFn )
115
- }
116
-
117
- freePoolIntervalFn = setTimeout ( async ( ) => {
118
- connections . delete ( connectionString )
119
- knexPool ?. destroy ( ) . catch ( ( e ) => {
120
- logger . error ( e , 'Error destroying pool' )
121
- } )
122
- clearTimeout ( freePoolIntervalFn )
123
- } , databaseFreePoolAfterInactivity )
124
- } )
125
-
126
- knexPool . client . pool . on ( 'startReaping' , ( ) => {
127
- if ( freePoolIntervalFn ) {
128
- clearTimeout ( freePoolIntervalFn )
129
- freePoolIntervalFn = undefined
130
- }
131
- } )
132
106
connections . set ( connectionString , knexPool )
133
107
}
134
108
@@ -141,10 +115,16 @@ export class TenantConnection {
141
115
}
142
116
}
143
117
144
- transaction ( isolation ?: Knex . IsolationLevels , instance ?: Knex ) {
145
- return ( instance || this . pool ) . transactionProvider ( {
146
- isolationLevel : isolation ,
147
- } )
118
+ transaction ( instance ?: Knex ) : Knex . TransactionProvider {
119
+ return async ( ) => {
120
+ const pool = instance || this . pool
121
+ const tnx = await pool . transaction ( )
122
+
123
+ if ( ! instance ) {
124
+ await tnx . raw ( `set search_path to ${ searchPath . join ( ', ' ) } ` )
125
+ }
126
+ return tnx
127
+ }
148
128
}
149
129
150
130
asSuperUser ( ) {
0 commit comments