From a09ee1f72c3775aa3b544ac2ca54722bdad01696 Mon Sep 17 00:00:00 2001
From: Dan Callaghan <dcallagh@redhat.com>
Date: Jun 01 2017 05:20:32 +0000
Subject: use a global requests session


... so that we benefit from connection pooling across requests.

---

diff --git a/greenwave/api_v1.py b/greenwave/api_v1.py
index 103d0e2..4d31693 100644
--- a/greenwave/api_v1.py
+++ b/greenwave/api_v1.py
@@ -17,6 +17,8 @@ from greenwave.policies import policies
 
 api = (Blueprint('api_v1', __name__))
 
+requests_session = requests.Session()
+
 
 @api.route('/decision', methods=['POST'])
 def make_decision():
@@ -59,63 +61,62 @@ def make_decision():
     policies_satisified = True
     summary = []
     unsatisfied_requirements = []
-    with requests.Session() as s:
-        for policy_id, policy in applicable_policies.iteritems():
-            for item in subjects:
-                res = s.get('{0}/results?item={1}&testcases={2}'.format(
-                    current_app.config['RESULTSDB_API_URL'], item, ','.join(policy['rules']))
-                )
-                res.raise_for_status()
-                results = res.json()['data']
-                total_failed_results = 0
-                if results:
-                    for result in results:
-                        if result['outcome'] not in ('PASSED', 'INFO'):
-                            # query WaiverDB to check whether the result has a waiver
-                            res = s.get('{0}/waivers/?project_version={1}&result_id={2}'.format(
-                                current_app.config['WAIVERDB_API_URL'], product_version,
-                                result['id'])
-                            )
-                            res.raise_for_status()
-                            waiver = res.json()['data']
-                            if not waiver or not waiver[0]['waived']:
-                                policies_satisified = False
-                                total_failed_results += 1
-                                unsatisfied_requirements.append({
-                                    'type': 'test-result-failed',
-                                    'item': item,
-                                    'testcase': result['testcase']['name'],
-                                    'result_id': result['id']})
-                    # find missing results
-                    rules_applied = [result['testcase']['name'] for result in results]
-                    for rule in policy['rules']:
-                        if rule not in rules_applied:
+    for policy_id, policy in applicable_policies.iteritems():
+        for item in subjects:
+            res = requests_session.get('{0}/results?item={1}&testcases={2}'.format(
+                current_app.config['RESULTSDB_API_URL'], item, ','.join(policy['rules']))
+            )
+            res.raise_for_status()
+            results = res.json()['data']
+            total_failed_results = 0
+            if results:
+                for result in results:
+                    if result['outcome'] not in ('PASSED', 'INFO'):
+                        # query WaiverDB to check whether the result has a waiver
+                        res = requests_session.get('{0}/waivers/?project_version={1}&result_id={2}'.format(
+                            current_app.config['WAIVERDB_API_URL'], product_version,
+                            result['id'])
+                        )
+                        res.raise_for_status()
+                        waiver = res.json()['data']
+                        if not waiver or not waiver[0]['waived']:
+                            policies_satisified = False
                             total_failed_results += 1
                             unsatisfied_requirements.append({
-                                'type': 'test-result-missing',
+                                'type': 'test-result-failed',
                                 'item': item,
-                                'testcase': rule})
-                    if total_failed_results:
-                        summary.append(
-                            '{0}: {1} of {2} required tests failed, the policy {3} is not satisfied'
-                            .format(item, total_failed_results, len(policy['rules']),
-                                    policy_id))
-                    else:
-                        summary.append(
-                            '%s: policy %s is satisfied as all required tests are passing' % (
-                                item, policy_id))
-                else:
-                    policies_satisified = False
-                    summary.append('%s: no test results found' % item)
-                    for rule in policy['rules']:
+                                'testcase': result['testcase']['name'],
+                                'result_id': result['id']})
+                # find missing results
+                rules_applied = [result['testcase']['name'] for result in results]
+                for rule in policy['rules']:
+                    if rule not in rules_applied:
+                        total_failed_results += 1
                         unsatisfied_requirements.append({
                             'type': 'test-result-missing',
                             'item': item,
                             'testcase': rule})
-        res = {
-            'policies_satisified': policies_satisified,
-            'summary': '\n'.join(summary),
-            'applicable_policies': list(applicable_policies.keys()),
-            'unsatisfied_requirements': unsatisfied_requirements
-        }
-        return jsonify(res), 200
+                if total_failed_results:
+                    summary.append(
+                        '{0}: {1} of {2} required tests failed, the policy {3} is not satisfied'
+                        .format(item, total_failed_results, len(policy['rules']),
+                                policy_id))
+                else:
+                    summary.append(
+                        '%s: policy %s is satisfied as all required tests are passing' % (
+                            item, policy_id))
+            else:
+                policies_satisified = False
+                summary.append('%s: no test results found' % item)
+                for rule in policy['rules']:
+                    unsatisfied_requirements.append({
+                        'type': 'test-result-missing',
+                        'item': item,
+                        'testcase': rule})
+    res = {
+        'policies_satisified': policies_satisified,
+        'summary': '\n'.join(summary),
+        'applicable_policies': list(applicable_policies.keys()),
+        'unsatisfied_requirements': unsatisfied_requirements
+    }
+    return jsonify(res), 200

