From 657a62b400d96d871485b271068ee7eb152d9efb Mon Sep 17 00:00:00 2001 From: Anant Prakash Date: Tue, 15 May 2018 15:47:29 +0530 Subject: [PATCH] Use two constructors for default and custom cleanupPeriod Add code comments Signed-off-by: Anant Prakash --- .../common/transactions/transactions.go | 20 ++++++++++--------- .../common/transactions/transactions_test.go | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/common/transactions/transactions.go b/src/github.com/matrix-org/dendrite/common/transactions/transactions.go index e61edd260..febcb9a75 100644 --- a/src/github.com/matrix-org/dendrite/common/transactions/transactions.go +++ b/src/github.com/matrix-org/dendrite/common/transactions/transactions.go @@ -26,22 +26,23 @@ type txnsMap map[string]*util.JSONResponse // Cache represents a temporary store for response entries. // Entries are evicted after a certain period, defined by cleanupPeriod. +// This works by keeping two maps of entries, and cycling the maps after the cleanupPeriod. type Cache struct { sync.RWMutex txnsMaps [2]txnsMap cleanupPeriod time.Duration } -// New creates a new Cache object, starts cacheCleanService. -// Takes cleanupPeriod (in minutes) as argument, in case of 0 DefaultCleanupPeriod is used instead. -// Returns a reference to newly created Cache. -func New(cleanupPeriodInMins int) *Cache { - t := Cache{txnsMaps: [2]txnsMap{make(txnsMap), make(txnsMap)}} +// New is a wrapper which calls NewWithCleanupPeriod with DefaultCleanupPeriod as argument. +func New() *Cache { + return NewWithCleanupPeriod(DefaultCleanupPeriod) +} - cleanupPeriod := time.Duration(cleanupPeriodInMins) * time.Minute - if cleanupPeriod == 0 { - cleanupPeriod = DefaultCleanupPeriod - } +// NewWithCleanupPeriod creates a new Cache object, starts cacheCleanService. +// Takes cleanupPeriod as argument. +// Returns a reference to newly created Cache. +func NewWithCleanupPeriod(cleanupPeriod time.Duration) *Cache { + t := Cache{txnsMaps: [2]txnsMap{make(txnsMap), make(txnsMap)}} t.cleanupPeriod = cleanupPeriod // Start clean service as the Cache is created @@ -75,6 +76,7 @@ func (t *Cache) AddTransaction(txnID string, res *util.JSONResponse) { // cacheCleanService is responsible for cleaning up entries after cleanupPeriod. // It guarantees that an entry will be present in cache for at least cleanupPeriod & at most 2 * cleanupPeriod. +// This cycles the txnMaps forward, i.e. back map is assigned the front and front is assigned an empty map. func cacheCleanService(t *Cache) { ticker := time.Tick(t.cleanupPeriod) for range ticker { diff --git a/src/github.com/matrix-org/dendrite/common/transactions/transactions_test.go b/src/github.com/matrix-org/dendrite/common/transactions/transactions_test.go index b0713dacb..0cdb776cc 100644 --- a/src/github.com/matrix-org/dendrite/common/transactions/transactions_test.go +++ b/src/github.com/matrix-org/dendrite/common/transactions/transactions_test.go @@ -30,7 +30,7 @@ var ( // TestCache creates a New Cache and tests AddTransaction & FetchTransaction func TestCache(t *testing.T) { - fakeTxnCache := New(0) + fakeTxnCache := New() fakeTxnCache.AddTransaction(fakeTxnID, fakeResponse) // Add entries for noise.