Skip to content

Commit

Permalink
retry and global r_session
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Reynolds committed May 2, 2024
1 parent a2eb35c commit 2fd5519
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
59 changes: 20 additions & 39 deletions labapp/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import requests
import markdown
from ce import get_ce_info, get_ce_state
from fetch import cloudapp_fetch, cloudapp_req_headers, cloudapp_res_headers
from fetch import get_runner_session, cloudapp_fetch, cloudapp_req_headers, cloudapp_res_headers

app = Flask(__name__)
app.config['ce_info'] = None
Expand All @@ -21,6 +21,9 @@
cache = Cache(app)
app.secret_key = "blahblahblah"

session = get_runner_session()
session.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})

class LabException(Exception):
"""lab exception"""

Expand Down Expand Up @@ -206,10 +209,8 @@ def score():
def ex_test():
"""Example test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
url = f"https://foo.{app.config['base_url']}/"
data = cloudapp_fetch(s, url, 7, 'info', {"foo": True})
data = cloudapp_fetch(session, url, 7, 'info', {"foo": True})
return jsonify(status='success', data=data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))
Expand All @@ -218,10 +219,8 @@ def ex_test():
def ex_test2():
"""Example test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
url = f"https://bar.{app.config['base_url']}/"
data = cloudapp_fetch(s, url, 7, 'info', {"bar": True})
data = cloudapp_fetch(session, url, 7, 'info', {"bar": True})
return jsonify(status='success', data=data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))
Expand All @@ -231,13 +230,11 @@ def ex_test2():
def lb_aws():
"""Azure LB test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
ns = eph_ns()
if not ns:
raise LabException("Ephemeral NS not set")
url = f"https://{ns}.{app.config['base_url']}"
data = cloudapp_fetch(s, url, 7, 'env', 'AWS')
data = cloudapp_fetch(session, url, 7, 'env', 'AWS')
return jsonify(status='success', data=data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))
Expand All @@ -246,13 +243,11 @@ def lb_aws():
def lb_azure():
"""Azure LB test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
ns = eph_ns()
if not ns:
raise LabException("Ephemeral NS not set")
url = f"https://{ns}.{app.config['base_url']}"
data = cloudapp_fetch(s, url, 7, 'env', 'Azure')
data = cloudapp_fetch(session, url, 7, 'env', 'Azure')
return jsonify(status='success', data=data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))
Expand All @@ -261,16 +256,14 @@ def lb_azure():
def route1():
"""First Route Test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
ns = eph_ns()
if not ns:
raise LabException("Ephemeral NS not set")
base_url = app.config['base_url']
aws_url = f"https://{ns}.{base_url}/aws/raw"
azure_url = f"https://{ns}.{base_url}/azure/raw"
aws_data = cloudapp_fetch(s, aws_url, 7, 'env', 'AWS')
azure_data = cloudapp_fetch(s, azure_url, 7, 'env', 'Azure')
aws_data = cloudapp_fetch(session, aws_url, 7, 'env', 'AWS')
azure_data = cloudapp_fetch(session, azure_url, 7, 'env', 'Azure')
data = {
"aws": aws_data,
"azure": azure_data
Expand All @@ -283,18 +276,16 @@ def route1():
def route2():
"""First Route Test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
ns = eph_ns()
if not ns:
raise LabException("Ephemeral NS not set")
base_url = app.config['base_url']
aws_url = f"https://{ns}.{base_url}/"
azure_url = f"https://{ns}.{base_url}/"
s.headers["X-MCN-lab"] = "aws"
aws_data = cloudapp_fetch(s, aws_url, 7, 'env', 'AWS')
s.headers["X-MCN-lab"] = "azure"
azure_data = cloudapp_fetch(s, azure_url, 7, 'env', 'Azure')
session.headers["X-MCN-lab"] = "aws"
aws_data = cloudapp_fetch(session, aws_url, 7, 'env', 'AWS')
session.headers["X-MCN-lab"] = "azure"
azure_data = cloudapp_fetch(session, azure_url, 7, 'env', 'Azure')
data = {
"aws": aws_data,
"azure": azure_data
Expand All @@ -307,14 +298,12 @@ def route2():
def manip1():
"""First Manip Test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
ns = eph_ns()
if not ns:
raise LabException("Ephemeral NS not set")
base_url = app.config['base_url']
url = f"https://{ns}.{base_url}/aws/raw"
r_data = cloudapp_fetch(s, url, 5, 'info', {"method": "GET", "path": "/raw"})
r_data = cloudapp_fetch(session, url, 5, 'info', {"method": "GET", "path": "/raw"})
return jsonify(status='success', data=r_data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))
Expand All @@ -323,15 +312,13 @@ def manip1():
def manip2():
"""Second Manip Test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
ns = eph_ns()
if not ns:
raise LabException("Ephemeral NS not set")
base_url = app.config['base_url']
url = f"https://{ns}.{base_url}/"
t_headers = { "x-mcn-namespace": ns, "x-mcn-src-site": app.config["ce_info"]["site_name"]}
r_data = cloudapp_req_headers(s, url, 7, t_headers)
r_data = cloudapp_req_headers(session, url, 7, t_headers)
return jsonify(status='success', data=r_data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))
Expand All @@ -340,8 +327,6 @@ def manip2():
def manip3():
"""Third Manip Test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
ns = eph_ns()
if not ns:
raise LabException("Ephemeral NS not set")
Expand All @@ -350,8 +335,8 @@ def manip3():
azure_url = f"https://{ns}.{base_url}/azure/"
aws_headers = { "x-mcn-dest-site": "student-awsnet" }
azure_headers = { "x-mcn-dest-site": "student-azurenet" }
aws_data = cloudapp_res_headers(s, aws_url, 7, aws_headers)
azure_data = cloudapp_res_headers(s, azure_url, 7, azure_headers)
aws_data = cloudapp_res_headers(session, aws_url, 7, aws_headers)
azure_data = cloudapp_res_headers(session, azure_url, 7, azure_headers)
data = {
"aws": aws_data,
"azure": azure_data
Expand All @@ -364,13 +349,11 @@ def manip3():
def port1():
"""Friend test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
ns = eph_ns()
if not ns:
raise LabException("Ephemeral NS not set")
url = f"https://{ns}.{app.config['base_url']}/"
data = cloudapp_fetch(s, url, 7, 'info', {"method": "GET", "path": "/"})
data = cloudapp_fetch(session, url, 7, 'info', {"method": "GET", "path": "/"})
return jsonify(status='success', data=data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))
Expand All @@ -379,13 +362,11 @@ def port1():
def port2():
"""Friend test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
data = request.get_json()
print(data)
eph_ns = data['userInput']
url = f"https://{eph_ns}.{app.config['base_url']}/"
data = cloudapp_fetch(s, url, 7, 'info', {"method": "GET", "path": "/"})
data = cloudapp_fetch(session, url, 7, 'info', {"method": "GET", "path": "/"})
return jsonify(status='success', data=data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))
Expand Down
22 changes: 22 additions & 0 deletions labapp/app/fetch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
from requests.structures import CaseInsensitiveDict
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

def get_runner_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(500, 502, 504),
session=None
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session

def headers_cleaner(headers):
"""
Expand Down

0 comments on commit 2fd5519

Please sign in to comment.