Cache exising task definitions to avoid rate limiting

This commit is contained in:
hwhelan-CB 2019-01-19 02:56:02 -05:00 committed by Traefiker Bot
parent 85ab0e6e70
commit 0d6f259adc

View file

@ -19,9 +19,11 @@ import (
"github.com/containous/traefik/old/provider" "github.com/containous/traefik/old/provider"
"github.com/containous/traefik/old/types" "github.com/containous/traefik/old/types"
"github.com/containous/traefik/safe" "github.com/containous/traefik/safe"
"github.com/patrickmn/go-cache"
) )
var _ provider.Provider = (*Provider)(nil) var _ provider.Provider = (*Provider)(nil)
var existingTaskDefCache = cache.New(30*time.Minute, 5*time.Minute)
// Provider holds configurations of the provider. // Provider holds configurations of the provider.
type Provider struct { type Provider struct {
@ -394,6 +396,10 @@ func (p *Provider) lookupEc2Instances(ctx context.Context, client *awsClient, cl
func (p *Provider) lookupTaskDefinitions(ctx context.Context, client *awsClient, taskDefArns map[string]*ecs.Task) (map[string]*ecs.TaskDefinition, error) { func (p *Provider) lookupTaskDefinitions(ctx context.Context, client *awsClient, taskDefArns map[string]*ecs.Task) (map[string]*ecs.TaskDefinition, error) {
taskDef := make(map[string]*ecs.TaskDefinition) taskDef := make(map[string]*ecs.TaskDefinition)
for arn, task := range taskDefArns { for arn, task := range taskDefArns {
if definition, ok := existingTaskDefCache.Get(arn); ok {
taskDef[arn] = definition.(*ecs.TaskDefinition)
log.Debugf("Found cached task definition for %s. Skipping the call", arn)
} else {
resp, err := client.ecs.DescribeTaskDefinitionWithContext(ctx, &ecs.DescribeTaskDefinitionInput{ resp, err := client.ecs.DescribeTaskDefinitionWithContext(ctx, &ecs.DescribeTaskDefinitionInput{
TaskDefinition: task.TaskDefinitionArn, TaskDefinition: task.TaskDefinitionArn,
}) })
@ -404,6 +410,8 @@ func (p *Provider) lookupTaskDefinitions(ctx context.Context, client *awsClient,
} }
taskDef[arn] = resp.TaskDefinition taskDef[arn] = resp.TaskDefinition
existingTaskDefCache.Set(arn, resp.TaskDefinition, cache.DefaultExpiration)
}
} }
return taskDef, nil return taskDef, nil
} }