From 0d6f259adcd5b012751b964eaac920ec1c62f6a0 Mon Sep 17 00:00:00 2001 From: hwhelan-CB <39995146+hwhelan-CB@users.noreply.github.com> Date: Sat, 19 Jan 2019 02:56:02 -0500 Subject: [PATCH] Cache exising task definitions to avoid rate limiting --- old/provider/ecs/ecs.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/old/provider/ecs/ecs.go b/old/provider/ecs/ecs.go index a9afc9ab6..32dccc3dd 100644 --- a/old/provider/ecs/ecs.go +++ b/old/provider/ecs/ecs.go @@ -19,9 +19,11 @@ import ( "github.com/containous/traefik/old/provider" "github.com/containous/traefik/old/types" "github.com/containous/traefik/safe" + "github.com/patrickmn/go-cache" ) var _ provider.Provider = (*Provider)(nil) +var existingTaskDefCache = cache.New(30*time.Minute, 5*time.Minute) // Provider holds configurations of the provider. type Provider struct { @@ -394,16 +396,22 @@ 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) { taskDef := make(map[string]*ecs.TaskDefinition) for arn, task := range taskDefArns { - resp, err := client.ecs.DescribeTaskDefinitionWithContext(ctx, &ecs.DescribeTaskDefinitionInput{ - TaskDefinition: task.TaskDefinitionArn, - }) + 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{ + TaskDefinition: task.TaskDefinitionArn, + }) - if err != nil { - log.Errorf("Unable to describe task definition: %s", err) - return nil, err + if err != nil { + log.Errorf("Unable to describe task definition: %s", err) + return nil, err + } + + taskDef[arn] = resp.TaskDefinition + existingTaskDefCache.Set(arn, resp.TaskDefinition, cache.DefaultExpiration) } - - taskDef[arn] = resp.TaskDefinition } return taskDef, nil }