mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: batch connection logs to avoid DB lock contention (#23727)
- Running 30k connections was generating a ton of lock contention in the DB
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/ed25519"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -144,10 +145,11 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
|
||||
}
|
||||
|
||||
if options.ConnectionLogger == nil {
|
||||
options.ConnectionLogger = connectionlog.NewConnectionLogger(
|
||||
connectionlog.NewDBBackend(options.Database),
|
||||
connLogger := connectionlog.New(
|
||||
connectionlog.NewDBBatcher(ctx, options.Database, options.Logger),
|
||||
connectionlog.NewSlogBackend(options.Logger),
|
||||
)
|
||||
options.ConnectionLogger = connLogger
|
||||
}
|
||||
|
||||
meshTLSConfig, err := replicasync.CreateDERPMeshTLSConfig(options.AccessURL.Hostname(), options.TLSCertificates)
|
||||
@@ -822,6 +824,12 @@ func (api *API) Close() error {
|
||||
api.Options.CheckInactiveUsersCancelFunc()
|
||||
}
|
||||
|
||||
// Close the connection logger to flush any remaining batched
|
||||
// entries before shutting down the database connection.
|
||||
if cl, ok := api.Options.ConnectionLogger.(io.Closer); ok {
|
||||
_ = cl.Close()
|
||||
}
|
||||
|
||||
return api.AGPL.Close()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user