|
27 | 27 |
|
28 | 28 | logger = logging.getLogger(__name__) |
29 | 29 |
|
| 30 | +session = None |
| 31 | + |
| 32 | + |
| 33 | +def get_session(): |
| 34 | + global session |
| 35 | + if session is None: |
| 36 | + session = requests.Session() |
| 37 | + session.headers = { |
| 38 | + # https://toolbelt.readthedocs.org/en/latest/user-agent.html#user-agent-constructor |
| 39 | + 'User-Agent': user_agent('Deis Controller', deis_version), |
| 40 | + } |
| 41 | + # `mount` a custom adapter that retries failed connections for HTTP and HTTPS requests. |
| 42 | + # http://docs.python-requests.org/en/latest/api/#requests.adapters.HTTPAdapter |
| 43 | + session.mount('http://', requests.adapters.HTTPAdapter(max_retries=10)) |
| 44 | + session.mount('https://', requests.adapters.HTTPAdapter(max_retries=10)) |
| 45 | + return session |
| 46 | + |
30 | 47 |
|
31 | 48 | # http://kubernetes.io/v1.1/docs/design/identifiers.html |
32 | 49 | def validate_id_is_docker_compatible(value): |
@@ -525,27 +542,18 @@ def verify_application_health(self, **kwargs): |
525 | 542 | allowed.remove(404) |
526 | 543 | req_timeout = 3 |
527 | 544 |
|
528 | | - session = requests.Session() |
529 | | - session.headers = { |
530 | | - # https://toolbelt.readthedocs.org/en/latest/user-agent.html#user-agent-constructor |
531 | | - 'User-Agent': user_agent('Deis Controller', deis_version), |
532 | | - # set the Host header for the application being checked - not used for actual routing |
533 | | - 'Host': '{}.{}.nip.io'.format(self.id, settings.ROUTER_HOST) |
534 | | - } |
535 | | - |
536 | | - # `mount` a custom adapter that retries failed connections for HTTP and HTTPS requests. |
537 | | - # http://docs.python-requests.org/en/latest/api/#requests.adapters.HTTPAdapter |
538 | | - session.mount('http://', requests.adapters.HTTPAdapter(max_retries=10)) |
539 | | - session.mount('https://', requests.adapters.HTTPAdapter(max_retries=10)) |
540 | | - |
541 | 545 | # Give the router max of 10 tries or max 30 seconds to become healthy |
542 | 546 | # Uses time module to account for the timout value of 3 seconds |
543 | 547 | start = time.time() |
544 | 548 | failed = False |
| 549 | + headers = { |
| 550 | + # set the Host header for the application being checked - not used for actual routing |
| 551 | + 'Host': '{}.{}.nip.io'.format(self.id, settings.ROUTER_HOST), |
| 552 | + } |
545 | 553 | for _ in range(10): |
546 | 554 | try: |
547 | 555 | # http://docs.python-requests.org/en/master/user/advanced/#timeouts |
548 | | - response = session.get(url, timeout=req_timeout) |
| 556 | + response = get_session().get(url, timeout=req_timeout, headers=headers) |
549 | 557 | failed = False |
550 | 558 | except requests.exceptions.RequestException: |
551 | 559 | # In case of a failure where response object is not available |
|
0 commit comments