Context:
I'm building a Mac app that needs to interface with a PostgreSQL database. I'd like to use Postgres-NIO. I understand that the library is designed for server-side use, where a request comes in, a connection to the database is opened, work is done, and the connection is then closed.
Question:
My app will obviously have many, many database queries over time. Can I KEEP a PostgresClient
object for long stretches of time (hours)? Or must I follow the server pattern where a client is opened, work performed, and then the client is closed until new work is ready?
The code below WORKS, but I'm not sure if I'll hit any downsides by keeping PostgresClient
alive for long periods (resource/memory limits, networking limits, etc):
final class DatabaseEngine
{
static let config = PostgresClient.Configuration(...) // truncated for brevity
private var client: PostgresClient? = nil
private var runTask: Task<Void, Never>? = nil
// Fire up the client so we can handle queries
func connect() async throws
{
client = PostgresClient(configuration: DatabaseEngine.config)
let immutableClient = client!
runTask = Task {
await immutableClient.run()
}
}
func fetchStuff() async throws
{
guard let client else {
return
}
let rows = try await client.query("SELECT * FROM some_table")
for try await (id, name, creationDate) in rows.decode((UUID, String, Date).self) {
...
}
}
// Stop the client. This likely wouldn't be called until the app is quit, potentially days later.
func disconnect() async
{
runTask?.cancel()
}
}
Additionally, is PostgresClient
, when used this way, going to recover automatically if the user's WiFi momentarily disconnects for a minute? Will the connection re-establish, or must I close the client and reconnect a new one?