Skip to content

Commit

Permalink
add: synchronize calls to DiscoverPollEndpoint
Browse files Browse the repository at this point in the history
Agents that share ECSClients can call DiscoverPollEndpoint (DPE) multiple
times per task. Each routine that calls DPE will first check the cache
before performing the actual API call over the network. The intention
here is that only one actual API call is performed (by the first
routine to call DPE).

However, it is possible for multiple routines to race and effectively
make many actual API calls. This is because the `pollEndpointCache` is
only updated when the first API call _returns_.

This change enforces the intended behavior by making subsequent
routines wait for the cache to be updated (or not) by the first
thread, eliminating simultaneous calls to DPE.
  • Loading branch information
Isaac Feldman authored and isaac-400 committed Feb 19, 2025
1 parent f00282d commit 870c60e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ecs-agent/api/ecs/client/ecs_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"net/http"
"strings"
"sync"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -74,6 +75,7 @@ type ecsClient struct {
ec2metadata ec2.EC2MetadataClient
httpClient *http.Client
pollEndpointCache async.TTLCache
pollEndpointLock sync.Mutex
isFIPSDetected bool
shouldExcludeIPv6PortBinding bool
sascCustomRetryBackoff func(func() error) error
Expand Down Expand Up @@ -731,6 +733,8 @@ func (client *ecsClient) DiscoverSystemLogsEndpoint(containerInstanceArn string,

func (client *ecsClient) discoverPollEndpoint(containerInstanceArn string,
availabilityZone string) (*ecsmodel.DiscoverPollEndpointOutput, error) {
client.pollEndpointLock.Lock()
defer client.pollEndpointLock.Unlock()
// Try getting an entry from the cache.
cachedEndpoint, expired, found := client.pollEndpointCache.Get(containerInstanceArn)
if !expired && found {
Expand Down

0 comments on commit 870c60e

Please sign in to comment.