Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Change cache policy
Browse files Browse the repository at this point in the history
  • Loading branch information
p-lambert committed Jan 26, 2018
1 parent cb1cf79 commit 08d0c21
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
21 changes: 20 additions & 1 deletion agent/service_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *ServiceMapper) update(metadata model.ServicesMetadata) {
var changes model.ServicesMetadata

for k, v := range metadata {
if _, ok := s.cache[k]; ok {
if !s.shouldAdd(k, metadata) {
continue
}

Expand All @@ -87,3 +87,22 @@ func (s *ServiceMapper) update(metadata model.ServicesMetadata) {
s.out <- changes
s.cache.Merge(changes)
}

func (s *ServiceMapper) shouldAdd(service string, metadata model.ServicesMetadata) bool {
cacheEntry, ok := s.cache[service]

// No cache entry?
if !ok {
return true
}

// Cache entry came from service API?
if _, ok = cacheEntry[model.ServiceApp]; ok {
return false
}

// New metadata value came from service API?
_, ok = metadata[service][model.ServiceApp]

return ok
}
30 changes: 30 additions & 0 deletions agent/service_mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ func TestServiceMapper(t *testing.T) {
assert.Len(mapper.cache, 2)
}

func TestCachePolicy(t *testing.T) {
assert := assert.New(t)

mapper, in, out := testMapper()
mapper.Start()
defer mapper.Stop()

input := model.ServicesMetadata{"service-a": {"app_type": "type-a"}}
in <- input
output := <-out

// A new service entry should propagate the metadata the the outbound channel
assert.Equal(input, output)

// A service entry that is already in cache should only be propagated IF:
// - Current version does NOT have "app"
// - New version DOES have "app"

// This first attempt won't be propagated to the writer
firstAttempt := model.ServicesMetadata{"service-a": {"app_type": "FIRST_ATTEMPT"}}
in <- firstAttempt

// But this second will
secondAttempt := model.ServicesMetadata{"service-a": {"app_type": "SECOND_ATTEMPT", "app": "app-a"}}
in <- secondAttempt

output = <-out
assert.Equal(secondAttempt, output)
}

func testMapper() (mapper *ServiceMapper, in, out chan model.ServicesMetadata) {
in = make(chan model.ServicesMetadata, 1)
out = make(chan model.ServicesMetadata, 1)
Expand Down
3 changes: 3 additions & 0 deletions model/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type ServicesMetadata map[string]map[string]string
// AppType is one of the pieces of information embedded in ServiceMetadata
const AppType = "app_type"

// ServiceApp represents the app to which certain integration belongs to
const ServiceApp = "app"

// Merge adds all entries from s2 to s1
func (s1 ServicesMetadata) Merge(s2 ServicesMetadata) {
for k, v := range s2 {
Expand Down

0 comments on commit 08d0c21

Please sign in to comment.