Skip to content

Commit

Permalink
ARCO-156: correctly release db resources
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Sep 17, 2024
1 parent f85b757 commit 386356d
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 7 deletions.
2 changes: 2 additions & 0 deletions internal/blocktx/store/postgresql/get_block_gaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (p *PostgreSQL) GetBlockGaps(ctx context.Context, blockHeightRange int) ([]
if err != nil {
return nil, err
}
defer rows.Close()

blockGaps := make([]*store.BlockGap, 0)
for rows.Next() {
var height uint64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (p *PostgreSQL) GetMinedTransactions(ctx context.Context, hashes []*chainha
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
var txHash []byte
Expand Down
4 changes: 2 additions & 2 deletions internal/blocktx/store/postgresql/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func (p *PostgreSQL) Close() error {
}

func (p *PostgreSQL) Ping(ctx context.Context) error {
_, err := p.db.QueryContext(ctx, "SELECT 1;")
r, err := p.db.QueryContext(ctx, "SELECT 1;")
if err != nil {
return err
}

return nil
return r.Close()
}
1 change: 1 addition & 0 deletions internal/blocktx/store/postgresql/register_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (p *PostgreSQL) RegisterTransactions(ctx context.Context, transactions []*b
if err != nil {
return nil, fmt.Errorf("failed to bulk insert transactions: %v", err)
}
defer rows.Close()

updatedTxs := make([]*chainhash.Hash, 0)
for rows.Next() {
Expand Down
1 change: 1 addition & 0 deletions internal/blocktx/store/postgresql/set_block_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (p *PostgreSQL) GetBlockHashesProcessingInProgress(ctx context.Context, pro
if err != nil {
return nil, err
}
defer rows.Close()

hashes := make([]*chainhash.Hash, 0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (p *PostgreSQL) UpsertBlockTransactions(ctx context.Context, blockId uint64
if err != nil {
return nil, fmt.Errorf("failed to execute transaction update query: %v", err)
}
defer rows.Close()

txIDs := make([]uint64, 0)
blockIDs := make([]uint64, 0)
Expand Down
14 changes: 9 additions & 5 deletions internal/metamorph/store/postgresql/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,14 @@ func (p *PostgreSQL) GetSeenOnNetwork(ctx context.Context, since time.Time, unti
}

res, err := getStoreDataFromRows(rows)
_ = rows.Close()

if err != nil {
if rollBackErr := tx.Rollback(); rollBackErr != nil {
return nil, errors.Join(err, fmt.Errorf("failed to rollback: %v", rollBackErr))
}
return nil, err
}
defer rows.Close()

err = tx.Commit()
if err != nil {
Expand Down Expand Up @@ -636,9 +637,10 @@ func (p *PostgreSQL) UpdateStatusBulk(ctx context.Context, updates []store.Updat
}
return nil, err
}
defer rows.Close()

res, err := getStoreDataFromRows(rows)
_ = rows.Close()

if err != nil {
if rollBackErr := tx.Rollback(); rollBackErr != nil {
return nil, errors.Join(err, fmt.Errorf("failed to rollback: %v", rollBackErr))
Expand Down Expand Up @@ -711,9 +713,9 @@ func (p *PostgreSQL) UpdateDoubleSpend(ctx context.Context, updates []store.Upda
}
return nil, err
}
defer rows.Close()

competingTxsData := getCompetingTxsFromRows(rows)
_ = rows.Close()

statuses := make([]metamorph_api.Status, len(updates))
competingTxs := make([]string, len(updates))
Expand Down Expand Up @@ -742,9 +744,9 @@ func (p *PostgreSQL) UpdateDoubleSpend(ctx context.Context, updates []store.Upda
}
return nil, err
}
defer rows.Close()

res, err := getStoreDataFromRows(rows)
_ = rows.Close()
if err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
return nil, errors.Join(err, fmt.Errorf("failed to rollback: %v", rollbackErr))
Expand Down Expand Up @@ -830,6 +832,7 @@ func (p *PostgreSQL) UpdateMined(ctx context.Context, txsBlocks []*blocktx_api.T
}

rejectedResponses := updateDoubleSpendRejected(ctx, rows, tx)
_ = rows.Close()

rows, err = tx.QueryContext(ctx, qBulkUpdate, metamorph_api.Status_MINED, p.now(), pq.Array(txHashes), pq.Array(blockHashes), pq.Array(blockHeights), pq.Array(merklePaths))
if err != nil {
Expand All @@ -838,9 +841,10 @@ func (p *PostgreSQL) UpdateMined(ctx context.Context, txsBlocks []*blocktx_api.T
}
return nil, err
}
defer rows.Close()

res, err := getStoreDataFromRows(rows)
_ = rows.Close()

if err != nil {
if rollBackErr := tx.Rollback(); rollBackErr != nil {
return nil, errors.Join(err, fmt.Errorf("failed to rollback: %v", rollBackErr))
Expand Down

0 comments on commit 386356d

Please sign in to comment.