-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DiscoverPollEndpoint: lengthen cache ttl and improve resiliency (#3109)
The acs/tacs endpoints that ecs agent connects have never changed before, so the current behavior of calling the API every 20 minutes is not necessary. Lengthening this cache TTL to 12 hours will reduce load on the ECS service and reduce the chance of customers being throttled. Additionally, the LRU cache we were using automatically evicted any value once it expired. This means that this API call could become a source of complete ecs agent failure in the event of an LSE, in the event that agent threw away it's cached endpoint and then failed to get a new one via DiscoverPollEndpoint. By keeping expired values in the cache, we can fallback to use the expired value in the event that the DiscoverPollEndpoint API is failing, thus keeping agent connected to ACS and TACS.
- Loading branch information
Showing
6 changed files
with
298 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package async | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type TTLCache interface { | ||
// Get fetches a value from cache, returns nil, false on miss | ||
Get(key string) (value interface{}, expired bool, ok bool) | ||
// Set sets a value in cache. overrites any existing value | ||
Set(key string, value interface{}) | ||
// Delete deletes the value from the cache | ||
Delete(key string) | ||
} | ||
|
||
// Creates a TTL cache with ttl for items. | ||
func NewTTLCache(ttl time.Duration) TTLCache { | ||
return &ttlCache{ | ||
ttl: ttl, | ||
cache: make(map[string]*ttlCacheEntry), | ||
} | ||
} | ||
|
||
type ttlCacheEntry struct { | ||
value interface{} | ||
expiry time.Time | ||
} | ||
|
||
type ttlCache struct { | ||
mu sync.RWMutex | ||
cache map[string]*ttlCacheEntry | ||
ttl time.Duration | ||
} | ||
|
||
// Get returns the value associated with the key. | ||
// returns if the item is expired (true if key is expired). | ||
// ok result indicates whether value was found in the map. | ||
// Note that items are not automatically deleted from the map when they expire. They will continue to be | ||
// returned with expired=true. | ||
func (t *ttlCache) Get(key string) (value interface{}, expired bool, ok bool) { | ||
t.mu.RLock() | ||
defer t.mu.RUnlock() | ||
if _, iok := t.cache[key]; !iok { | ||
return nil, false, false | ||
} | ||
entry := t.cache[key] | ||
expired = time.Now().After(entry.expiry) | ||
return entry.value, expired, true | ||
} | ||
|
||
// Set sets the key-value pair in the cache | ||
func (t *ttlCache) Set(key string, value interface{}) { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
t.cache[key] = &ttlCacheEntry{ | ||
value: value, | ||
expiry: time.Now().Add(t.ttl), | ||
} | ||
} | ||
|
||
// Delete removes the entry associated with the key from cache | ||
func (t *ttlCache) Delete(key string) { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
delete(t.cache, key) | ||
} |
Oops, something went wrong.