Skip to content

Commit

Permalink
feat: improved release of database resources. (#78)
Browse files Browse the repository at this point in the history
- Added sql.Rows.Close() calls when missing
- Allows the BlockRepository to be closed, and thus closing
  the underlying sql.DB connections.

Fixes #77
  • Loading branch information
ronoaldo authored Nov 30, 2023
1 parent 990b478 commit 254275b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
4 changes: 4 additions & 0 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ type BlockRepository interface {
// storage space if not done automatically by the backend.
Vacuum() error

// Count returns the total number of stored blocks in the map database.
Count() (int64, error)

// Close gracefully finishes the connection with the database backend.
Close() error
}

// NewBlockRepository initializes the connection with the appropriate database
Expand Down
4 changes: 4 additions & 0 deletions block/block_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,7 @@ func (r *postgresBlockRepository) Import(z *zip.Reader) error {

return nil
}

func (r *postgresBlockRepository) Close() error {
return r.db.Close()
}
10 changes: 5 additions & 5 deletions block/block_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func setupPostgress(t *testing.T) (block.BlockRepository, *sql.DB) {
}

func TestPostgresBlocksRepo(t *testing.T) {
blocks_repo, _ := setupPostgress(t)
testBlocksRepository(t, blocks_repo)
r, _ := setupPostgress(t)
testBlocksRepository(t, r)
}

func TestPostgresMaxConnections(t *testing.T) {
blocks_repo, db := setupPostgress(t)
r, db := setupPostgress(t)

var maxConnections int
row := db.QueryRow("show max_connections;")
Expand All @@ -43,14 +43,14 @@ func TestPostgresMaxConnections(t *testing.T) {
PosZ: 1,
Data: []byte("test"),
}
if err := blocks_repo.Update(&fakeBlock); err != nil {
if err := r.Update(&fakeBlock); err != nil {
t.Fatalf("Error setting up test case: %v", err)
}

// Run more than max_connections query operations in a loop
count := 0
for i := 0; i < maxConnections*2; i++ {
b, err := blocks_repo.GetByPos(1, 1, 1)
b, err := r.GetByPos(1, 1, 1)
count++
if b != nil && count%10 == 0 {
t.Logf("Executed %d operations. b=%v", count, b)
Expand Down
5 changes: 5 additions & 0 deletions block/block_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (repo *sqliteBlockRepository) GetByPos(x, y, z int) (*Block, error) {
if err != nil {
return nil, err
}
defer rows.Close()
if !rows.Next() {
return nil, nil
}
Expand Down Expand Up @@ -212,3 +213,7 @@ func (r *sqliteBlockRepository) Import(z *zip.Reader) error {

return nil
}

func (r *sqliteBlockRepository) Close() error {
return r.db.Close()
}
28 changes: 15 additions & 13 deletions block/block_sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,29 @@ func setupSqlite(t *testing.T) (block.BlockRepository, *sql.DB) {

func TestSqliteBlockRepo(t *testing.T) {
// open db
blocks_repo, _ := setupSqlite(t)
testBlocksRepository(t, blocks_repo)
r, _ := setupSqlite(t)
defer r.Close()
testBlocksRepository(t, r)
}

func TestSqliteIterator(t *testing.T) {
blocks_repo, _ := setupSqlite(t)
testBlocksRepositoryIterator(t, blocks_repo)
r, _ := setupSqlite(t)
defer r.Close()
testBlocksRepositoryIterator(t, r)
}

func TestSqliteIteratorErrorHandling(t *testing.T) {
blocks_repo, db := setupSqlite(t)
r, db := setupSqlite(t)
defer db.Close()
defer r.Close()

testIteratorErrorHandling(t, r, db, `UPDATE blocks SET pos = 18446744073709551615;`)
}

testIteratorErrorHandling(t, blocks_repo, db, `
UPDATE blocks SET pos = 18446744073709551615;
`)
func TestSqliteIteratorCloser(t *testing.T) {
r, _ := setupSqlite(t)
defer r.Close()
testIteratorClose(t, r)
}

func TestCoordToPlain(t *testing.T) {
Expand Down Expand Up @@ -72,8 +79,3 @@ func TestCoordToPlain(t *testing.T) {
}
}
}

func TestSqliteIteratorCloser(t *testing.T) {
r, _ := setupSqlite(t)
testIteratorClose(t, r)
}

0 comments on commit 254275b

Please sign in to comment.