Skip to content

Conversation

@rudokemper
Copy link
Member

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).

@rudokemper rudokemper merged commit f3191a8 into main Dec 4, 2024
1 check passed
@rudokemper rudokemper deleted the refresh-postgres-connection-when-lost branch December 4, 2024 13:31
Copy link
Contributor

@IamJeffG IamJeffG left a 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();
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Member Author

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

Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants