Skip to content

Commit

Permalink
Merge pull request #14 from georgijd/support-multiple-helper-instances
Browse files Browse the repository at this point in the history
Initial support for multiple helper instances
  • Loading branch information
liamg authored Mar 3, 2020
2 parents 053629c + 490c74f commit 9180b38
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 20 deletions.
74 changes: 74 additions & 0 deletions pkg/helpers/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package helpers

import (
"strings"
"sync"
"time"
)

// HelperRun represents a single run of a helper
type HelperRun struct {
output string
runTime time.Time
}

// Output returns the output of the run
func (hr HelperRun) Output() string {
return hr.output
}

// Time returns the time of the run
func (hr HelperRun) Time() time.Time {
return hr.runTime
}

// HelperRunCache stores the last run of all unique helpers.
// A helper is defined by its name and config
type HelperRunCache struct {
lock sync.Mutex
store map[string]HelperRun
}

// NewHelperRunCache creates a new helper run cache
func NewHelperRunCache() HelperRunCache {
return HelperRunCache{
lock: sync.Mutex{},
store: map[string]HelperRun{},
}
}

// GetOrAdd tries to find an item in the cache and creates one if not found
func (c *HelperRunCache) GetOrAdd(key string) HelperRun {
c.lock.Lock()
defer c.lock.Unlock()

item, found := c.store[key]
if !found {
item = HelperRun{
runTime: time.Now(),
}
c.store[key] = item
}

return item
}

// Put updates an existing item in the cache or creates one if not found
func (c *HelperRunCache) Put(key, output string) {
c.lock.Lock()
defer c.lock.Unlock()

item, found := c.store[key]
if !found {
item = HelperRun{}
}

item.output = output
item.runTime = time.Now()
c.store[key] = item
}

// Key generates a cache key from a Helper's attributes
func (c *HelperRunCache) Key(parts ...string) string {
return strings.Join(parts, ":")
}
26 changes: 6 additions & 20 deletions pkg/helpers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type Helper interface {
UpdateInterval() time.Duration // UpdateInterval returns the minimum time period before the helper should run again
}

var regLock sync.Mutex
var regLock = sync.Mutex{}
var registry = map[string]Helper{}
var runCache = NewHelperRunCache()

// Register registers a new helper by name
func Register(name string, helper Helper) {
Expand All @@ -28,14 +29,6 @@ func Register(name string, helper Helper) {
// ErrHelperNotFound means no helper exists by the specified name
var ErrHelperNotFound = fmt.Errorf("helper not found")

var cacheLock sync.Mutex
var cache = map[string]helperRun{}

type helperRun struct {
output string
runTime time.Time
}

// Run executes a helper with the provided config string
func Run(name, config string) (string, error) {
regLock.Lock()
Expand All @@ -45,19 +38,12 @@ func Run(name, config string) (string, error) {
return "", ErrHelperNotFound
}

cacheLock.Lock()
defer cacheLock.Unlock()

if lastRun, ok := cache[name]; ok {
if time.Since(lastRun.runTime) < helper.UpdateInterval() {
return lastRun.output, nil
}
lastRun := runCache.GetOrAdd(runCache.Key(name, config))
if time.Since(lastRun.Time()) < helper.UpdateInterval() {
return lastRun.Output(), nil
}

output := helper.Run(config)
cache[name] = helperRun{
output: output,
runTime: time.Now(),
}
runCache.Put(runCache.Key(name, config), output)
return output, nil
}

0 comments on commit 9180b38

Please sign in to comment.