Don't use transactions when direct db access is enough

For read-only action transactions are not necessary and they risk to deadlock
if multiple go-routines try to read the database.
This commit is contained in:
Lorenzo Bolla
2021-08-24 11:15:39 +02:00
parent fd83c1a5bf
commit 2fa3adee1d
4 changed files with 17 additions and 18 deletions
+5 -4
View File
@@ -237,12 +237,13 @@ func (collection *LocalRepoCollection) Len() int {
// Drop removes remote repo from collection // Drop removes remote repo from collection
func (collection *LocalRepoCollection) Drop(repo *LocalRepo) error { func (collection *LocalRepoCollection) Drop(repo *LocalRepo) error {
transaction, err := collection.db.OpenTransaction() if _, err := collection.db.Get(repo.Key()); err != nil {
if err != nil { if err == database.ErrNotFound {
return errors.New("local repo not found")
}
return err return err
} }
defer transaction.Discard()
delete(collection.cache, repo.UUID) delete(collection.cache, repo.UUID)
batch := collection.db.CreateBatch() batch := collection.db.CreateBatch()
+10
View File
@@ -201,3 +201,13 @@ func (s *LocalRepoCollectionSuite) TestDrop(c *C) {
c.Check(s.collection.Drop(repo1), ErrorMatches, "local repo not found") c.Check(s.collection.Drop(repo1), ErrorMatches, "local repo not found")
} }
func (s *LocalRepoCollectionSuite) TestDropNonExisting(c *C) {
repo := NewLocalRepo("local3", "Comment 3")
_, err := s.collection.ByUUID(repo.UUID)
c.Check(err, ErrorMatches, "local repo .* not found")
err = s.collection.Drop(repo)
c.Check(s.collection.Drop(repo), ErrorMatches, "local repo not found")
}
+1 -7
View File
@@ -898,13 +898,7 @@ func (collection *RemoteRepoCollection) Len() int {
// Drop removes remote repo from collection // Drop removes remote repo from collection
func (collection *RemoteRepoCollection) Drop(repo *RemoteRepo) error { func (collection *RemoteRepoCollection) Drop(repo *RemoteRepo) error {
transaction, err := collection.db.OpenTransaction() if _, err := collection.db.Get(repo.Key()); err != nil {
if err != nil {
return err
}
defer transaction.Discard()
if _, err = transaction.Get(repo.Key()); err != nil {
if err == database.ErrNotFound { if err == database.ErrNotFound {
return errors.New("repo not found") return errors.New("repo not found")
} }
+1 -7
View File
@@ -389,13 +389,7 @@ func (collection *SnapshotCollection) Len() int {
// Drop removes snapshot from collection // Drop removes snapshot from collection
func (collection *SnapshotCollection) Drop(snapshot *Snapshot) error { func (collection *SnapshotCollection) Drop(snapshot *Snapshot) error {
transaction, err := collection.db.OpenTransaction() if _, err := collection.db.Get(snapshot.Key()); err != nil {
if err != nil {
return err
}
defer transaction.Discard()
if _, err = transaction.Get(snapshot.Key()); err != nil {
if err == database.ErrNotFound { if err == database.ErrNotFound {
return errors.New("snapshot not found") return errors.New("snapshot not found")
} }