kibae / pg-logical-replication

PostgreSQL Logical Replication client for node.js
https://www.npmjs.com/package/pg-logical-replication
MIT License
100 stars 22 forks source link

Connection interruption terminates the whole process, doesn't allow to be try/caught #34

Open rik-iso opened 4 months ago

rik-iso commented 4 months ago

I'm trying to make the code resilient to a Aurora RDS failover (which interrupts the connection to the master). I'm wrapping my await service.subscribe(plugin, REPLICATION_SLOT_NAME) in a try/catch, but when I fail over the whole process just ends:

/Users/rik/iso/Code/graph/node_modules/.pnpm/eventemitter2@6.4.9/node_modules/eventemitter2/lib/eventemitter2.js:1053
        throw arguments[1]; // Unhandled 'error' event
        ^

Error: Connection terminated unexpectedly
    at Connection.<anonymous> (/Users/rik/iso/Code/graph/node_modules/.pnpm/pg@8.11.5/node_modules/pg/lib/client.js:132:73)
    at Object.onceWrapper (node:events:628:28)
    at Connection.emit (node:events:514:28)
    at Connection.emit (node:domain:489:12)
    at Socket.<anonymous> (/Users/rik/iso/Code/graph/node_modules/.pnpm/pg@8.11.5/node_modules/pg/lib/connection.js:63:12)
    at Socket.emit (node:events:514:28)
    at Socket.emit (node:domain:489:12)
    at TCP.<anonymous> (node:net:323:12)

Node.js v18.17.1

This occurs if I use the example code in the README too.

rik-iso commented 4 months ago

Ok looks like adding

  service.on('error', (er) => {
    console.log(er)
  })

i.e. handling the error somehow fixes it. I guess this is an eventemitter2 thing. Would be worth documenting as the example in the README doesn't work to retry like it implies it does.