vault/helper
Christopher Swenson a65d9133a1
database: Avoid race condition in connection creation (#26147)
When creating database connections, there is a race
condition when multiple goroutines try to create the
connection at the same time. This happens, for
example, on leadership changes in a cluster.

Normally, the extra database connections are cleaned
up when this is detected. However, some database
implementations, notably Postgres, do not seem to
clean up in a timely manner, and can leak in these
scenarios.

To fix this, we create a global lock when creating
database connections to prevent multiple connections
from being created at the same time.

We also clean up the logic at the end so that
if (somehow) we ended up creating an additional
connection, we use the existing one rather than
the new one. This by itself would solve our
problem long-term, however, would still involve
many transient database connections being created
and immediately killed on leadership changes.

It's not ideal to have a single global lock for
database connection creation. Some potential
alternatives:

* a map of locks from the connection name to the lock.
  The biggest downside is the we probably will want to
  garbage collect this map so that we don't have an
  unbounded number of locks.
* a small pool of locks, where we hash the connection
  names to pick the lock. Using such a pool generally
  is a good way to introduce deadlock, but since we
  will only use it in a specific case, and the purpose
  is to improve performance for concurrent connection
  creation, this is probably acceptable.

Co-authored-by: Jason O'Donnell <2160810+jasonodonnell@users.noreply.github.com>
2024-03-26 16:58:07 +00:00
..
benchhelpers [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
builtinplugins add gosimport to make fmt and run it (#25383) 2024-02-13 14:07:02 -08:00
constants [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
dhutil add gosimport to make fmt and run it (#25383) 2024-02-13 14:07:02 -08:00
experiments Add experiment flag for import (#25205) 2024-02-02 14:18:43 -06:00
fairshare add gosimport to make fmt and run it (#25383) 2024-02-13 14:07:02 -08:00
flag-kv [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
flag-slice [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
forwarding chore: fix function names (#26087) 2024-03-22 12:43:01 -04:00
hostutil [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
identity lint: fix proto delta and simports (#25825) 2024-03-07 18:10:51 +00:00
locking [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
logging fix -log-file so that it uses the correct name and only adds timestamps on rotation (#24297) 2023-12-01 11:30:34 +00:00
metricsutil helper/metricsutil: Update usage of expfmt (#25947) 2024-03-14 10:13:45 +00:00
monitor [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
namespace [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
osutil [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
parseip [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
pgpkeys make-fmt (#24940) 2024-01-18 20:00:00 +00:00
pkcs7 add gosimport to make fmt and run it (#25383) 2024-02-13 14:07:02 -08:00
policies [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
proxyutil [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
random [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
storagepacker VAULT-20396 CE side changes for identity test-identified issue (#26072) 2024-03-21 11:16:35 -04:00
syncmap database: Avoid race condition in connection creation (#26147) 2024-03-26 16:58:07 +00:00
testhelpers VAULT-24013: Audit regression attempting to recover from panic (#25605) 2024-02-26 10:33:30 +00:00
timeutil chore: fix function names (#26087) 2024-03-22 12:43:01 -04:00
useragent [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00
versions [COMPLIANCE] License changes (#22290) 2023-08-10 18:14:03 -07:00