comparison configmix/extras/aws.py @ 283:503768f91a05

More granular configuration of retrieving AWS metadata: retries with backoff setting
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 07 Dec 2020 01:51:24 +0100
parents da1596034954
children 4aaf74858d07
comparison
equal deleted inserted replaced
282:da1596034954 283:503768f91a05
13 __all__ = [] 13 __all__ = []
14 14
15 15
16 import requests 16 import requests
17 import requests.exceptions 17 import requests.exceptions
18 import requests.adapters
19 import urllib3
18 20
19 21
20 _MARKER = object() 22 _MARKER = object()
21 23
22 24
23 URL_META_INSTANCEID = "http://169.254.169.254/latest/meta-data/instance-id" 25 URL_META_INSTANCEID = "http://169.254.169.254/latest/meta-data/instance-id"
24 URL_META_REGION = "http://169.254.169.254/latest/meta-data/placement/region" 26 URL_META_REGION = "http://169.254.169.254/latest/meta-data/placement/region"
25 URL_META_AVAILABILITY_ZONE = "http://169.254.169.254/latest/meta-data/availability-zone" 27 URL_META_AVAILABILITY_ZONE = "http://169.254.169.254/latest/meta-data/availability-zone"
26 URL_DYN_INSTANCE_IDENTITY_DOC = "http://169.254.169.254/latest/dynamic/instance-identity/document" 28 URL_DYN_INSTANCE_IDENTITY_DOC = "http://169.254.169.254/latest/dynamic/instance-identity/document"
27 TIMEOUT = 10 29 TIMEOUT = 2
30 # See https://gist.github.com/doublenns/7e3e4b72df4aaeccbeabf87ba767f44e
31 RETRIES = urllib3.Retry(total=3, backoff_factor=0.3)
28 32
29 _meta_instanceid = None 33 _meta_instanceid = None
30 _meta_region = None 34 _meta_region = None
31 _meta_availability_zone = None 35 _meta_availability_zone = None
32 _dyn_instance_identity_doc = None 36 _dyn_instance_identity_doc = None
33 37
34 38
35 def _get_text_req(url): 39 def _get_text_req(url):
36 with requests.Session() as sess: 40 with requests.Session() as sess:
37 try: 41 try:
42 a = requests.adapters.HTTPAdapter(max_retries=RETRIES)
43 sess.mount("http://", a)
38 resp = sess.get(url, timeout=TIMEOUT) 44 resp = sess.get(url, timeout=TIMEOUT)
39 resp.raise_for_status() 45 resp.raise_for_status()
40 return resp.text 46 return resp.text
41 except requests.exceptions.RequestException: 47 except requests.exceptions.RequestException:
42 return _MARKER 48 return _MARKER
43 49
44 50
45 def _get_json_req(url): 51 def _get_json_req(url):
46 with requests.Session() as sess: 52 with requests.Session() as sess:
47 try: 53 try:
54 a = requests.adapters.HTTPAdapter(max_retries=RETRIES)
55 sess.mount("http://", a)
48 resp = sess.get(url, timeout=TIMEOUT) 56 resp = sess.get(url, timeout=TIMEOUT)
49 resp.raise_for_status() 57 resp.raise_for_status()
50 return resp.json() 58 return resp.json()
51 except requests.exceptions.RequestException: 59 except requests.exceptions.RequestException:
52 return _MARKER 60 return _MARKER