Use a session so only one connection is needed.
This commit is contained in:
parent
5d961341c0
commit
ea19fd8416
@ -29,10 +29,9 @@ opts = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def _fetch_metadata(fetch_url):
|
def _fetch_metadata(fetch_url, session):
|
||||||
global h
|
|
||||||
try:
|
try:
|
||||||
r = requests.get(fetch_url)
|
r = session.get(fetch_url)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except (requests.HTTPError,
|
except (requests.HTTPError,
|
||||||
requests.ConnectionError,
|
requests.ConnectionError,
|
||||||
@ -48,11 +47,12 @@ def _fetch_metadata(fetch_url):
|
|||||||
sub_fetch_url = fetch_url + subkey
|
sub_fetch_url = fetch_url + subkey
|
||||||
if subkey[-1] == '/':
|
if subkey[-1] == '/':
|
||||||
subkey = subkey[:-1]
|
subkey = subkey[:-1]
|
||||||
new_content[subkey] = _fetch_metadata(sub_fetch_url)
|
new_content[subkey] = _fetch_metadata(sub_fetch_url, session)
|
||||||
content = new_content
|
content = new_content
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
def collect():
|
def collect():
|
||||||
root_url = '%s/' % (CONF.ec2.metadata_url)
|
root_url = '%s/' % (CONF.ec2.metadata_url)
|
||||||
return _fetch_metadata(root_url)
|
session = requests.Session()
|
||||||
|
return _fetch_metadata(root_url, session)
|
||||||
|
@ -29,7 +29,7 @@ class TestCollect(testtools.TestCase):
|
|||||||
super(TestCollect, self).setUp()
|
super(TestCollect, self).setUp()
|
||||||
self.useFixture(
|
self.useFixture(
|
||||||
fixtures.MonkeyPatch(
|
fixtures.MonkeyPatch(
|
||||||
'requests.get', test_ec2.fake_get))
|
'requests.Session', test_ec2.FakeSession))
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
super(TestCollect, self).tearDown()
|
super(TestCollect, self).tearDown()
|
||||||
|
@ -58,7 +58,8 @@ class FakeResponse(dict):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def fake_get(url):
|
class FakeSession(object):
|
||||||
|
def get(self, url):
|
||||||
url = urlparse.urlparse(url)
|
url = urlparse.urlparse(url)
|
||||||
|
|
||||||
if url.path == '/latest/meta-data/':
|
if url.path == '/latest/meta-data/':
|
||||||
@ -72,7 +73,8 @@ def fake_get(url):
|
|||||||
return FakeResponse(META_DATA[path])
|
return FakeResponse(META_DATA[path])
|
||||||
|
|
||||||
|
|
||||||
def fake_fail_get(url):
|
class FakeFailSession(object):
|
||||||
|
def get(self, url):
|
||||||
raise requests.exceptions.HTTPError(403, 'Forbidden')
|
raise requests.exceptions.HTTPError(403, 'Forbidden')
|
||||||
|
|
||||||
|
|
||||||
@ -83,7 +85,7 @@ class TestCollect(testtools.TestCase):
|
|||||||
|
|
||||||
def test_collect_ec2(self):
|
def test_collect_ec2(self):
|
||||||
self.useFixture(
|
self.useFixture(
|
||||||
fixtures.MonkeyPatch('requests.get', fake_get))
|
fixtures.MonkeyPatch('requests.Session', FakeSession))
|
||||||
collect.setup_conf()
|
collect.setup_conf()
|
||||||
ec2_md = ec2.collect()
|
ec2_md = ec2.collect()
|
||||||
self.assertThat(ec2_md, matchers.IsInstance(dict))
|
self.assertThat(ec2_md, matchers.IsInstance(dict))
|
||||||
@ -103,7 +105,7 @@ class TestCollect(testtools.TestCase):
|
|||||||
def test_collect_ec2_fail(self):
|
def test_collect_ec2_fail(self):
|
||||||
self.useFixture(
|
self.useFixture(
|
||||||
fixtures.MonkeyPatch(
|
fixtures.MonkeyPatch(
|
||||||
'requests.get', fake_fail_get))
|
'requests.Session', FakeFailSession))
|
||||||
collect.setup_conf()
|
collect.setup_conf()
|
||||||
self.assertRaises(exc.Ec2MetadataNotAvailable, ec2.collect)
|
self.assertRaises(exc.Ec2MetadataNotAvailable, ec2.collect)
|
||||||
self.assertIn('Forbidden', self.log.output)
|
self.assertIn('Forbidden', self.log.output)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user