Skip to content

Commit 106814f

Browse files
committed
feat(server): cache pg-meta connections
1 parent 29b979a commit 106814f

16 files changed

+13052
-144
lines changed

package-lock.json

Lines changed: 12961 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"devDependencies": {
4545
"@types/crypto-js": "^3.1.47",
4646
"@types/jest": "^27.0.1",
47+
"@types/lru-cache": "^5.1.1",
4748
"@types/node": "^14.0.13",
4849
"@types/pg": "^7.14.3",
4950
"@types/pg-format": "^1.0.1",
@@ -53,6 +54,7 @@
5354
"fastify-cors": "^5.2.0",
5455
"fastify-swagger": "^4.7.0",
5556
"jest": "^27.1.0",
57+
"lru-cache": "^6.0.0",
5658
"npm-run-all": "^4.1.5",
5759
"pino-pretty": "^4.7.1",
5860
"pkg": "^4.4.8",

src/server/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@ const PG_META_DB_PASSWORD = process.env.PG_META_DB_PASSWORD || 'postgres'
1010
export const PG_CONNECTION = `postgres://${PG_META_DB_USER}:${PG_META_DB_PASSWORD}@${PG_META_DB_HOST}:${PG_META_DB_PORT}/${PG_META_DB_NAME}?sslmode=disable`
1111

1212
export const PG_META_EXPORT_DOCS = process.env.PG_META_EXPORT_DOCS === 'true' || false
13-
14-
export const DEFAULT_POOL_CONFIG = { max: 1 }

src/server/pgMetaCache.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import LruCache from 'lru-cache'
2+
import { PostgresMeta } from '../lib'
3+
4+
const cache = new LruCache<string, PostgresMeta>({
5+
max: 100,
6+
dispose: async (_, value) => {
7+
await value.end()
8+
},
9+
})
10+
11+
/**
12+
* Retrieve a PostgresMeta handle from the cache. If it doesn't exist, create
13+
* the handle and insert it into the cache.
14+
*/
15+
const get = (connectionString: string): PostgresMeta => {
16+
let pgMeta: PostgresMeta | undefined = cache.get(connectionString)
17+
18+
if (pgMeta === undefined) {
19+
pgMeta = new PostgresMeta({ connectionString, max: 1 })
20+
cache.set(connectionString, pgMeta)
21+
}
22+
23+
return pgMeta
24+
}
25+
26+
export default {
27+
get,
28+
}

src/server/routes/columns.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { FastifyInstance } from 'fastify'
2-
import { PostgresMeta } from '../../lib'
3-
import { DEFAULT_POOL_CONFIG } from '../constants'
2+
import PgMetaCache from '../pgMetaCache'
43

54
export default async (fastify: FastifyInstance) => {
65
fastify.get<{
@@ -16,13 +15,12 @@ export default async (fastify: FastifyInstance) => {
1615
const limit = request.query.limit
1716
const offset = request.query.offset
1817

19-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
18+
const pgMeta = PgMetaCache.get(connectionString)
2019
const { data, error } = await pgMeta.columns.list({
2120
includeSystemSchemas,
2221
limit,
2322
offset,
2423
})
25-
await pgMeta.end()
2624
if (error) {
2725
request.log.error(JSON.stringify({ error, req: request.body }))
2826
reply.code(500)
@@ -40,9 +38,8 @@ export default async (fastify: FastifyInstance) => {
4038
}>('/:id(\\d+\\.\\d+)', async (request, reply) => {
4139
const connectionString = request.headers.pg
4240

43-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
41+
const pgMeta = PgMetaCache.get(connectionString)
4442
const { data, error } = await pgMeta.columns.retrieve({ id: request.params.id })
45-
await pgMeta.end()
4643
if (error) {
4744
request.log.error(JSON.stringify({ error, req: request.body }))
4845
reply.code(400)
@@ -59,9 +56,8 @@ export default async (fastify: FastifyInstance) => {
5956
}>('/', async (request, reply) => {
6057
const connectionString = request.headers.pg
6158

62-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
59+
const pgMeta = PgMetaCache.get(connectionString)
6360
const { data, error } = await pgMeta.columns.create(request.body)
64-
await pgMeta.end()
6561
if (error) {
6662
request.log.error(JSON.stringify({ error, req: request.body }))
6763
reply.code(400)
@@ -81,9 +77,8 @@ export default async (fastify: FastifyInstance) => {
8177
}>('/:id(\\d+\\.\\d+)', async (request, reply) => {
8278
const connectionString = request.headers.pg
8379

84-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
80+
const pgMeta = PgMetaCache.get(connectionString)
8581
const { data, error } = await pgMeta.columns.update(request.params.id, request.body)
86-
await pgMeta.end()
8782
if (error) {
8883
request.log.error(JSON.stringify({ error, req: request.body }))
8984
reply.code(400)
@@ -105,9 +100,8 @@ export default async (fastify: FastifyInstance) => {
105100
}>('/:id(\\d+\\.\\d+)', async (request, reply) => {
106101
const connectionString = request.headers.pg
107102

108-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
103+
const pgMeta = PgMetaCache.get(connectionString)
109104
const { data, error } = await pgMeta.columns.remove(request.params.id)
110-
await pgMeta.end()
111105
if (error) {
112106
request.log.error(JSON.stringify({ error, req: request.body }))
113107
reply.code(400)

src/server/routes/config.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { FastifyInstance } from 'fastify'
2-
import { PostgresMeta } from '../../lib'
3-
import { DEFAULT_POOL_CONFIG } from '../constants'
2+
import PgMetaCache from '../pgMetaCache'
43

54
export default async (fastify: FastifyInstance) => {
65
fastify.get<{
@@ -14,9 +13,8 @@ export default async (fastify: FastifyInstance) => {
1413
const limit = request.query.limit
1514
const offset = request.query.offset
1615

17-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
16+
const pgMeta = PgMetaCache.get(connectionString)
1817
const { data, error } = await pgMeta.config.list({ limit, offset })
19-
await pgMeta.end()
2018
if (error) {
2119
request.log.error(JSON.stringify({ error, req: request.body }))
2220
reply.code(500)
@@ -31,9 +29,8 @@ export default async (fastify: FastifyInstance) => {
3129
}>('/version', async (request, reply) => {
3230
const connectionString = request.headers.pg
3331

34-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
32+
const pgMeta = PgMetaCache.get(connectionString)
3533
const { data, error } = await pgMeta.version.retrieve()
36-
await pgMeta.end()
3734
if (error) {
3835
request.log.error(JSON.stringify({ error, req: request.body }))
3936
reply.code(500)

src/server/routes/extensions.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { FastifyInstance } from 'fastify'
2-
import { PostgresMeta } from '../../lib'
3-
import { DEFAULT_POOL_CONFIG } from '../constants'
2+
import PgMetaCache from '../pgMetaCache'
43

54
export default async (fastify: FastifyInstance) => {
65
fastify.get<{
@@ -14,9 +13,8 @@ export default async (fastify: FastifyInstance) => {
1413
const limit = request.query.limit
1514
const offset = request.query.offset
1615

17-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
16+
const pgMeta = PgMetaCache.get(connectionString)
1817
const { data, error } = await pgMeta.extensions.list({ limit, offset })
19-
await pgMeta.end()
2018
if (error) {
2119
request.log.error(JSON.stringify({ error, req: request.body }))
2220
reply.code(500)
@@ -34,9 +32,8 @@ export default async (fastify: FastifyInstance) => {
3432
}>('/:name', async (request, reply) => {
3533
const connectionString = request.headers.pg
3634

37-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
35+
const pgMeta = PgMetaCache.get(connectionString)
3836
const { data, error } = await pgMeta.extensions.retrieve({ name: request.params.name })
39-
await pgMeta.end()
4037
if (error) {
4138
request.log.error(JSON.stringify({ error, req: request.body }))
4239
reply.code(404)
@@ -52,9 +49,8 @@ export default async (fastify: FastifyInstance) => {
5249
}>('/', async (request, reply) => {
5350
const connectionString = request.headers.pg
5451

55-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
52+
const pgMeta = PgMetaCache.get(connectionString)
5653
const { data, error } = await pgMeta.extensions.create(request.body)
57-
await pgMeta.end()
5854
if (error) {
5955
request.log.error(JSON.stringify({ error, req: request.body }))
6056
reply.code(400)
@@ -73,9 +69,8 @@ export default async (fastify: FastifyInstance) => {
7369
}>('/:name', async (request, reply) => {
7470
const connectionString = request.headers.pg
7571

76-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
72+
const pgMeta = PgMetaCache.get(connectionString)
7773
const { data, error } = await pgMeta.extensions.update(request.params.name, request.body)
78-
await pgMeta.end()
7974
if (error) {
8075
request.log.error(JSON.stringify({ error, req: request.body }))
8176
reply.code(400)
@@ -98,9 +93,8 @@ export default async (fastify: FastifyInstance) => {
9893
const connectionString = request.headers.pg
9994
const cascade = request.query.cascade === 'true'
10095

101-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
96+
const pgMeta = PgMetaCache.get(connectionString)
10297
const { data, error } = await pgMeta.extensions.remove(request.params.name, { cascade })
103-
await pgMeta.end()
10498
if (error) {
10599
request.log.error(JSON.stringify({ error, req: request.body }))
106100
reply.code(400)

src/server/routes/functions.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { FastifyInstance } from 'fastify'
2-
import { PostgresMeta } from '../../lib'
3-
import { DEFAULT_POOL_CONFIG } from '../constants'
2+
import PgMetaCache from '../pgMetaCache'
43

54
export default async (fastify: FastifyInstance) => {
65
fastify.get<{
@@ -16,9 +15,8 @@ export default async (fastify: FastifyInstance) => {
1615
const limit = request.query.limit
1716
const offset = request.query.offset
1817

19-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
18+
const pgMeta = PgMetaCache.get(connectionString)
2019
const { data, error } = await pgMeta.functions.list({ includeSystemSchemas, limit, offset })
21-
await pgMeta.end()
2220
if (error) {
2321
request.log.error(JSON.stringify({ error, req: request.body }))
2422
reply.code(500)
@@ -37,9 +35,8 @@ export default async (fastify: FastifyInstance) => {
3735
const connectionString = request.headers.pg
3836
const id = Number(request.params.id)
3937

40-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
38+
const pgMeta = PgMetaCache.get(connectionString)
4139
const { data, error } = await pgMeta.functions.retrieve({ id })
42-
await pgMeta.end()
4340
if (error) {
4441
request.log.error(JSON.stringify({ error, req: request.body }))
4542
reply.code(404)
@@ -55,9 +52,8 @@ export default async (fastify: FastifyInstance) => {
5552
}>('/', async (request, reply) => {
5653
const connectionString = request.headers.pg
5754

58-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
55+
const pgMeta = PgMetaCache.get(connectionString)
5956
const { data, error } = await pgMeta.functions.create(request.body)
60-
await pgMeta.end()
6157
if (error) {
6258
request.log.error(JSON.stringify({ error, req: request.body }))
6359
reply.code(400)
@@ -76,9 +72,8 @@ export default async (fastify: FastifyInstance) => {
7672
const connectionString = request.headers.pg
7773
const id = Number(request.params.id)
7874

79-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
75+
const pgMeta = PgMetaCache.get(connectionString)
8076
const { data, error } = await pgMeta.functions.update(id, request.body)
81-
await pgMeta.end()
8277
if (error) {
8378
request.log.error(JSON.stringify({ error, req: request.body }))
8479
reply.code(400)
@@ -97,9 +92,8 @@ export default async (fastify: FastifyInstance) => {
9792
const connectionString = request.headers.pg
9893
const id = Number(request.params.id)
9994

100-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
95+
const pgMeta = PgMetaCache.get(connectionString)
10196
const { data, error } = await pgMeta.functions.remove(id)
102-
await pgMeta.end()
10397
if (error) {
10498
request.log.error(JSON.stringify({ error, req: request.body }))
10599
reply.code(400)

src/server/routes/policies.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { FastifyInstance } from 'fastify'
2-
import { PostgresMeta } from '../../lib'
3-
import { DEFAULT_POOL_CONFIG } from '../constants'
2+
import PgMetaCache from '../pgMetaCache'
43

54
export default async (fastify: FastifyInstance) => {
65
fastify.get<{
@@ -16,9 +15,8 @@ export default async (fastify: FastifyInstance) => {
1615
const limit = request.query.limit
1716
const offset = request.query.offset
1817

19-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
18+
const pgMeta = PgMetaCache.get(connectionString)
2019
const { data, error } = await pgMeta.policies.list({ includeSystemSchemas, limit, offset })
21-
await pgMeta.end()
2220
if (error) {
2321
request.log.error(JSON.stringify({ error, req: request.body }))
2422
reply.code(500)
@@ -37,9 +35,8 @@ export default async (fastify: FastifyInstance) => {
3735
const connectionString = request.headers.pg
3836
const id = Number(request.params.id)
3937

40-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
38+
const pgMeta = PgMetaCache.get(connectionString)
4139
const { data, error } = await pgMeta.policies.retrieve({ id })
42-
await pgMeta.end()
4340
if (error) {
4441
request.log.error(JSON.stringify({ error, req: request.body }))
4542
reply.code(404)
@@ -55,9 +52,8 @@ export default async (fastify: FastifyInstance) => {
5552
}>('/', async (request, reply) => {
5653
const connectionString = request.headers.pg
5754

58-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
55+
const pgMeta = PgMetaCache.get(connectionString)
5956
const { data, error } = await pgMeta.policies.create(request.body)
60-
await pgMeta.end()
6157
if (error) {
6258
request.log.error(JSON.stringify({ error, req: request.body }))
6359
reply.code(400)
@@ -77,9 +73,8 @@ export default async (fastify: FastifyInstance) => {
7773
const connectionString = request.headers.pg
7874
const id = Number(request.params.id)
7975

80-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
76+
const pgMeta = PgMetaCache.get(connectionString)
8177
const { data, error } = await pgMeta.policies.update(id, request.body)
82-
await pgMeta.end()
8378
if (error) {
8479
request.log.error(JSON.stringify({ error, req: request.body }))
8580
reply.code(400)
@@ -99,9 +94,8 @@ export default async (fastify: FastifyInstance) => {
9994
const connectionString = request.headers.pg
10095
const id = Number(request.params.id)
10196

102-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
97+
const pgMeta = PgMetaCache.get(connectionString)
10398
const { data, error } = await pgMeta.policies.remove(id)
104-
await pgMeta.end()
10599
if (error) {
106100
request.log.error(JSON.stringify({ error, req: request.body }))
107101
reply.code(400)

src/server/routes/publications.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { FastifyInstance } from 'fastify'
2-
import { PostgresMeta } from '../../lib'
3-
import { DEFAULT_POOL_CONFIG } from '../constants'
2+
import PgMetaCache from '../pgMetaCache'
43

54
export default async (fastify: FastifyInstance) => {
65
fastify.get<{
@@ -14,9 +13,8 @@ export default async (fastify: FastifyInstance) => {
1413
const limit = request.query.limit
1514
const offset = request.query.offset
1615

17-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
16+
const pgMeta = PgMetaCache.get(connectionString)
1817
const { data, error } = await pgMeta.publications.list({ limit, offset })
19-
await pgMeta.end()
2018
if (error) {
2119
request.log.error(JSON.stringify({ error, req: request.body }))
2220
reply.code(500)
@@ -35,9 +33,8 @@ export default async (fastify: FastifyInstance) => {
3533
const connectionString = request.headers.pg
3634
const id = Number(request.params.id)
3735

38-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
36+
const pgMeta = PgMetaCache.get(connectionString)
3937
const { data, error } = await pgMeta.publications.retrieve({ id })
40-
await pgMeta.end()
4138
if (error) {
4239
request.log.error(JSON.stringify({ error, req: request.body }))
4340
reply.code(404)
@@ -53,9 +50,8 @@ export default async (fastify: FastifyInstance) => {
5350
}>('/', async (request, reply) => {
5451
const connectionString = request.headers.pg
5552

56-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
53+
const pgMeta = PgMetaCache.get(connectionString)
5754
const { data, error } = await pgMeta.publications.create(request.body)
58-
await pgMeta.end()
5955
if (error) {
6056
request.log.error(JSON.stringify({ error, req: request.body }))
6157
reply.code(400)
@@ -75,9 +71,8 @@ export default async (fastify: FastifyInstance) => {
7571
const connectionString = request.headers.pg
7672
const id = Number(request.params.id)
7773

78-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
74+
const pgMeta = PgMetaCache.get(connectionString)
7975
const { data, error } = await pgMeta.publications.update(id, request.body)
80-
await pgMeta.end()
8176
if (error) {
8277
request.log.error(JSON.stringify({ error, req: request.body }))
8378
reply.code(400)
@@ -97,9 +92,8 @@ export default async (fastify: FastifyInstance) => {
9792
const connectionString = request.headers.pg
9893
const id = Number(request.params.id)
9994

100-
const pgMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
95+
const pgMeta = PgMetaCache.get(connectionString)
10196
const { data, error } = await pgMeta.publications.remove(id)
102-
await pgMeta.end()
10397
if (error) {
10498
request.log.error(JSON.stringify({ error, req: request.body }))
10599
reply.code(400)

0 commit comments

Comments
 (0)