Skip to content

Commit

Permalink
feat: close database connections on stop
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney committed Nov 11, 2024
1 parent 27b3307 commit 47bb811
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
)

type Database interface {
Close() error
Metadata() *gorm.DB
Blob() *badger.DB
Transaction(bool) *Txn
Expand Down Expand Up @@ -61,6 +62,23 @@ func (b *BaseDatabase) Transaction(readWrite bool) *Txn {
return NewTxn(b, readWrite)
}

// Close cleans up the database connections
func (b *BaseDatabase) Close() error {
var err error
// Close metadata
sqlDB, sqlDBerr := b.metadata.DB()
if sqlDBerr != nil {
err = errors.Join(err, sqlDBerr)
} else {
metadataErr := sqlDB.Close()
err = errors.Join(err, metadataErr)
}
// Close blob
blobErr := b.blob.Close()
err = errors.Join(err, blobErr)
return err
}

func (b *BaseDatabase) init() error {
if b.logger == nil {
// Create logger to throw away logs
Expand Down
3 changes: 3 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func (n *Node) Stop() error {
func (n *Node) shutdown() error {
ctx := context.TODO()
var err error
// Shutdown ledger
err = errors.Join(err, n.ledgerState.Close())
// Call shutdown functions
for _, fn := range n.shutdownFuncs {
err = errors.Join(err, fn(ctx))
}
Expand Down
4 changes: 4 additions & 0 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func NewLedgerState(cfg LedgerStateConfig) (*LedgerState, error) {
return ls, nil
}

func (ls *LedgerState) Close() error {
return ls.db.Close()
}

func (ls *LedgerState) scheduleCleanupConsumedUtxos() {
ls.Lock()
defer ls.Unlock()
Expand Down

0 comments on commit 47bb811

Please sign in to comment.