From eb165fb359383d1db81274b20141c5835b9c9ee3 Mon Sep 17 00:00:00 2001 From: Brannon King Date: Fri, 24 Sep 2021 11:00:25 -0400 Subject: [PATCH] lower flush period due to smaller block time, fixed possible data race --- database/ffldb/dbcache.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/database/ffldb/dbcache.go b/database/ffldb/dbcache.go index 39197d82..29743e76 100644 --- a/database/ffldb/dbcache.go +++ b/database/ffldb/dbcache.go @@ -23,7 +23,7 @@ const ( // defaultFlushSecs is the default number of seconds to use as a // threshold in between database cache flushes when the cache size has // not been exceeded. - defaultFlushSecs = 300 // 5 minutes + defaultFlushSecs = 120 // 2 minutes // ldbBatchHeaderSize is the size of a leveldb batch header which // includes the sequence header and record counter. @@ -499,10 +499,12 @@ func (c *dbCache) flush() error { // Since the cached keys to be added and removed use an immutable treap, // a snapshot is simply obtaining the root of the tree under the lock // which is used to atomically swap the root. - c.cacheLock.RLock() + c.cacheLock.Lock() cachedKeys := c.cachedKeys cachedRemove := c.cachedRemove - c.cacheLock.RUnlock() + c.cachedKeys = treap.NewImmutable() + c.cachedRemove = treap.NewImmutable() + c.cacheLock.Unlock() // Nothing to do if there is no data to flush. if cachedKeys.Len() == 0 && cachedRemove.Len() == 0 { @@ -514,12 +516,6 @@ func (c *dbCache) flush() error { return err } - // Clear the cache since it has been flushed. - c.cacheLock.Lock() - c.cachedKeys = treap.NewImmutable() - c.cachedRemove = treap.NewImmutable() - c.cacheLock.Unlock() - return nil }