Merge pull request #346 from smira/api-no-lock-fix

Flush collection contents on each DB unlock in API.
This commit is contained in:
Andrey Smirnov
2016-02-14 14:13:52 +03:00
+30 -34
View File
@@ -27,6 +27,29 @@ const (
RELEASEDB RELEASEDB
) )
// Flushes all collections which cache in-memory objects
func flushColections() {
// lock everything to eliminate in-progress calls
r := context.CollectionFactory().RemoteRepoCollection()
r.Lock()
defer r.Unlock()
l := context.CollectionFactory().LocalRepoCollection()
l.Lock()
defer l.Unlock()
s := context.CollectionFactory().SnapshotCollection()
s.Lock()
defer s.Unlock()
p := context.CollectionFactory().PublishedRepoCollection()
p.Lock()
defer p.Unlock()
// all collections locked, flush them
context.CollectionFactory().Flush()
}
// Periodically flushes CollectionFactory to free up memory used by // Periodically flushes CollectionFactory to free up memory used by
// collections, flushing caches. If the two channels are provided, // collections, flushing caches. If the two channels are provided,
// they are used to acquire and release the database. // they are used to acquire and release the database.
@@ -38,40 +61,12 @@ func cacheFlusher(requests chan int, acks chan error) {
for { for {
<-ticker <-ticker
func() { // if aptly API runs in -no-lock mode,
// lock database if needed // caches are flushed when DB is closed anyway, no need
if requests != nil { // to flush them here
requests <- ACQUIREDB if requests == nil {
err := <-acks flushColections()
if err != nil { }
return
}
defer func() {
requests <- RELEASEDB
<-acks
}()
}
// lock everything to eliminate in-progress calls
r := context.CollectionFactory().RemoteRepoCollection()
r.Lock()
defer r.Unlock()
l := context.CollectionFactory().LocalRepoCollection()
l.Lock()
defer l.Unlock()
s := context.CollectionFactory().SnapshotCollection()
s.Lock()
defer s.Unlock()
p := context.CollectionFactory().PublishedRepoCollection()
p.Lock()
defer p.Unlock()
// all collections locked, flush them
context.CollectionFactory().Flush()
}()
} }
} }
@@ -95,6 +90,7 @@ func acquireDatabase(requests chan int, acks chan error) {
case RELEASEDB: case RELEASEDB:
clients-- clients--
if clients == 0 { if clients == 0 {
flushColections()
acks <- context.CloseDatabase() acks <- context.CloseDatabase()
} else { } else {
acks <- nil acks <- nil