mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-29 03:01:52 +01:00
kvdb/postgres: make global dbConns thread safe
In this commit, a mutex is added to guard access to the global dbConns variable in the kvdb/postgres package.
This commit is contained in:
parent
72dbc3dbb4
commit
c6abf585ee
@ -57,11 +57,21 @@ type db struct {
|
|||||||
// Enforce db implements the walletdb.DB interface.
|
// Enforce db implements the walletdb.DB interface.
|
||||||
var _ walletdb.DB = (*db)(nil)
|
var _ walletdb.DB = (*db)(nil)
|
||||||
|
|
||||||
// Global set of database connections.
|
var (
|
||||||
var dbConns *dbConnSet
|
// dbConns is a global set of database connections.
|
||||||
|
dbConns *dbConnSet
|
||||||
|
dbConnsMu sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
// Init initializes the global set of database connections.
|
// Init initializes the global set of database connections.
|
||||||
func Init(maxConnections int) {
|
func Init(maxConnections int) {
|
||||||
|
dbConnsMu.Lock()
|
||||||
|
defer dbConnsMu.Unlock()
|
||||||
|
|
||||||
|
if dbConns != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
dbConns = newDbConnSet(maxConnections)
|
dbConns = newDbConnSet(maxConnections)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,6 +80,9 @@ func Init(maxConnections int) {
|
|||||||
func newPostgresBackend(ctx context.Context, config *Config, prefix string) (
|
func newPostgresBackend(ctx context.Context, config *Config, prefix string) (
|
||||||
*db, error) {
|
*db, error) {
|
||||||
|
|
||||||
|
dbConnsMu.Lock()
|
||||||
|
defer dbConnsMu.Unlock()
|
||||||
|
|
||||||
if prefix == "" {
|
if prefix == "" {
|
||||||
return nil, errors.New("empty postgres prefix")
|
return nil, errors.New("empty postgres prefix")
|
||||||
}
|
}
|
||||||
@ -256,6 +269,9 @@ func (db *db) Copy(w io.Writer) error {
|
|||||||
// Close cleanly shuts down the database and syncs all data.
|
// Close cleanly shuts down the database and syncs all data.
|
||||||
// This function is part of the walletdb.Db interface implementation.
|
// This function is part of the walletdb.Db interface implementation.
|
||||||
func (db *db) Close() error {
|
func (db *db) Close() error {
|
||||||
|
dbConnsMu.Lock()
|
||||||
|
defer dbConnsMu.Unlock()
|
||||||
|
|
||||||
log.Infof("Closing database %v", db.prefix)
|
log.Infof("Closing database %v", db.prefix)
|
||||||
|
|
||||||
return dbConns.Close(db.cfg.Dsn)
|
return dbConns.Close(db.cfg.Dsn)
|
||||||
|
@ -19,7 +19,8 @@ type dbConnSet struct {
|
|||||||
dbConn map[string]*dbConn
|
dbConn map[string]*dbConn
|
||||||
maxConnections int
|
maxConnections int
|
||||||
|
|
||||||
sync.Mutex
|
// mu is used to guard access to the dbConn map.
|
||||||
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// newDbConnSet initializes a new set of connections.
|
// newDbConnSet initializes a new set of connections.
|
||||||
@ -33,8 +34,8 @@ func newDbConnSet(maxConnections int) *dbConnSet {
|
|||||||
// Open opens a new database connection. If a connection already exists for the
|
// Open opens a new database connection. If a connection already exists for the
|
||||||
// given dsn, the existing connection is returned.
|
// given dsn, the existing connection is returned.
|
||||||
func (d *dbConnSet) Open(dsn string) (*sql.DB, error) {
|
func (d *dbConnSet) Open(dsn string) (*sql.DB, error) {
|
||||||
d.Lock()
|
d.mu.Lock()
|
||||||
defer d.Unlock()
|
defer d.mu.Unlock()
|
||||||
|
|
||||||
if dbConn, ok := d.dbConn[dsn]; ok {
|
if dbConn, ok := d.dbConn[dsn]; ok {
|
||||||
dbConn.count++
|
dbConn.count++
|
||||||
@ -66,8 +67,8 @@ func (d *dbConnSet) Open(dsn string) (*sql.DB, error) {
|
|||||||
// Close closes the connection with the given dsn. If there are still other
|
// Close closes the connection with the given dsn. If there are still other
|
||||||
// users of the same connection, this function does nothing.
|
// users of the same connection, this function does nothing.
|
||||||
func (d *dbConnSet) Close(dsn string) error {
|
func (d *dbConnSet) Close(dsn string) error {
|
||||||
d.Lock()
|
d.mu.Lock()
|
||||||
defer d.Unlock()
|
defer d.mu.Unlock()
|
||||||
|
|
||||||
dbConn, ok := d.dbConn[dsn]
|
dbConn, ok := d.dbConn[dsn]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user