Lock down CollectionFactory. #116

This commit is contained in:
Andrey Smirnov
2014-10-06 19:55:46 +04:00
parent e4b9e974d2
commit 52c5934eb6
+18 -1
View File
@@ -2,10 +2,12 @@ package deb
import ( import (
"github.com/smira/aptly/database" "github.com/smira/aptly/database"
"sync"
) )
// CollectionFactory is a single place to generate all desired collections // CollectionFactory is a single place to generate all desired collections
type CollectionFactory struct { type CollectionFactory struct {
*sync.Mutex
db database.Storage db database.Storage
packages *PackageCollection packages *PackageCollection
remoteRepos *RemoteRepoCollection remoteRepos *RemoteRepoCollection
@@ -16,11 +18,14 @@ type CollectionFactory struct {
// NewCollectionFactory creates new factory // NewCollectionFactory creates new factory
func NewCollectionFactory(db database.Storage) *CollectionFactory { func NewCollectionFactory(db database.Storage) *CollectionFactory {
return &CollectionFactory{db: db} return &CollectionFactory{Mutex: &sync.Mutex{}, db: db}
} }
// PackageCollection returns (or creates) new PackageCollection // PackageCollection returns (or creates) new PackageCollection
func (factory *CollectionFactory) PackageCollection() *PackageCollection { func (factory *CollectionFactory) PackageCollection() *PackageCollection {
factory.Lock()
defer factory.Unlock()
if factory.packages == nil { if factory.packages == nil {
factory.packages = NewPackageCollection(factory.db) factory.packages = NewPackageCollection(factory.db)
} }
@@ -30,6 +35,9 @@ func (factory *CollectionFactory) PackageCollection() *PackageCollection {
// RemoteRepoCollection returns (or creates) new RemoteRepoCollection // RemoteRepoCollection returns (or creates) new RemoteRepoCollection
func (factory *CollectionFactory) RemoteRepoCollection() *RemoteRepoCollection { func (factory *CollectionFactory) RemoteRepoCollection() *RemoteRepoCollection {
factory.Lock()
defer factory.Unlock()
if factory.remoteRepos == nil { if factory.remoteRepos == nil {
factory.remoteRepos = NewRemoteRepoCollection(factory.db) factory.remoteRepos = NewRemoteRepoCollection(factory.db)
} }
@@ -39,6 +47,9 @@ func (factory *CollectionFactory) RemoteRepoCollection() *RemoteRepoCollection {
// SnapshotCollection returns (or creates) new SnapshotCollection // SnapshotCollection returns (or creates) new SnapshotCollection
func (factory *CollectionFactory) SnapshotCollection() *SnapshotCollection { func (factory *CollectionFactory) SnapshotCollection() *SnapshotCollection {
factory.Lock()
defer factory.Unlock()
if factory.snapshots == nil { if factory.snapshots == nil {
factory.snapshots = NewSnapshotCollection(factory.db) factory.snapshots = NewSnapshotCollection(factory.db)
} }
@@ -48,6 +59,9 @@ func (factory *CollectionFactory) SnapshotCollection() *SnapshotCollection {
// LocalRepoCollection returns (or creates) new LocalRepoCollection // LocalRepoCollection returns (or creates) new LocalRepoCollection
func (factory *CollectionFactory) LocalRepoCollection() *LocalRepoCollection { func (factory *CollectionFactory) LocalRepoCollection() *LocalRepoCollection {
factory.Lock()
defer factory.Unlock()
if factory.localRepos == nil { if factory.localRepos == nil {
factory.localRepos = NewLocalRepoCollection(factory.db) factory.localRepos = NewLocalRepoCollection(factory.db)
} }
@@ -57,6 +71,9 @@ func (factory *CollectionFactory) LocalRepoCollection() *LocalRepoCollection {
// PublishedRepoCollection returns (or creates) new PublishedRepoCollection // PublishedRepoCollection returns (or creates) new PublishedRepoCollection
func (factory *CollectionFactory) PublishedRepoCollection() *PublishedRepoCollection { func (factory *CollectionFactory) PublishedRepoCollection() *PublishedRepoCollection {
factory.Lock()
defer factory.Unlock()
if factory.publishedRepos == nil { if factory.publishedRepos == nil {
factory.publishedRepos = NewPublishedRepoCollection(factory.db) factory.publishedRepos = NewPublishedRepoCollection(factory.db)
} }