Skip to content

Commit

Permalink
caching whether or not we are on ec2
Browse files Browse the repository at this point in the history
  • Loading branch information
drocamor committed Feb 18, 2014
1 parent faa0c70 commit a5b1ad4
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import (
"time"
)

type location struct {
ec2 bool
checked bool
}

var loc *location

func serviceAndRegion(host string) (string, string) {
var region, service string
parts := strings.Split(host, ".")
Expand Down Expand Up @@ -65,15 +72,23 @@ func checkKeys() {
}

func onEC2() bool {
if loc == nil {
loc = &location{}
}
if !(loc.checked) {
fmt.Println("Checking location...")
c, err := net.DialTimeout("tcp", "169.254.169.254:80", time.Second)

c, err := net.DialTimeout("tcp", "169.254.169.254:80", time.Second)

if err != nil {
return false
} else {
c.Close()
return true
if err != nil {
loc.ec2 = false
} else {
c.Close()
loc.ec2 = true
}
loc.checked = true
}

return loc.ec2
}

// getIAMRoleList gets a list of the roles that are available to this instance
Expand Down

0 comments on commit a5b1ad4

Please sign in to comment.