@@ -2,8 +2,8 @@ import '../lib/instrument.js'
2
2
import http from 'node:http'
3
3
import { once } from 'node:events'
4
4
import { createHandler } from '../index.js'
5
- import Redis from 'ioredis '
6
- import Redlock from 'redlock '
5
+ import pg from 'pg '
6
+ import { migrate } from '../migrations/index.js '
7
7
8
8
const {
9
9
PORT : port = 3000 ,
@@ -15,7 +15,7 @@ const {
15
15
'0x646ac6F1941CAb0ce3fE1368e9AD30364a9F51dA' , // Miroslav
16
16
'0x3ee4A552b1a6519A266AEFb0514633F289FF2A9F' // Julian
17
17
] . join ( ',' ) ,
18
- REDIS_URL : redisUrl = 'redis://localhost:6379'
18
+ DATABASE_URL
19
19
} = process . env
20
20
21
21
const logger = {
@@ -24,17 +24,28 @@ const logger = {
24
24
request : [ '1' , 'true' ] . includes ( requestLogging ) ? console . info : ( ) => { }
25
25
}
26
26
27
- const redisUrlParsed = new URL ( redisUrl )
28
- const redis = new Redis ( {
29
- host : redisUrlParsed . hostname ,
30
- port : redisUrlParsed . port ,
31
- username : redisUrlParsed . username ,
32
- password : redisUrlParsed . password ,
33
- family : 6 // required for upstash
27
+ const pgPool = new pg . Pool ( {
28
+ connectionString : DATABASE_URL ,
29
+ // allow the pool to close all connections and become empty
30
+ min : 0 ,
31
+ // this values should correlate with service concurrency hard_limit configured in fly.toml
32
+ // and must take into account the connection limit of our PG server, see
33
+ // https://fly.io/docs/postgres/managing/configuration-tuning/
34
+ max : 100 ,
35
+ // close connections that haven't been used for one second
36
+ idleTimeoutMillis : 1000 ,
37
+ // automatically close connections older than 60 seconds
38
+ maxLifetimeSeconds : 60
34
39
} )
35
- const redlock = new Redlock ( [ redis ] )
40
+ pgPool . on ( 'error' , err => {
41
+ // Prevent crashing the process on idle client errors, the pool will recover
42
+ // itself. If all connections are lost, the process will still crash.
43
+ // https://github.com/brianc/node-postgres/issues/1324#issuecomment-308778405
44
+ console . error ( 'An idle client has experienced an error' , err . stack )
45
+ } )
46
+ await migrate ( pgPool )
36
47
37
- const handler = await createHandler ( { logger, redis , redlock , signerAddresses } )
48
+ const handler = await createHandler ( { logger, pgPool , signerAddresses } )
38
49
const server = http . createServer ( handler )
39
50
server . listen ( port , host )
40
51
await once ( server , 'listening' )
0 commit comments