Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/database: Limit the number of open connections to 1 #39

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading