Use cycling double map instead, improve code logic, remove unneeded test

Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com>
This commit is contained in:
Anant Prakash 2018-05-14 23:38:20 +05:30
parent a0715ceb1f
commit a6e171ddf7
No known key found for this signature in database
GPG key ID: C5D399F626523045
2 changed files with 39 additions and 66 deletions

View file

@ -13,75 +13,73 @@
package transactions package transactions
import ( import (
"errors"
"sync" "sync"
"time" "time"
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
// CleanupPeriod represents time in nanoseconds after which cacheCleanService runs. // DefaultCleanupPeriod represents time in nanoseconds after which cacheCleanService runs.
const CleanupPeriod time.Duration = 30 * time.Minute const DefaultCleanupPeriod time.Duration = 30 * time.Minute
type entry struct { type txnsMap map[string]*util.JSONResponse
value *util.JSONResponse
entryTime time.Time
}
// Cache represents a temporary store for entries. // Cache represents a temporary store for entries.
// Entries are evicted after a certain period, defined by CleanupPeriod. // Entries are evicted after a certain period, defined by cleanupPeriod.
type Cache struct { type Cache struct {
sync.RWMutex sync.RWMutex
txns map[string]entry txnsMaps [2]txnsMap
cleanupPeriod time.Duration
} }
// New creates a new Cache object, starts cacheCleanService. // New creates a new Cache object, starts cacheCleanService.
// Returns a referance to newly created Cache. // Takes cleanupPeriod (in minutes) as argument, in case of 0 DefaultCleanupPeriod is used instead.
func New() *Cache { // Returns a reference to newly created Cache.
t := Cache{txns: make(map[string]entry)} func New(cleanupPeriodInMins int) *Cache {
t := Cache{txnsMaps: [2]txnsMap{make(txnsMap), make(txnsMap)}}
// Start cleanup service as the Cache is created cleanupPeriod := time.Duration(cleanupPeriodInMins) * time.Minute
if cleanupPeriod == 0 {
cleanupPeriod = DefaultCleanupPeriod
}
t.cleanupPeriod = cleanupPeriod
// Start clean service as the Cache is created
go cacheCleanService(&t) go cacheCleanService(&t)
return &t return &t
} }
// FetchTransaction looks up an entry for txnID in Cache. // FetchTransaction looks up an entry for txnID in Cache.
// Returns a JSON response if txnID is found, else returns error. // Returns (JSON response, true) if txnID is found, else the returned bool is false.
func (t *Cache) FetchTransaction(txnID string) (*util.JSONResponse, error) { func (t *Cache) FetchTransaction(txnID string) (*util.JSONResponse, bool) {
t.RLock() t.RLock()
res, ok := t.txns[txnID] defer t.RUnlock()
t.RUnlock() for _, txns := range t.txnsMaps {
res, ok := txns[txnID]
if ok { if ok {
return res.value, nil return res, true
}
} }
return nil, errors.New("TxnID not present") return nil, false
} }
// AddTransaction adds a entry for txnID in Cache for later access. // AddTransaction adds an entry for txnID in Cache for later access.
func (t *Cache) AddTransaction(txnID string, res *util.JSONResponse) { func (t *Cache) AddTransaction(txnID string, res *util.JSONResponse) {
t.Lock() t.Lock()
defer t.Unlock() defer t.Unlock()
t.txns[txnID] = entry{value: res, entryTime: time.Now()} t.txnsMaps[0][txnID] = res
} }
// cacheCleanService is responsible for cleaning up transactions older than CleanupPeriod. // cacheCleanService is responsible for cleaning up entries after cleanupPeriod.
// It guarantees that a transaction will be present in cache for at least CleanupPeriod & at most 2 * CleanupPeriod. // It guarantees that an entry will be present in cache for at least cleanupPeriod & at most 2 * cleanupPeriod.
// This works by
func cacheCleanService(t *Cache) { func cacheCleanService(t *Cache) {
for { ticker := time.Tick(t.cleanupPeriod)
time.Sleep(CleanupPeriod) for range ticker {
go clean(t)
}
}
func clean(t *Cache) {
expire := time.Now().Add(-CleanupPeriod)
for key := range t.txns {
t.Lock() t.Lock()
if t.txns[key].entryTime.Before(expire) { t.txnsMaps[1] = t.txnsMaps[0]
delete(t.txns, key) t.txnsMaps[0] = make(txnsMap)
}
t.Unlock() t.Unlock()
} }
} }

View file

@ -30,8 +30,7 @@ var (
// TestCache creates a New Cache and tests AddTransaction & FetchTransaction // TestCache creates a New Cache and tests AddTransaction & FetchTransaction
func TestCache(t *testing.T) { func TestCache(t *testing.T) {
fakeTxnCache := New(0)
fakeTxnCache := New()
fakeTxnCache.AddTransaction(fakeTxnID, fakeResponse) fakeTxnCache.AddTransaction(fakeTxnID, fakeResponse)
// Add entries for noise. // Add entries for noise.
@ -42,34 +41,10 @@ func TestCache(t *testing.T) {
) )
} }
testResponse, err := fakeTxnCache.FetchTransaction(fakeTxnID) testResponse, ok := fakeTxnCache.FetchTransaction(fakeTxnID)
if err != nil { if !ok {
t.Error("Failed to retrieve entry for txnID: ", fakeTxnID) t.Error("Failed to retrieve entry for txnID: ", fakeTxnID)
} else if testResponse.JSON != fakeResponse.JSON { } else if testResponse.JSON != fakeResponse.JSON {
t.Error("Incorrect fetched response. Expected: ", fakeResponse.JSON, " got: ", testResponse.JSON) t.Error("Fetched response incorrect. Expected: ", fakeResponse.JSON, " got: ", testResponse.JSON)
}
}
// TestConcurentAccess provides some guarantee against corruption.
func TestConcurentAccess(t *testing.T) {
fakeTxnCache := New()
// Signal that this test should run in parallel
t.Parallel()
// Add entries concurrently to test concurrent access.
for i := 1; i <= 1000; i++ {
go check(t, fakeTxnCache, i)
}
}
// Adds an entry and checks it is retrieved.
func check(t *testing.T, fakeTxnCache *Cache, i int) {
fakeTxnCache.AddTransaction(
fakeTxnID+string(i),
&util.JSONResponse{Code: http.StatusOK, JSON: fakeType{ID: string(i)}},
)
_, err := fakeTxnCache.FetchTransaction(fakeTxnID + string(i))
if err != nil {
t.Error("Failed to retrieve entry for txnID: ", fakeTxnID+string(i))
} }
} }