Skip to content

Commit

Permalink
pkg/database: Limit the number of open connections to 1 (#39)
Browse files Browse the repository at this point in the history
Getting a `database is locked` error. This [upstream
issue](mattn/go-sqlite3#274 (comment))
suggested limiting the number of connection to one and it seems to have
helped. It's not idea, but I need to move on.

ref #38
  • Loading branch information
kalbasit authored Dec 6, 2024
1 parent 8f74bc2 commit 98cef5a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func Open(logger log15.Logger, dbpath string) (*DB, error) {
return nil, fmt.Errorf("error opening the SQLite3 database at %q: %w", dbpath, err)
}

// Getting an error `database is locked` when data is being inserted in the
// database at a fast rate. This will slow down read/write from the database
// but at least none of them will fail due to connection issues.
sdb.SetMaxOpenConns(1)

db := &DB{DB: sdb, logger: logger.New("dbpath", dbpath)}

return db, db.createTables()
Expand Down
61 changes: 61 additions & 0 deletions pkg/database/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -254,6 +255,66 @@ func TestInsertNarInfoRecord(t *testing.T) {
t.Errorf("want %q got %q", want, got)
}
})

t.Run("can write many narinfos", func(t *testing.T) {
var wg sync.WaitGroup

errC := make(chan error)

for i := 0; i < 10000; i++ {
wg.Add(1)

go func() {
defer wg.Done()

hash, err := helper.RandString(128, nil)
if err != nil {
errC <- fmt.Errorf("expected no error but got: %w", err)

return
}

tx, err := db.Begin()
if err != nil {
errC <- fmt.Errorf("expected no error but got: %w", err)

return
}

//nolint:errcheck
defer tx.Rollback()

if _, err := db.InsertNarInfoRecord(tx, hash); err != nil {
errC <- fmt.Errorf("expected no error got: %w", err)

return
}

if err := tx.Commit(); err != nil {
errC <- fmt.Errorf("expected no error got: %w", err)

return
}
}()
}

done := make(chan struct{})

go func() {
wg.Wait()

close(done)
}()

for {
select {
case err := <-errC:
t.Errorf("got an error: %s", err)
case <-done:
return
}
}
})
}

//nolint:paralleltest
Expand Down

0 comments on commit 98cef5a

Please sign in to comment.