Skip to content

Commit

Permalink
save EC2 metadata between lookups so that if a lookup ever times out …
Browse files Browse the repository at this point in the history
…we still have the old value. fixes #554.
  • Loading branch information
Matt Singleton committed Jul 10, 2013
1 parent 124b479 commit e5a894c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class EC2(object):
"""
URL = "http://169.254.169.254/latest/meta-data"
TIMEOUT = 0.1 # second
metadata = {}

@staticmethod
def get_metadata():
Expand All @@ -214,7 +215,6 @@ def get_metadata():
# 'ami-id\nami-launch-index\nami-manifest-path\nhostname\ninstance-id\nlocal-ipv4\npublic-keys/\nreservation-id\nsecurity-groups'
# >>> urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id', timeout=1).read()
# 'i-deadbeef'
metadata = {}

# Every call may add TIMEOUT seconds in latency so don't abuse this call
# python 2.4 does not support an explicit timeout argument so force it here
Expand All @@ -230,7 +230,7 @@ def get_metadata():
try:
v = urllib2.urlopen(EC2.URL + "/" + unicode(k)).read().strip()
assert type(v) in (types.StringType, types.UnicodeType) and len(v) > 0, "%s is not a string" % v
metadata[k] = v
EC2.metadata[k] = v
except:
pass

Expand All @@ -241,7 +241,7 @@ def get_metadata():
except:
pass

return metadata
return EC2.metadata

@staticmethod
def get_instance_id():
Expand Down

0 comments on commit e5a894c

Please sign in to comment.