Skip to content

Commit

Permalink
Performance fixes for running ec2tag in non-aws environments
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Mar 23, 2017
1 parent 5143caa commit 1fcee69
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions aws/ec2info.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package aws

import (
"net/http"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)

var describerClient InstanceDescriber

// Ec2Info -
type Ec2Info struct {
describer func() InstanceDescriber
Expand All @@ -23,8 +28,11 @@ func NewEc2Info() *Ec2Info {
metaClient := NewEc2Meta()
return &Ec2Info{
describer: func() InstanceDescriber {
region := metaClient.Region()
return ec2Client(region)
if describerClient == nil {
region := metaClient.Region()
describerClient = ec2Client(region)
}
return describerClient
},
metaClient: metaClient,
cache: make(map[string]interface{}),
Expand All @@ -34,6 +42,7 @@ func NewEc2Info() *Ec2Info {
func ec2Client(region string) (client InstanceDescriber) {
config := aws.NewConfig()
config = config.WithRegion(region)
config = config.WithHTTPClient(&http.Client{Timeout: 500 * time.Millisecond})
client = ec2.New(session.New(config))
return client
}
Expand All @@ -59,6 +68,8 @@ func (e *Ec2Info) Tag(tag string, def ...string) string {
}

func (e *Ec2Info) describeInstance() (output *ec2.DescribeInstancesOutput) {
// cache the InstanceDescriber here
e.describer()
if e.metaClient.nonAWS {
return nil
}
Expand Down

0 comments on commit 1fcee69

Please sign in to comment.