Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions comanage_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import sys
import json
import time
import urllib.error
import urllib.request
from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES, SAFE_SYNC
Expand All @@ -27,9 +28,9 @@
TEST_LDAP_TARGET_ID = 9


MIN_TIMEOUT = 5
MAX_TIMEOUT = 625
TIMEOUTMULTIPLE = 5
TIMEOUT_MIN = 5
TIMEOUT_MULTIPLE = 5
MAX_RETRIES = 5


GET = "GET"
Expand Down Expand Up @@ -81,20 +82,27 @@ def call_api2(method, target, endpoint, authstr, **kw):

def call_api3(method, target, data, endpoint, authstr, **kw):
req = mkrequest(method, target, data, endpoint, authstr, **kw)
trying = True
currentTimeout = MIN_TIMEOUT
while trying:
retries = 0
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
currentTimeout = TIMEOUT_MIN
requestingStart = time.time()
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
payload = None
while payload == None:
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
try:
resp = urllib.request.urlopen(req, timeout=currentTimeout)
if retries > 0:
print(f"Succeeded for request {req.full_url} after {retries} retries.")
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
payload = resp.read()
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
trying = False
except urllib.error.URLError as exception:
Comment thread
brianhlin marked this conversation as resolved.
if currentTimeout < MAX_TIMEOUT:
currentTimeout *= TIMEOUTMULTIPLE
if retries < MAX_RETRIES:
print(f"Error: {exception} for request {req.full_url}, sleeping for {currentTimeout} seconds and retrying.")
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
time.sleep(currentTimeout)
currentTimeout *= TIMEOUT_MULTIPLE
retries += 1
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
else:
requestingStop = time.time()
sys.exit(
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
f"Exception raised after maximum number of retries and/or timeout {MAX_TIMEOUT} seconds reached. "
+ f"Exception reason: {exception.reason}.\n Request: {req.full_url}"
f"Exception raised after maximum number of retries reached after {requestingStop - requestingStart} seconds. Retries: {retries}. "
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
Comment thread
williamnswanson marked this conversation as resolved.
Outdated
+ f"Exception reason: {exception}.\n Request: {req.full_url}"
)

return json.loads(payload) if payload else None
Expand Down