Skip to content

Commit

Permalink
Merge pull request #23 from newrelic/gsanchez/fix/cache
Browse files Browse the repository at this point in the history
Improve performance of cache Put func
  • Loading branch information
gsanchezgavier authored Aug 26, 2020
2 parents 4d1c0cf + 4ca597a commit b73233c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
30 changes: 13 additions & 17 deletions cfclient/cfapps/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import (

// Cache ...
type Cache struct {
Collection map[string]*CFApp
sync *sync.RWMutex
Collection map[string]*CFApp
WriteBuffer chan *CFApp
sync *sync.RWMutex
}

// NewCache ...
func NewCache() *Cache {
cache := &Cache{
Collection: map[string]*CFApp{},
sync: &sync.RWMutex{},
Collection: map[string]*CFApp{},
WriteBuffer: make(chan *CFApp, 1024),
sync: &sync.RWMutex{},
}
cache.Start()
return cache
Expand Down Expand Up @@ -57,6 +59,11 @@ func (c *Cache) Start() {
v.UpdateInstances()
}
c.sync.RUnlock()

case app := <-c.WriteBuffer:
c.sync.Lock()
c.Collection[app.GUID] = app
c.sync.Unlock()
}
}
}()
Expand All @@ -74,17 +81,6 @@ func (c *Cache) Get(id string) (app *CFApp, found bool) {
}

// Put ...
func (c *Cache) Put(id string) *CFApp {
c.sync.Lock()
defer c.sync.Unlock()

if app, found := c.Collection[id]; found {
return app
}
GetInstance().app.Log.Debug("Adding new app: ", id)
app := NewCFApp(id)
c.Collection[app.GUID] = app
GetInstance().UpdateAppAsync(app)

return app
func (c *Cache) Put(app *CFApp) {
c.WriteBuffer <- app
}
15 changes: 10 additions & 5 deletions cfclient/cfapps/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,27 @@ func (c *CFAppManager) GetAppInstanceAttributes(appID string, instanceID int32)
}

// GetApp ...
func (c *CFAppManager) GetApp(guid string) *CFApp {
if app, found := c.Cache.Get(guid); found {
func (c *CFAppManager) GetApp(guid string) (app *CFApp) {
var found bool
if app, found = c.Cache.Get(guid); found {
return app
}
return c.Cache.Put(guid)
app = NewCFApp(guid)
c.app.Log.Debug("Adding new app: ", guid)
c.Cache.Put(app)
c.updateAppAsync(app)
return app
}

func (c *CFAppManager) UpdateAppAsync(app *CFApp) {
func (c *CFAppManager) updateAppAsync(app *CFApp) {
go func() {
if err := c.FetchApp(app); err != nil {
if atomic.LoadInt32(&app.retryCount) > 2 {
c.app.Log.Warn("Max retries trying to fetch app: ", app.GUID)
return
}
atomic.AddInt32(&app.retryCount, 1)
c.UpdateAppAsync(app)
c.updateAppAsync(app)
} else {
atomic.StoreInt32(&app.retryCount, 0)
}
Expand Down

0 comments on commit b73233c

Please sign in to comment.