-
Notifications
You must be signed in to change notification settings - Fork 0
Refresh Postgres connection when lost #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
IamJeffG
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar enough with Nuxt to be certain if my comments are correct, but consider them a start to a conversation.
Btw: I had been rebooting the db yesterday, so I'm not surprised you've come up against this bug. Thanks for finding a fix!
| data.style = "mapbox"; | ||
| } | ||
|
|
||
| const db = await getDatabaseConnection(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I worry that multiple requests or threads are calling refreshDatabaseConnection and/or setupDatabaseConnection simultaneously, race conditions might occur on this db variable. (I could be wrong; is defineEventHandler called once per app, or once per request?)
You might look into connection pools:
import { Pool } from "pg";
const pool = new Pool(getConfig());
const db = await pool.connect();
Many threads/simultaneous requests can share this pool, and the pg package manages multiple clients and re-connections for you (I think) so you do not need to deal with reconnecting manually.
I have not used this in the javascript ecosystem but I know it's a very good practice from my time in python. Maybe look for a quick tutorial to better understand if this is the right choice for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In either case, last thing I'll say is, when the application shuts down, you may want to clean up database connection. If using a pool, that would be pool.end(), or with your current code that's db.end(). Does your current web framework let you hook in to run code on app shutdown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I agreed with you that pooling is an improvement here. #41
I have never seen anyone implement cleaning up a database connection in Nuxt.js, but it's certainly a good suggestion and possible via hooking into the Nitro close lifecycle. I wrote a ticket for it: https://github.com/ConservationMetrics/gc-shared-resources/issues/3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd assume that using the pg client's own connection pooling would (necessarily) handle clean up for you, so doing the one change may enable you to close both issues. Have not verified. Anyway, sounds good.
Goal
The goal here is to refresh a Postgres database connection when discovered to have been lost. Changes similar to ConservationMetrics/gc-explorer#82, only even more simplified as map-packer does not have a need to connect to two databases (configDb and db).