lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
8.89k stars 906 forks source link

ssl: reuse *tls.Config if connection settings are identical #1033

Open kevinburke1 opened 3 years ago

kevinburke1 commented 3 years ago

Previously, we would reload and re-parse a certificate from disk every single time we initialized a connection and the sslrootcert setting was enabled. This results in a lot of allocations that can be avoided.

Instead, save the *tls.Config for a given configuration hash, and reuse it when we see it again.

Fixes #1032.

jmunson commented 2 years ago

Unless I'm missing something, this looks like it would result in a stale certificate staying in memory until your program is restarted. I don't know if it would be appropriate to register a SIGHUP handler in a library to drop the cache, but that would be preferable to needing to restart the service.

Or maybe we could still stat() the file on connect time and re-parse if mtime is newer than what we have cached?