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
+22 -26
View File
@@ -27,31 +27,8 @@ const (
RELEASEDB RELEASEDB
) )
// Periodically flushes CollectionFactory to free up memory used by // Flushes all collections which cache in-memory objects
// collections, flushing caches. If the two channels are provided, func flushColections() {
// they are used to acquire and release the database.
//
// Should be run in goroutine!
func cacheFlusher(requests chan int, acks chan error) {
ticker := time.Tick(15 * time.Minute)
for {
<-ticker
func() {
// lock database if needed
if requests != nil {
requests <- ACQUIREDB
err := <-acks
if err != nil {
return
}
defer func() {
requests <- RELEASEDB
<-acks
}()
}
// lock everything to eliminate in-progress calls // lock everything to eliminate in-progress calls
r := context.CollectionFactory().RemoteRepoCollection() r := context.CollectionFactory().RemoteRepoCollection()
r.Lock() r.Lock()
@@ -71,7 +48,25 @@ func cacheFlusher(requests chan int, acks chan error) {
// all collections locked, flush them // all collections locked, flush them
context.CollectionFactory().Flush() context.CollectionFactory().Flush()
}() }
// Periodically flushes CollectionFactory to free up memory used by
// collections, flushing caches. If the two channels are provided,
// they are used to acquire and release the database.
//
// Should be run in goroutine!
func cacheFlusher(requests chan int, acks chan error) {
ticker := time.Tick(15 * time.Minute)
for {
<-ticker
// if aptly API runs in -no-lock mode,
// caches are flushed when DB is closed anyway, no need
// to flush them here
if requests == nil {
flushColections()
}
} }
} }
@@ -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